Skip to content

Commit 7b431fa

Browse files
author
Thomas Krause
committed
Use ESM instead of CJS. Potentially fixes #160
1 parent cc9af0f commit 7b431fa

6 files changed

Lines changed: 17 additions & 16 deletions

File tree

src/NodeJS/Javascript/Servers/OutOfProcess/Http/Http11Server.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,8 @@ function serverOnRequestListener(req: http.IncomingMessage, res: http.ServerResp
118118
}
119119
} else if (invocationRequest.moduleSourceType === ModuleSourceType.File) {
120120
const resolvedPath = path.resolve(projectDir, invocationRequest.moduleSource);
121-
if (resolvedPath.endsWith('.mjs')) {
122121
exports = await import(/* webpackIgnore: true */ 'file:///' + resolvedPath.replaceAll('\\', '/'));
123122
} else {
124-
exports = __non_webpack_require__(resolvedPath);
125-
}
126-
} else {
127123
respondWithError(res, `Invalid module source type: ${invocationRequest.moduleSourceType}.`);
128124
return;
129125
}
@@ -135,7 +131,7 @@ function serverOnRequestListener(req: http.IncomingMessage, res: http.ServerResp
135131
// Get function to invoke
136132
let functionToInvoke: Function;
137133
if (invocationRequest.exportName != null) {
138-
functionToInvoke = exports[invocationRequest.exportName];
134+
functionToInvoke = exports[invocationRequest.exportName] ?? exports.default?.[invocationRequest.exportName];
139135
if (functionToInvoke == null) {
140136
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}.`);
141137
return;

src/NodeJS/Javascript/Servers/OutOfProcess/Http/Http20Server.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,8 @@ function serverOnRequestListener(req: http2.Http2ServerRequest, res: http2.Http2
104104
}
105105
} else if (invocationRequest.moduleSourceType === ModuleSourceType.File) {
106106
const resolvedPath = path.resolve(projectDir, invocationRequest.moduleSource);
107-
if (resolvedPath.endsWith('.mjs')) {
108107
exports = await import(/* webpackIgnore: true */ 'file:///' + resolvedPath.replaceAll('\\', '/'));
109108
} else {
110-
exports = __non_webpack_require__(resolvedPath);
111-
}
112-
} else {
113109
respondWithError(res, `Invalid module source type: ${invocationRequest.moduleSourceType}.`);
114110
return;
115111
}
@@ -121,7 +117,7 @@ function serverOnRequestListener(req: http2.Http2ServerRequest, res: http2.Http2
121117
// Get function to invoke
122118
let functionToInvoke: Function;
123119
if (invocationRequest.exportName != null) {
124-
functionToInvoke = exports[invocationRequest.exportName];
120+
functionToInvoke = exports[invocationRequest.exportName] ?? exports.default?.[invocationRequest.exportName];
125121
if (functionToInvoke == null) {
126122
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}.`);
127123
return;

src/NodeJS/Javascript/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"private": true,
3+
"type": "module",
34
"scripts": {
45
"build": "echo ------ Yarn Install ------ && yarn install && echo ------ Webpack Compile ------ && webpack"
56
},
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
const path = require('path');
1+
import * as path from 'path';
2+
import { fileURLToPath } from 'url';
23

3-
module.exports = env => {
4+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
5+
6+
export default env => {
47

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

811
return {
912
mode: mode,
10-
target: 'node',
13+
target: 'node14',
1114
resolve: {
1215
extensions: ['.ts', '.js']
1316
},
@@ -19,9 +22,14 @@ module.exports = env => {
1922
entry: env.entry,
2023
output: {
2124
hashFunction: 'xxhash64',
22-
libraryTarget: 'commonjs2',
25+
library: {
26+
type: 'module'
27+
},
2328
path: path.join(__dirname, 'bin', env.mode),
2429
filename: path.basename(env.entry, path.extname(env.entry)) + '.js'
30+
},
31+
experiments: {
32+
outputModule: true
2533
}
2634
};
2735
};

src/NodeJS/NodeJSServiceImplementations/OutOfProcess/NodeJSProcessFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ internal ProcessStartInfo CreateStartInfo(string nodeServerScript)
4848
#endif
4949
var startInfo = new ProcessStartInfo(_nodeJSProcessOptions.ExecutablePath!) // ConfigureNodeJSProcessOptions sets ExecutablePath to "node" if user specified value is null, whitespace or an empty string
5050
{
51-
Arguments = $"{_nodeJSProcessOptions.NodeAndV8Options} -e \"{nodeServerScript}\" -- --parentPid {currentProcessPid} --port {_nodeJSProcessOptions.Port}",
51+
Arguments = $"{_nodeJSProcessOptions.NodeAndV8Options} --input-type=module -e \"{nodeServerScript}\" -- --parentPid {currentProcessPid} --port {_nodeJSProcessOptions.Port}",
5252
UseShellExecute = false,
5353
RedirectStandardInput = true,
5454
RedirectStandardOutput = true,

test/NodeJS/NodeJSServiceImplementations/OutOfProcess/NodeJSProcessFactoryUnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void CreateStartInfo_CreatesStartInfo()
4242
#else
4343
int currentProcessPid = Process.GetCurrentProcess().Id;
4444
#endif
45-
Assert.Equal($"{dummyNodeAndV8Options} -e \"{dummyNodeServerScript}\" -- --parentPid {currentProcessPid} --port {dummyPort}", result.Arguments);
45+
Assert.Equal($"{dummyNodeAndV8Options} --input-type=module -e \"{dummyNodeServerScript}\" -- --parentPid {currentProcessPid} --port {dummyPort}", result.Arguments);
4646
Assert.False(result.UseShellExecute);
4747
Assert.True(result.RedirectStandardInput);
4848
Assert.True(result.RedirectStandardOutput);

0 commit comments

Comments
 (0)