-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
esm: do not call
getSource
when format is commonjs
Ensure that `defaultLoad` does not uselessly access the file system to get the source of modules that are known to be in CommonJS format. This allows CommonJS imports to resolve in the current phase of the event loop. Refs: eslint/eslint#17683 PR-URL: #50465 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
- Loading branch information
1 parent
844f056
commit 7c386fa
Showing
6 changed files
with
69 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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,19 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
const assert = require('node:assert'); | ||
|
||
(async () => { | ||
|
||
// Make sure that the CommonJS module lexer has been initialized. | ||
// See https://github.com/nodejs/node/blob/v21.1.0/lib/internal/modules/esm/translators.js#L61-L81. | ||
await import(fixtures.fileURL('empty.js')); | ||
|
||
let tickDuringCJSImport = false; | ||
process.nextTick(() => { tickDuringCJSImport = true; }); | ||
await import(fixtures.fileURL('empty.cjs')); | ||
|
||
assert(!tickDuringCJSImport); | ||
|
||
})().then(common.mustCall()); |
This file contains 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,9 @@ | ||
import '../common/index.mjs'; | ||
import fixtures from '../common/fixtures.js'; | ||
import assert from 'node:assert'; | ||
|
||
let tickDuringCJSImport = false; | ||
process.nextTick(() => { tickDuringCJSImport = true; }); | ||
await import(fixtures.fileURL('empty.cjs')); | ||
|
||
assert(!tickDuringCJSImport); |
This file contains 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,10 @@ | ||
// Flags: --experimental-loader ./test/fixtures/es-module-loaders/preset-cjs-source.mjs | ||
import '../common/index.mjs'; | ||
import * as fixtures from '../common/fixtures.mjs'; | ||
import assert from 'assert'; | ||
|
||
const { default: existingFileSource } = await import(fixtures.fileURL('es-modules', 'cjs-file.cjs')); | ||
const { default: noSuchFileSource } = await import(new URL('./no-such-file.cjs', import.meta.url)); | ||
|
||
assert.strictEqual(existingFileSource, 'no .cjs file was read to get this source'); | ||
assert.strictEqual(noSuchFileSource, 'no .cjs file was read to get this source'); |
Empty file.
This file contains 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,21 @@ | ||
export function resolve(specifier, context, next) { | ||
if (specifier.endsWith('/no-such-file.cjs')) { | ||
// Shortcut to avoid ERR_MODULE_NOT_FOUND for non-existing file, but keep the url for the load hook. | ||
return { | ||
shortCircuit: true, | ||
url: specifier, | ||
}; | ||
} | ||
return next(specifier); | ||
} | ||
|
||
export function load(href, context, next) { | ||
if (href.endsWith('.cjs')) { | ||
return { | ||
format: 'commonjs', | ||
shortCircuit: true, | ||
source: 'module.exports = "no .cjs file was read to get this source";', | ||
}; | ||
} | ||
return next(href); | ||
} |