-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
esm: bypass CJS loader in default load under --default-type=module
#50004
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f2000b1
esm: bypass CJS loader in default load under `--default-type=module`
aduh95 bd3208a
fixup! esm: bypass CJS loader in default load under `--default-type=m…
aduh95 fadff3b
fixup! esm: bypass CJS loader in default load under `--default-type=m…
aduh95 197863a
fixup! esm: bypass CJS loader in default load under `--default-type=m…
aduh95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
esm: bypass CJS loader in default load under
--default-type=module
This allows user to opt-out from using the monkey-patchable CJS loader, even to load CJS modules.
- Loading branch information
commit f2000b134e1e7dea610f4424ba8a0f10c3291219
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,101 @@ | ||
| import { spawnPromisified } from '../common/index.mjs'; | ||
| import * as fixtures from '../common/fixtures.mjs'; | ||
| import { describe, it } from 'node:test'; | ||
| import { match, strictEqual } from 'node:assert'; | ||
|
|
||
| describe('--experimental-default-type=module should not affect the interpretation of files with unknown extensions', | ||
| { concurrency: true }, () => { | ||
| it('should error on an entry point with an unknown extension', async () => { | ||
| const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ | ||
| '--experimental-default-type=module', | ||
| fixtures.path('es-modules/package-type-module/extension.unknown'), | ||
| ]); | ||
|
|
||
| match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/); | ||
| strictEqual(stdout, ''); | ||
| strictEqual(code, 1); | ||
| strictEqual(signal, null); | ||
| }); | ||
|
|
||
| it('should error on an import with an unknown extension', async () => { | ||
| const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ | ||
| '--experimental-default-type=module', | ||
| fixtures.path('es-modules/package-type-module/imports-unknownext.mjs'), | ||
| ]); | ||
|
|
||
| match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/); | ||
| strictEqual(stdout, ''); | ||
| strictEqual(code, 1); | ||
| strictEqual(signal, null); | ||
| }); | ||
| }); | ||
| import { deepStrictEqual, match, strictEqual } from 'node:assert'; | ||
|
|
||
| describe('--experimental-default-type=module', { concurrency: true }, () => { | ||
| describe('should not affect the interpretation of files with unknown extensions', { concurrency: true }, () => { | ||
| it('should error on an entry point with an unknown extension', async () => { | ||
| const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ | ||
| '--experimental-default-type=module', | ||
| fixtures.path('es-modules/package-type-module/extension.unknown'), | ||
| ]); | ||
|
|
||
| match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/); | ||
| strictEqual(stdout, ''); | ||
| strictEqual(code, 1); | ||
| strictEqual(signal, null); | ||
| }); | ||
|
|
||
| it('should error on an import with an unknown extension', async () => { | ||
| const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ | ||
| '--experimental-default-type=module', | ||
| fixtures.path('es-modules/package-type-module/imports-unknownext.mjs'), | ||
| ]); | ||
|
|
||
| match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/); | ||
| strictEqual(stdout, ''); | ||
| strictEqual(code, 1); | ||
| strictEqual(signal, null); | ||
| }); | ||
| }); | ||
|
|
||
| it('should affect CJS .js files', async () => { | ||
| const result = await spawnPromisified(process.execPath, [ | ||
| '--experimental-default-type=module', | ||
| fixtures.path('es-modules/package-type-commonjs/echo-require-cache.js'), | ||
| ]); | ||
|
|
||
| deepStrictEqual(result, { | ||
| code: 0, | ||
| stderr: '', | ||
| stdout: 'undefined\n', | ||
| signal: null, | ||
| }); | ||
| }); | ||
|
|
||
| it('should affect .cjs files that are imported', async () => { | ||
| const result = await spawnPromisified(process.execPath, [ | ||
| '--experimental-default-type=module', | ||
| '-e', | ||
| `import ${JSON.stringify(fixtures.fileURL('es-module-require-cache/echo.cjs'))}`, | ||
| ]); | ||
|
|
||
| deepStrictEqual(result, { | ||
| code: 0, | ||
| stderr: '', | ||
| stdout: 'undefined\n', | ||
| signal: null, | ||
| }); | ||
| }); | ||
|
|
||
| it('should not affect entry point .cjs files (with no hooks)', async () => { | ||
| const { stderr, stdout, code } = await spawnPromisified(process.execPath, [ | ||
| '--experimental-default-type=module', | ||
| fixtures.path('es-module-require-cache/echo.cjs'), | ||
| ]); | ||
|
|
||
| strictEqual(stderr, ''); | ||
| match(stdout, /^\[Object: null prototype\] \{(\n .+)+\n\}\n$/); | ||
| strictEqual(code, 0); | ||
| }); | ||
|
|
||
| it('should affect entry point .cjs files (when any hooks is registered)', async () => { | ||
| const result = await spawnPromisified(process.execPath, [ | ||
| '--experimental-default-type=module', | ||
| '--import', | ||
| 'data:text/javascript,import{register}from"node:module";register("data:text/javascript,");', | ||
| fixtures.path('es-module-require-cache/echo.cjs'), | ||
| ]); | ||
|
|
||
| deepStrictEqual(result, { | ||
| code: 0, | ||
| stderr: '', | ||
| stdout: 'undefined\n', | ||
| signal: null, | ||
| }); | ||
| }); | ||
|
|
||
| it('should not affect CJS from input-type', async () => { | ||
| const { stderr, stdout, code } = await spawnPromisified(process.execPath, [ | ||
| '--experimental-default-type=module', | ||
| '--input-type=commonjs', | ||
| '-p', | ||
| 'require.cache', | ||
| ]); | ||
|
|
||
| strictEqual(stderr, ''); | ||
| match(stdout, /^\[Object: null prototype\] \{\}\n$/); | ||
| strictEqual(code, 0); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| console.log(require.cache); |
1 change: 1 addition & 0 deletions
1
test/fixtures/es-modules/package-type-commonjs/echo-require-cache.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| console.log(require.cache); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CJS .js files . . . that are required?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that are required, imported, or used as entry point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth spelling that out, I was assuming “CJS .js files” meant .js files required by CommonJS modules, rather than what I now presume you meant, “.js file that is interpreted as CommonJS.”