Skip to content

Commit 59b108a

Browse files
authored
Merge pull request #173 from aKzenT/fix-160
Use ESM instead of CJS. Potentially fixes #160
2 parents c69be47 + 1fbe5a4 commit 59b108a

File tree

12 files changed

+221
-945
lines changed

12 files changed

+221
-945
lines changed

Changelog.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ This project uses [semantic versioning](http://semver.org/spec/v2.0.0.html). Ref
33
*[Semantic Versioning in Practice](https://www.jering.tech/articles/semantic-versioning-in-practice)*
44
for an overview of semantic versioning.
55

6-
## [Unreleased](https://github.com/JeringTech/Javascript.NodeJS/compare/7.0.0-beta.4...HEAD)
6+
## [Unreleased](https://github.com/JeringTech/Javascript.NodeJS/compare/7.0.0-beta.5...HEAD)
7+
8+
## [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
9+
### Changes
10+
- 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)).
711

812
## [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
913
### Fixes

ReadMe.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,7 @@ Contributions are welcome!
17831783
- [samcic](https://github.com/samcic)
17841784
- [johnrom](https://github.com/johnrom)
17851785
- [aKzenT](https://github.com/aKzenT)
1786+
- [thebuilder](https://github.com/thebuilder)
17861787

17871788
## About
17881789
Follow [@JeringTech](https://twitter.com/JeringTech) for updates and more.

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// The typings for module are incomplete and can't be augmented, so import as any.
2-
const Module = require('module');
1+
import ModuleTemp from 'module';
32
import * as http from 'http';
43
import { AddressInfo, Socket } from 'net';
54
import * as path from 'path';
@@ -8,6 +7,9 @@ import InvocationRequest from '../../../InvocationData/InvocationRequest';
87
import ModuleSourceType from '../../../InvocationData/ModuleSourceType';
98
import { getTempIdentifier, respondWithError, setup } from './Shared';
109

10+
// The typings for module are incomplete and can't be augmented, so import as any.
11+
const Module = ModuleTemp as any;
12+
1113
// Setup
1214
const [args, projectDir, moduleResolutionPaths] = setup();
1315

@@ -118,11 +120,7 @@ function serverOnRequestListener(req: http.IncomingMessage, res: http.ServerResp
118120
}
119121
} else if (invocationRequest.moduleSourceType === ModuleSourceType.File) {
120122
const resolvedPath = path.resolve(projectDir, invocationRequest.moduleSource);
121-
if (resolvedPath.endsWith('.mjs')) {
122-
exports = await import(/* webpackIgnore: true */ 'file:///' + resolvedPath.replaceAll('\\', '/'));
123-
} else {
124-
exports = __non_webpack_require__(resolvedPath);
125-
}
123+
exports = await import('file:///' + resolvedPath.replaceAll('\\', '/'));
126124
} else {
127125
respondWithError(res, `Invalid module source type: ${invocationRequest.moduleSourceType}.`);
128126
return;
@@ -135,9 +133,10 @@ function serverOnRequestListener(req: http.IncomingMessage, res: http.ServerResp
135133
// Get function to invoke
136134
let functionToInvoke: Function;
137135
if (invocationRequest.exportName != null) {
138-
functionToInvoke = exports[invocationRequest.exportName];
136+
functionToInvoke = exports[invocationRequest.exportName] ?? exports.default?.[invocationRequest.exportName];
139137
if (functionToInvoke == null) {
140-
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}.`);
138+
let availableExports = Object.keys(exports).join(', ');
139+
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}. Available exports are: ${availableExports}`);
141140
return;
142141
}
143142
if (!(typeof functionToInvoke === 'function')) {

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// The typings for module are incomplete and can't be augmented, so import as any.
2-
const Module = require('module');
1+
import ModuleTemp from 'module';
32
import * as http2 from 'http2';
43
import { AddressInfo } from 'net';
54
import * as path from 'path';
@@ -8,6 +7,9 @@ import InvocationRequest from '../../../InvocationData/InvocationRequest';
87
import ModuleSourceType from '../../../InvocationData/ModuleSourceType';
98
import { getTempIdentifier, respondWithError, setup } from './Shared';
109

10+
// The typings for module are incomplete and can't be augmented, so import as any.
11+
const Module = ModuleTemp as any;
12+
1113
// Setup
1214
const [args, projectDir, moduleResolutionPaths] = setup();
1315

@@ -104,11 +106,7 @@ function serverOnRequestListener(req: http2.Http2ServerRequest, res: http2.Http2
104106
}
105107
} else if (invocationRequest.moduleSourceType === ModuleSourceType.File) {
106108
const resolvedPath = path.resolve(projectDir, invocationRequest.moduleSource);
107-
if (resolvedPath.endsWith('.mjs')) {
108-
exports = await import(/* webpackIgnore: true */ 'file:///' + resolvedPath.replaceAll('\\', '/'));
109-
} else {
110-
exports = __non_webpack_require__(resolvedPath);
111-
}
109+
exports = await import('file:///' + resolvedPath.replaceAll('\\', '/'));
112110
} else {
113111
respondWithError(res, `Invalid module source type: ${invocationRequest.moduleSourceType}.`);
114112
return;
@@ -121,9 +119,10 @@ function serverOnRequestListener(req: http2.Http2ServerRequest, res: http2.Http2
121119
// Get function to invoke
122120
let functionToInvoke: Function;
123121
if (invocationRequest.exportName != null) {
124-
functionToInvoke = exports[invocationRequest.exportName];
122+
functionToInvoke = exports[invocationRequest.exportName] ?? exports.default?.[invocationRequest.exportName];
125123
if (functionToInvoke == null) {
126-
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}.`);
124+
let availableExports = Object.keys(exports).join(', ');
125+
respondWithError(res, `The module ${getTempIdentifier(invocationRequest)} has no export named ${invocationRequest.exportName}. Available exports are: ${availableExports}`);
127126
return;
128127
}
129128
if (!(typeof functionToInvoke === 'function')) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const path = require("path");
1+
import * as path from 'path';
22
import * as stream from 'stream';
33
import * as http from 'http';
44
import * as http2 from 'http2';

src/NodeJS/Javascript/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{
22
"private": true,
3+
"type": "module",
34
"scripts": {
4-
"build": "echo ------ Yarn Install ------ && yarn install && echo ------ Webpack Compile ------ && webpack"
5+
"build": "echo ------ Yarn Install ------ && yarn install && echo ------ Vite Compile ------ && vite build"
56
},
67
"dependencies": {
78
"@types/node": "^18.11.18",
8-
"ts-loader": "^9.2.6",
99
"typescript": "^4.9.4",
10-
"webpack": "^5.75.0",
11-
"webpack-cli": "^5.0.1"
10+
"vite": "^4.4.7"
1211
}
1312
}

src/NodeJS/Javascript/vite.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from "vite";
2+
3+
export default defineConfig({
4+
build: {
5+
emptyOutDir: false,
6+
ssr: true,
7+
},
8+
});

src/NodeJS/Javascript/webpack.config.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)