Skip to content

Commit 54e48eb

Browse files
committed
Add support for mjs.
1 parent 962276a commit 54e48eb

10 files changed

+263
-19
lines changed

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+11-6
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ 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-
exports = __non_webpack_require__(resolvedPath);
121+
if (resolvedPath.endsWith('.mjs')) {
122+
exports = await import(/* webpackIgnore: true */ 'file:///' + resolvedPath.replaceAll('\\', '/'));
123+
} else {
124+
exports = __non_webpack_require__(resolvedPath);
125+
}
122126
} else {
123127
respondWithError(res, `Invalid module source type: ${invocationRequest.moduleSourceType}.`);
124128
return;
@@ -140,12 +144,13 @@ function serverOnRequestListener(req: http.IncomingMessage, res: http.ServerResp
140144
respondWithError(res, `The export named ${invocationRequest.exportName} from module ${getTempIdentifier(invocationRequest)} is not a function.`);
141145
return;
142146
}
143-
} else {
144-
if (!(typeof exports === 'function')) {
145-
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} does not export a function.`);
146-
return;
147-
}
147+
} else if (typeof exports === 'function') {
148148
functionToInvoke = exports;
149+
} else if (typeof exports.default === 'function') { // .mjs default export
150+
functionToInvoke = exports.default;
151+
} else {
152+
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} does not export a function.`);
153+
return;
149154
}
150155

151156
let callbackCalled = false;

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

+11-6
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ 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-
exports = __non_webpack_require__(resolvedPath);
107+
if (resolvedPath.endsWith('.mjs')) {
108+
exports = await import(/* webpackIgnore: true */ 'file:///' + resolvedPath.replaceAll('\\', '/'));
109+
} else {
110+
exports = __non_webpack_require__(resolvedPath);
111+
}
108112
} else {
109113
respondWithError(res, `Invalid module source type: ${invocationRequest.moduleSourceType}.`);
110114
return;
@@ -126,12 +130,13 @@ function serverOnRequestListener(req: http2.Http2ServerRequest, res: http2.Http2
126130
respondWithError(res, `The export named ${invocationRequest.exportName} from module ${getTempIdentifier(invocationRequest)} is not a function.`);
127131
return;
128132
}
129-
} else {
130-
if (!(typeof exports === 'function')) {
131-
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} does not export a function.`);
132-
return;
133-
}
133+
} else if (typeof exports === 'function') {
134134
functionToInvoke = exports;
135+
} else if (typeof exports.default === 'function') { // .mjs default export
136+
functionToInvoke = exports.default;
137+
} else {
138+
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} does not export a function.`);
139+
return;
135140
}
136141

137142
let callbackCalled = false;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import path = require("path");
1+
const path = require("path");
22
import * as http from 'http';
33
import * as http2 from 'http2';
44
import InvocationRequest from "../../../InvocationData/InvocationRequest";

src/NodeJS/Javascript/tsconfig.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"compilerOptions": {
3-
"target": "es6",
4-
"module": "commonjs",
3+
"target": "es2022",
4+
"module": "es2022",
5+
"esModuleInterop": true,
56
"moduleResolution": "node",
67
"typeRoots": [ "node_modules/@types" ],
78
"types": [ "node" ]

src/NodeJS/Jering.Javascript.NodeJS.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
<ItemGroup>
6868
<ProjectReference Include="..\..\generators\Jering.Javascript.NodeJS.CodeGenerators\Jering.Javascript.NodeJS.CodeGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
6969
<!-- Documentation only needs to be generated once, for the latest framework -->
70-
<ProjectReference Condition=" '$(TargetFramework)' == 'net5.0' " Include="..\..\generators\Jering.Javascript.NodeJS.DocumentationGenerators\Jering.Javascript.NodeJS.DocumentationGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
70+
<ProjectReference Condition=" '$(TargetFramework)' == 'net7.0' " Include="..\..\generators\Jering.Javascript.NodeJS.DocumentationGenerators\Jering.Javascript.NodeJS.DocumentationGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
7171
</ItemGroup>
7272

7373
<ItemGroup>

0 commit comments

Comments
 (0)