Skip to content

Commit

Permalink
add test for loader-provided source
Browse files Browse the repository at this point in the history
  • Loading branch information
fasttime committed Nov 7, 2023
1 parent bc7dcaa commit c93377f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/es-module/test-esm-loader-with-source.mjs
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');
21 changes: 21 additions & 0 deletions test/fixtures/es-module-loaders/preset-cjs-source.mjs
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);
}

0 comments on commit c93377f

Please sign in to comment.