Skip to content

Commit

Permalink
Use ESM instead of CJS. Potentially fixes JeringTech#160
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Krause committed Jul 26, 2023
1 parent cc9af0f commit 7b431fa
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,8 @@ 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);
}
} else {
respondWithError(res, `Invalid module source type: ${invocationRequest.moduleSourceType}.`);
return;
}
Expand All @@ -135,7 +131,7 @@ 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}.`);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,8 @@ 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);
}
} else {
respondWithError(res, `Invalid module source type: ${invocationRequest.moduleSourceType}.`);
return;
}
Expand All @@ -121,7 +117,7 @@ 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}.`);
return;
Expand Down
1 change: 1 addition & 0 deletions src/NodeJS/Javascript/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"private": true,
"type": "module",
"scripts": {
"build": "echo ------ Yarn Install ------ && yarn install && echo ------ Webpack Compile ------ && webpack"
},
Expand Down
16 changes: 12 additions & 4 deletions src/NodeJS/Javascript/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const path = require('path');
import * as path from 'path';
import { fileURLToPath } from 'url';

module.exports = env => {
const __dirname = path.dirname(fileURLToPath(import.meta.url));

export default env => {

let mode = env.mode.toLowerCase() === 'release' ? 'production' : 'development'; // Default to development, production mode minifies scripts
console.log(`Mode: ${mode}.`);

return {
mode: mode,
target: 'node',
target: 'node14',
resolve: {
extensions: ['.ts', '.js']
},
Expand All @@ -19,9 +22,14 @@ module.exports = env => {
entry: env.entry,
output: {
hashFunction: 'xxhash64',
libraryTarget: 'commonjs2',
library: {
type: 'module'
},
path: path.join(__dirname, 'bin', env.mode),
filename: path.basename(env.entry, path.extname(env.entry)) + '.js'
},
experiments: {
outputModule: true
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal ProcessStartInfo CreateStartInfo(string nodeServerScript)
#endif
var startInfo = new ProcessStartInfo(_nodeJSProcessOptions.ExecutablePath!) // ConfigureNodeJSProcessOptions sets ExecutablePath to "node" if user specified value is null, whitespace or an empty string
{
Arguments = $"{_nodeJSProcessOptions.NodeAndV8Options} -e \"{nodeServerScript}\" -- --parentPid {currentProcessPid} --port {_nodeJSProcessOptions.Port}",
Arguments = $"{_nodeJSProcessOptions.NodeAndV8Options} --input-type=module -e \"{nodeServerScript}\" -- --parentPid {currentProcessPid} --port {_nodeJSProcessOptions.Port}",
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void CreateStartInfo_CreatesStartInfo()
#else
int currentProcessPid = Process.GetCurrentProcess().Id;
#endif
Assert.Equal($"{dummyNodeAndV8Options} -e \"{dummyNodeServerScript}\" -- --parentPid {currentProcessPid} --port {dummyPort}", result.Arguments);
Assert.Equal($"{dummyNodeAndV8Options} --input-type=module -e \"{dummyNodeServerScript}\" -- --parentPid {currentProcessPid} --port {dummyPort}", result.Arguments);
Assert.False(result.UseShellExecute);
Assert.True(result.RedirectStandardInput);
Assert.True(result.RedirectStandardOutput);
Expand Down

0 comments on commit 7b431fa

Please sign in to comment.