-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 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
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(fixtures.fileURL('no-such-file.cjs')); | ||
|
||
assert.strictEqual(existingFileSource, 'no .cjs file was read to get this source'); | ||
assert.strictEqual(noSuchFileSource, 'no .cjs file was read to get this source'); |
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); | ||
} |