Skip to content

Commit c93377f

Browse files
committed
add test for loader-provided source
1 parent bc7dcaa commit c93377f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Flags: --experimental-loader ./test/fixtures/es-module-loaders/preset-cjs-source.mjs
2+
import '../common/index.mjs';
3+
import * as fixtures from '../common/fixtures.mjs';
4+
import assert from 'assert';
5+
6+
const { default: existingFileSource } = await import(fixtures.fileURL('es-modules', 'cjs-file.cjs'));
7+
const { default: noSuchFileSource } = await import(fixtures.fileURL('no-such-file.cjs'));
8+
9+
assert.strictEqual(existingFileSource, 'no .cjs file was read to get this source');
10+
assert.strictEqual(noSuchFileSource, 'no .cjs file was read to get this source');
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export function resolve(specifier, context, next) {
2+
if (specifier.endsWith('/no-such-file.cjs')) {
3+
// Shortcut to avoid ERR_MODULE_NOT_FOUND for non-existing file, but keep the url for the load hook.
4+
return {
5+
shortCircuit: true,
6+
url: specifier,
7+
};
8+
}
9+
return next(specifier);
10+
}
11+
12+
export function load(href, context, next) {
13+
if (href.endsWith('.cjs')) {
14+
return {
15+
format: 'commonjs',
16+
shortCircuit: true,
17+
source: 'module.exports = "no .cjs file was read to get this source";',
18+
};
19+
}
20+
return next(href);
21+
}

0 commit comments

Comments
 (0)