Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: remove bogus assertion in CJS entrypoint handling with --import #54592

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,9 @@ function createCJSModuleWrap(url, source, isMain, loadCJS = loadCJSModule) {

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

return createCJSModuleWrap(url, source, isMain, (module, source, url, filename, isMain) => {
assert(module === CJSModule._cache[filename]);
assert(!isMain);
wrapModuleLoad(filename, null, isMain);
});
});
Expand Down
133 changes: 83 additions & 50 deletions test/es-module/test-require-module-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,98 @@ const fixtures = require('../common/fixtures');

const stderr = /ExperimentalWarning: Support for loading ES Module in require/;

// Test named exports.
{
spawnSyncAndExitWithoutError(
process.execPath,
[ '--experimental-require-module', '-r', fixtures.path('../fixtures/es-module-loaders/module-named-exports.mjs') ],
{
stderr,
}
);
}
function testPreload(preloadFlag) {
// Test named exports.
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no need for those blocks since we're not declaring any variable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s mostly for folding in the editors that support folding blocks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't most editors treat multiline function calls as foldable blocks?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my experience it doesn't always work correctly, but blocks almost never fail.

spawnSyncAndExitWithoutError(
process.execPath,
[
'--experimental-require-module',
preloadFlag,
fixtures.path('es-module-loaders/module-named-exports.mjs'),
fixtures.path('printA.js'),
],
{
stdout: 'A',
stderr,
trim: true,
}
);
}

// Test ESM that import ESM.
{
spawnSyncAndExitWithoutError(
process.execPath,
[ '--experimental-require-module', '-r', fixtures.path('../fixtures/es-modules/import-esm.mjs') ],
{
stderr,
stdout: 'world',
trim: true,
}
);
}
// Test ESM that import ESM.
{
spawnSyncAndExitWithoutError(
process.execPath,
[
'--experimental-require-module',
preloadFlag,
fixtures.path('es-modules/import-esm.mjs'),
joyeecheung marked this conversation as resolved.
Show resolved Hide resolved
fixtures.path('printA.js'),
],
{
stderr,
stdout: /^world\s+A$/,
trim: true,
}
);
}

// Test ESM that import CJS.
{
spawnSyncAndExitWithoutError(
process.execPath,
[ '--experimental-require-module', '-r', fixtures.path('../fixtures/es-modules/cjs-exports.mjs') ],
{
stdout: 'ok',
stderr,
trim: true,
}
);
}
// Test ESM that import CJS.
{
spawnSyncAndExitWithoutError(
process.execPath,
[
'--experimental-require-module',
preloadFlag,
fixtures.path('es-modules/cjs-exports.mjs'),
fixtures.path('printA.js'),
],
{
stdout: /^ok\s+A$/,
stderr,
trim: true,
}
);
}

// Test ESM that require() CJS.
// Can't use the common/index.mjs here because that checks the globals, and
// -r injects a bunch of globals.
{
spawnSyncAndExitWithoutError(
process.execPath,
[ '--experimental-require-module', '-r', fixtures.path('../fixtures/es-modules/require-cjs.mjs') ],
{
stdout: 'world',
stderr,
trim: true,
}
);
// Test ESM that require() CJS.
// Can't use the common/index.mjs here because that checks the globals, and
// -r injects a bunch of globals.
{
spawnSyncAndExitWithoutError(
process.execPath,
[
'--experimental-require-module',
preloadFlag,
fixtures.path('es-modules/require-cjs.mjs'),
fixtures.path('printA.js'),
],
{
stdout: /^world\s+A$/,
stderr,
trim: true,
}
);
}
}

// Test "type": "module" and "main" field in package.json.
testPreload('--require');
testPreload('--import');

// Test "type": "module" and "main" field in package.json, this is only for --require because
// --import does not support extension-less preloads.
{
spawnSyncAndExitWithoutError(
process.execPath,
[ '--experimental-require-module', '-r', fixtures.path('../fixtures/es-modules/package-type-module') ],
[
'--experimental-require-module',
'--require',
fixtures.path('es-modules/package-type-module'),
fixtures.path('printA.js'),
],
{
stdout: 'package-type-module',
stdout: /^package-type-module\s+A$/,
stderr,
trim: true,
}
Expand Down
Loading