GeezerVison is showing github.com — esm: bypass CJS loader in default load under `--default-type=module` · nodejs/node@b48cf31
File treeExpand file treeCollapse file tree Open diff view settings es-modules/package-type-commonjs
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/module.md +2-1Lines changed: 2 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -622,7 +622,8 @@ Omitting vs providing a `source` for `'commonjs'` has very different effects: | 622 | 622 | registered hooks. This behavior for nullish `source` is temporary — in the | 623 | 623 | future, nullish `source` will not be supported. | 624 | 624 |
| 625 | | -The Node.js internal `load` implementation, which is the value of `next` for the | | 625 | +When `node` is run with `--experimental-default-type=commonjs`, the Node.js | | 626 | +internal `load` implementation, which is the value of `next` for the | 626 | 627 | last hook in the `load` chain, returns `null` for `source` when `format` is | 627 | 628 | `'commonjs'` for backward compatibility. Here is an example hook that would | 628 | 629 | opt-in to using the non-default behavior: | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/load.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,6 +18,8 @@ const policy = getOptionValue('--experimental-policy') ? | 18 | 18 | null; | 19 | 19 | const experimentalNetworkImports = | 20 | 20 | getOptionValue('--experimental-network-imports'); | | 21 | +const defaultType = | | 22 | + getOptionValue('--experimental-default-type'); | 21 | 23 |
| 22 | 24 | const { Buffer: { from: BufferFrom } } = require('buffer'); | 23 | 25 |
| @@ -140,7 +142,7 @@ async function defaultLoad(url, context = kEmptyObject) { | 140 | 142 | // Now that we have the source for the module, run `defaultGetFormat` again in case we detect ESM syntax. | 141 | 143 | format ??= await defaultGetFormat(urlInstance, contextToPass); | 142 | 144 |
| 143 | | - if (format === 'commonjs' && contextToPass !== context) { | | 145 | + if (format === 'commonjs' && contextToPass !== context && defaultType !== 'module') { | 144 | 146 | // For backward compatibility reasons, we need to discard the source in | 145 | 147 | // order for the CJS loader to re-fetch it. | 146 | 148 | source = null; | |
Collapse file Copy file name to clipboard +98-28Lines changed: 98 additions & 28 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { spawnPromisified } from '../common/index.mjs'; | 2 | 2 | import * as fixtures from '../common/fixtures.mjs'; | 3 | 3 | import { describe, it } from 'node:test'; | 4 | | -import { match, strictEqual } from 'node:assert'; | 5 | | - | 6 | | -describe('--experimental-default-type=module should not affect the interpretation of files with unknown extensions', | 7 | | - { concurrency: true }, () => { | 8 | | - it('should error on an entry point with an unknown extension', async () => { | 9 | | - const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ | 10 | | - '--experimental-default-type=module', | 11 | | - fixtures.path('es-modules/package-type-module/extension.unknown'), | 12 | | - ]); | 13 | | - | 14 | | - match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/); | 15 | | - strictEqual(stdout, ''); | 16 | | - strictEqual(code, 1); | 17 | | - strictEqual(signal, null); | 18 | | - }); | 19 | | - | 20 | | - it('should error on an import with an unknown extension', async () => { | 21 | | - const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ | 22 | | - '--experimental-default-type=module', | 23 | | - fixtures.path('es-modules/package-type-module/imports-unknownext.mjs'), | 24 | | - ]); | 25 | | - | 26 | | - match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/); | 27 | | - strictEqual(stdout, ''); | 28 | | - strictEqual(code, 1); | 29 | | - strictEqual(signal, null); | 30 | | - }); | 31 | | - }); | | 4 | +import { deepStrictEqual, match, strictEqual } from 'node:assert'; | | 5 | + | | 6 | +describe('--experimental-default-type=module', { concurrency: true }, () => { | | 7 | + describe('should not affect the interpretation of files with unknown extensions', { concurrency: true }, () => { | | 8 | + it('should error on an entry point with an unknown extension', async () => { | | 9 | + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ | | 10 | + '--experimental-default-type=module', | | 11 | + fixtures.path('es-modules/package-type-module/extension.unknown'), | | 12 | + ]); | | 13 | + | | 14 | + match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/); | | 15 | + strictEqual(stdout, ''); | | 16 | + strictEqual(code, 1); | | 17 | + strictEqual(signal, null); | | 18 | + }); | | 19 | + | | 20 | + it('should error on an import with an unknown extension', async () => { | | 21 | + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ | | 22 | + '--experimental-default-type=module', | | 23 | + fixtures.path('es-modules/package-type-module/imports-unknownext.mjs'), | | 24 | + ]); | | 25 | + | | 26 | + match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/); | | 27 | + strictEqual(stdout, ''); | | 28 | + strictEqual(code, 1); | | 29 | + strictEqual(signal, null); | | 30 | + }); | | 31 | + }); | | 32 | + | | 33 | + it('should affect CJS .js files (imported, required, entry points)', async () => { | | 34 | + const result = await spawnPromisified(process.execPath, [ | | 35 | + '--experimental-default-type=module', | | 36 | + fixtures.path('es-modules/package-type-commonjs/echo-require-cache.js'), | | 37 | + ]); | | 38 | + | | 39 | + deepStrictEqual(result, { | | 40 | + code: 0, | | 41 | + stderr: '', | | 42 | + stdout: 'undefined\n', | | 43 | + signal: null, | | 44 | + }); | | 45 | + }); | | 46 | + | | 47 | + it('should affect .cjs files that are imported', async () => { | | 48 | + const result = await spawnPromisified(process.execPath, [ | | 49 | + '--experimental-default-type=module', | | 50 | + '-e', | | 51 | + `import ${JSON.stringify(fixtures.fileURL('es-module-require-cache/echo.cjs'))}`, | | 52 | + ]); | | 53 | + | | 54 | + deepStrictEqual(result, { | | 55 | + code: 0, | | 56 | + stderr: '', | | 57 | + stdout: 'undefined\n', | | 58 | + signal: null, | | 59 | + }); | | 60 | + }); | | 61 | + | | 62 | + it('should affect entry point .cjs files (with no hooks)', async () => { | | 63 | + const { stderr, stdout, code } = await spawnPromisified(process.execPath, [ | | 64 | + '--experimental-default-type=module', | | 65 | + fixtures.path('es-module-require-cache/echo.cjs'), | | 66 | + ]); | | 67 | + | | 68 | + strictEqual(stderr, ''); | | 69 | + match(stdout, /^undefined\n$/); | | 70 | + strictEqual(code, 0); | | 71 | + }); | | 72 | + | | 73 | + it('should affect entry point .cjs files (when any hooks is registered)', async () => { | | 74 | + const result = await spawnPromisified(process.execPath, [ | | 75 | + '--experimental-default-type=module', | | 76 | + '--import', | | 77 | + 'data:text/javascript,import{register}from"node:module";register("data:text/javascript,");', | | 78 | + fixtures.path('es-module-require-cache/echo.cjs'), | | 79 | + ]); | | 80 | + | | 81 | + deepStrictEqual(result, { | | 82 | + code: 0, | | 83 | + stderr: '', | | 84 | + stdout: 'undefined\n', | | 85 | + signal: null, | | 86 | + }); | | 87 | + }); | | 88 | + | | 89 | + it('should not affect CJS from input-type', async () => { | | 90 | + const { stderr, stdout, code } = await spawnPromisified(process.execPath, [ | | 91 | + '--experimental-default-type=module', | | 92 | + '--input-type=commonjs', | | 93 | + '-p', | | 94 | + 'require.cache', | | 95 | + ]); | | 96 | + | | 97 | + strictEqual(stderr, ''); | | 98 | + match(stdout, /^\[Object: null prototype\] \{\}\n$/); | | 99 | + strictEqual(code, 0); | | 100 | + }); | | 101 | +}); |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +console.log(require.cache); |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +console.log(require.cache); |
|