Skip to content

Use ESM instead of CJS. Potentially fixes #160 #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ This project uses [semantic versioning](http://semver.org/spec/v2.0.0.html). Ref
*[Semantic Versioning in Practice](https://www.jering.tech/articles/semantic-versioning-in-practice)*
for an overview of semantic versioning.

## [Unreleased](https://github.com/JeringTech/Javascript.NodeJS/compare/7.0.0-beta.4...HEAD)
## [Unreleased](https://github.com/JeringTech/Javascript.NodeJS/compare/7.0.0-beta.5...HEAD)

## [7.0.0-beta.5](https://github.com/JeringTech/Javascript.NodeJS/compare/7.0.0-beta.4...7.0.0-beta.5) - Jul 28, 2023
### Changes
- Changed server scripts from CommonJS to EcmaScript modules. This should improve the reliability of `.mjs` file invocations. ([#173](https://github.com/JeringTech/Javascript.NodeJS/pull/173)).

## [7.0.0-beta.4](https://github.com/JeringTech/Javascript.NodeJS/compare/7.0.0-beta.3...7.0.0-beta.4) - Apr 18, 2023
### Fixes
Expand Down
1 change: 1 addition & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,7 @@ Contributions are welcome!
- [samcic](https://github.com/samcic)
- [johnrom](https://github.com/johnrom)
- [aKzenT](https://github.com/aKzenT)
- [thebuilder](https://github.com/thebuilder)

## About
Follow [@JeringTech](https://twitter.com/JeringTech) for updates and more.
17 changes: 8 additions & 9 deletions src/NodeJS/Javascript/Servers/OutOfProcess/Http/Http11Server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// The typings for module are incomplete and can't be augmented, so import as any.
const Module = require('module');
import ModuleTemp from 'module';
import * as http from 'http';
import { AddressInfo, Socket } from 'net';
import * as path from 'path';
Expand All @@ -8,6 +7,9 @@ import InvocationRequest from '../../../InvocationData/InvocationRequest';
import ModuleSourceType from '../../../InvocationData/ModuleSourceType';
import { getTempIdentifier, respondWithError, setup } from './Shared';

// The typings for module are incomplete and can't be augmented, so import as any.
const Module = ModuleTemp as any;

// Setup
const [args, projectDir, moduleResolutionPaths] = setup();

Expand Down Expand Up @@ -118,11 +120,7 @@ function serverOnRequestListener(req: http.IncomingMessage, res: http.ServerResp
}
} else if (invocationRequest.moduleSourceType === ModuleSourceType.File) {
const resolvedPath = path.resolve(projectDir, invocationRequest.moduleSource);
if (resolvedPath.endsWith('.mjs')) {
exports = await import(/* webpackIgnore: true */ 'file:///' + resolvedPath.replaceAll('\\', '/'));
} else {
exports = __non_webpack_require__(resolvedPath);
}
exports = await import('file:///' + resolvedPath.replaceAll('\\', '/'));
} else {
respondWithError(res, `Invalid module source type: ${invocationRequest.moduleSourceType}.`);
return;
Expand All @@ -135,9 +133,10 @@ function serverOnRequestListener(req: http.IncomingMessage, res: http.ServerResp
// Get function to invoke
let functionToInvoke: Function;
if (invocationRequest.exportName != null) {
functionToInvoke = exports[invocationRequest.exportName];
functionToInvoke = exports[invocationRequest.exportName] ?? exports.default?.[invocationRequest.exportName];
if (functionToInvoke == null) {
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}.`);
let availableExports = Object.keys(exports).join(', ');
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}. Available exports are: ${availableExports}`);
return;
}
if (!(typeof functionToInvoke === 'function')) {
Expand Down
17 changes: 8 additions & 9 deletions src/NodeJS/Javascript/Servers/OutOfProcess/Http/Http20Server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// The typings for module are incomplete and can't be augmented, so import as any.
const Module = require('module');
import ModuleTemp from 'module';
import * as http2 from 'http2';
import { AddressInfo } from 'net';
import * as path from 'path';
Expand All @@ -8,6 +7,9 @@ import InvocationRequest from '../../../InvocationData/InvocationRequest';
import ModuleSourceType from '../../../InvocationData/ModuleSourceType';
import { getTempIdentifier, respondWithError, setup } from './Shared';

// The typings for module are incomplete and can't be augmented, so import as any.
const Module = ModuleTemp as any;

// Setup
const [args, projectDir, moduleResolutionPaths] = setup();

Expand Down Expand Up @@ -104,11 +106,7 @@ function serverOnRequestListener(req: http2.Http2ServerRequest, res: http2.Http2
}
} else if (invocationRequest.moduleSourceType === ModuleSourceType.File) {
const resolvedPath = path.resolve(projectDir, invocationRequest.moduleSource);
if (resolvedPath.endsWith('.mjs')) {
exports = await import(/* webpackIgnore: true */ 'file:///' + resolvedPath.replaceAll('\\', '/'));
} else {
exports = __non_webpack_require__(resolvedPath);
}
exports = await import('file:///' + resolvedPath.replaceAll('\\', '/'));
} else {
respondWithError(res, `Invalid module source type: ${invocationRequest.moduleSourceType}.`);
return;
Expand All @@ -121,9 +119,10 @@ function serverOnRequestListener(req: http2.Http2ServerRequest, res: http2.Http2
// Get function to invoke
let functionToInvoke: Function;
if (invocationRequest.exportName != null) {
functionToInvoke = exports[invocationRequest.exportName];
functionToInvoke = exports[invocationRequest.exportName] ?? exports.default?.[invocationRequest.exportName];
if (functionToInvoke == null) {
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}.`);
let availableExports = Object.keys(exports).join(', ');
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}. Available exports are: ${availableExports}`);
return;
}
if (!(typeof functionToInvoke === 'function')) {
Expand Down
2 changes: 1 addition & 1 deletion src/NodeJS/Javascript/Servers/OutOfProcess/Http/Shared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const path = require("path");
import * as path from 'path';
import * as stream from 'stream';
import * as http from 'http';
import * as http2 from 'http2';
Expand Down
7 changes: 3 additions & 4 deletions src/NodeJS/Javascript/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{
"private": true,
"type": "module",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is node correctly running the bundled files? Just wondering if it correctly detects the package.json, so it can see the type: "module". Otherwise it would see the compiled .js files as CommonJS.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only relevant during the build of the library itself. The compiled files are embedded in the library and are passed to nodejs as a string on the command line. For this reason we have to specify --input-type=module as well when calling node. We cannot depend on any package.json being present at the time of execution, because there could be none and also the compiled file is always ESM regardless of what the package.json in the CWD specifies.

"scripts": {
"build": "echo ------ Yarn Install ------ && yarn install && echo ------ Webpack Compile ------ && webpack"
"build": "echo ------ Yarn Install ------ && yarn install && echo ------ Vite Compile ------ && vite build"
},
"dependencies": {
"@types/node": "^18.11.18",
"ts-loader": "^9.2.6",
"typescript": "^4.9.4",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1"
"vite": "^4.4.7"
}
}
8 changes: 8 additions & 0 deletions src/NodeJS/Javascript/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from "vite";

export default defineConfig({
build: {
emptyOutDir: false,
ssr: true,
},
});
27 changes: 0 additions & 27 deletions src/NodeJS/Javascript/webpack.config.js

This file was deleted.

Loading