Skip to content

Commit a0566f6

Browse files
committed
module: remove bogus assertion in CJS entrypoint handling with --import
The synchronous CJS translator can handle entrypoints now, this can be hit when --import is used, so lift the bogus assertions and added tests. PR-URL: nodejs#54592 Fixes: nodejs#54577 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 285f0fa commit a0566f6

File tree

2 files changed

+101
-56
lines changed

2 files changed

+101
-56
lines changed

lib/internal/modules/esm/translators.js

-2
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,9 @@ function createCJSModuleWrap(url, source, isMain, loadCJS = loadCJSModule) {
285285

286286
translators.set('commonjs-sync', function requireCommonJS(url, source, isMain) {
287287
initCJSParseSync();
288-
assert(!isMain); // This is only used by imported CJS modules.
289288

290289
return createCJSModuleWrap(url, source, isMain, (module, source, url, filename, isMain) => {
291290
assert(module === CJSModule._cache[filename]);
292-
assert(!isMain);
293291
wrapModuleLoad(filename, null, isMain);
294292
});
295293
});

test/es-module/test-require-module-preload.js

+101-54
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,117 @@
11
'use strict';
22

33
require('../common');
4-
const { spawnSyncAndExitWithoutError } = require('../common/child_process');
5-
const fixtures = require('../common/fixtures');
6-
4+
const { spawnSyncAndAssert } = require('../common/child_process');
5+
const { fixturesDir } = require('../common/fixtures');
76
const stderr = /ExperimentalWarning: Support for loading ES Module in require/;
87

9-
// Test named exports.
10-
{
11-
spawnSyncAndExitWithoutError(
12-
process.execPath,
13-
[ '--experimental-require-module', '-r', fixtures.path('../fixtures/es-module-loaders/module-named-exports.mjs') ],
14-
{
15-
stderr,
16-
}
17-
);
18-
}
8+
function testPreload(preloadFlag) {
9+
// Test named exports.
10+
{
11+
spawnSyncAndAssert(
12+
process.execPath,
13+
[
14+
'--experimental-require-module',
15+
preloadFlag,
16+
'./es-module-loaders/module-named-exports.mjs',
17+
'./printA.js',
18+
],
19+
{
20+
cwd: fixturesDir
21+
},
22+
{
23+
stdout: 'A',
24+
stderr,
25+
trim: true,
26+
}
27+
);
28+
}
1929

20-
// Test ESM that import ESM.
21-
{
22-
spawnSyncAndExitWithoutError(
23-
process.execPath,
24-
[ '--experimental-require-module', '-r', fixtures.path('../fixtures/es-modules/import-esm.mjs') ],
25-
{
26-
stderr,
27-
stdout: 'world',
28-
trim: true,
29-
}
30-
);
31-
}
30+
// Test ESM that import ESM.
31+
{
32+
spawnSyncAndAssert(
33+
process.execPath,
34+
[
35+
'--experimental-require-module',
36+
preloadFlag,
37+
'./es-modules/import-esm.mjs',
38+
'./printA.js',
39+
],
40+
{
41+
cwd: fixturesDir
42+
},
43+
{
44+
stderr,
45+
stdout: /^world\s+A$/,
46+
trim: true,
47+
}
48+
);
49+
}
3250

33-
// Test ESM that import CJS.
34-
{
35-
spawnSyncAndExitWithoutError(
36-
process.execPath,
37-
[ '--experimental-require-module', '-r', fixtures.path('../fixtures/es-modules/cjs-exports.mjs') ],
38-
{
39-
stdout: 'ok',
40-
stderr,
41-
trim: true,
42-
}
43-
);
44-
}
51+
// Test ESM that import CJS.
52+
{
53+
spawnSyncAndAssert(
54+
process.execPath,
55+
[
56+
'--experimental-require-module',
57+
preloadFlag,
58+
'./es-modules/cjs-exports.mjs',
59+
'./printA.js',
60+
],
61+
{
62+
cwd: fixturesDir
63+
},
64+
{
65+
stdout: /^ok\s+A$/,
66+
stderr,
67+
trim: true,
68+
}
69+
);
70+
}
4571

46-
// Test ESM that require() CJS.
47-
// Can't use the common/index.mjs here because that checks the globals, and
48-
// -r injects a bunch of globals.
49-
{
50-
spawnSyncAndExitWithoutError(
51-
process.execPath,
52-
[ '--experimental-require-module', '-r', fixtures.path('../fixtures/es-modules/require-cjs.mjs') ],
53-
{
54-
stdout: 'world',
55-
stderr,
56-
trim: true,
57-
}
58-
);
72+
// Test ESM that require() CJS.
73+
// Can't use the common/index.mjs here because that checks the globals, and
74+
// -r injects a bunch of globals.
75+
{
76+
spawnSyncAndAssert(
77+
process.execPath,
78+
[
79+
'--experimental-require-module',
80+
preloadFlag,
81+
'./es-modules/require-cjs.mjs',
82+
'./printA.js',
83+
],
84+
{
85+
cwd: fixturesDir
86+
},
87+
{
88+
stdout: /^world\s+A$/,
89+
stderr,
90+
trim: true,
91+
}
92+
);
93+
}
5994
}
6095

61-
// Test "type": "module" and "main" field in package.json.
96+
testPreload('--require');
97+
testPreload('--import');
98+
99+
// Test "type": "module" and "main" field in package.json, this is only for --require because
100+
// --import does not support extension-less preloads.
62101
{
63-
spawnSyncAndExitWithoutError(
102+
spawnSyncAndAssert(
64103
process.execPath,
65-
[ '--experimental-require-module', '-r', fixtures.path('../fixtures/es-modules/package-type-module') ],
104+
[
105+
'--experimental-require-module',
106+
'--require',
107+
'./es-modules/package-type-module',
108+
'./printA.js',
109+
],
110+
{
111+
cwd: fixturesDir
112+
},
66113
{
67-
stdout: 'package-type-module',
114+
stdout: /^package-type-module\s+A$/,
68115
stderr,
69116
trim: true,
70117
}

0 commit comments

Comments
 (0)