Skip to content

Commit f9cd3c9

Browse files
GeoffreyBoothdygabo
authored andcommitted
add test that detection doesn't interfere with hooks
1 parent 7d2de9c commit f9cd3c9

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

test/es-module/test-esm-detect-ambiguous.mjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,25 @@ describe('--experimental-detect-module', { concurrency: !process.env.TEST_PARALL
400400
strictEqual(code, 0);
401401
strictEqual(signal, null);
402402
});
403+
404+
it('should detect the syntax of the source as returned by a custom load hook', async () => {
405+
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
406+
'--no-warnings',
407+
'--experimental-detect-module',
408+
'--import',
409+
`data:text/javascript,${encodeURIComponent(
410+
'import { register } from "node:module";' +
411+
'import { pathToFileURL } from "node:url";' +
412+
'register("./transpile-esm-to-cjs.mjs", pathToFileURL("./"));'
413+
)}`,
414+
fixtures.path('es-modules/package-without-type/esm-with-check.js'),
415+
], { cwd: fixtures.fileURL('es-module-loaders/') });
416+
417+
strictEqual(stderr, '');
418+
strictEqual(stdout, 'commonjs\ntransformed!\n');
419+
strictEqual(code, 0);
420+
strictEqual(signal, null);
421+
});
403422
});
404423
});
405424

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export async function load(url, context, next) {
2+
const output = await next(url, context);
3+
let source = `${output.source}`
4+
5+
// This is a very naive implementation for testing purposes only
6+
if (source?.includes('import')) {
7+
source = source.replace(/import\s+{\s*([^;]+)\s*}\s+from\s+(['"])(.*?)\2;/g, 'const { $1 } = require($2$3$2);')
8+
9+
source += '\nconsole.log("transformed!");'; // To verify the hook ran
10+
11+
output.source = source;
12+
output.format = 'commonjs';
13+
}
14+
15+
return output;
16+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { version } from 'node:process';
2+
console.log(this === undefined ? 'module' : 'commonjs');

0 commit comments

Comments
 (0)