Skip to content

Commit

Permalink
add test that detection doesn't interfere with hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyBooth authored and dygabo committed Jun 24, 2024
1 parent a350159 commit 7964fe8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
19 changes: 19 additions & 0 deletions test/es-module/test-esm-detect-ambiguous.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,25 @@ describe('--experimental-detect-module', { concurrency: !process.env.TEST_PARALL
strictEqual(code, 0);
strictEqual(signal, null);
});

it('should detect the syntax of the source as returned by a custom load hook', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-detect-module',
'--import',
`data:text/javascript,${encodeURIComponent(
'import { register } from "node:module";' +
'import { pathToFileURL } from "node:url";' +
'register("./transpile-esm-to-cjs.mjs", pathToFileURL("./"));'
)}`,
fixtures.path('es-modules/package-without-type/esm-with-check.js'),
], { cwd: fixtures.fileURL('es-module-loaders/') });

strictEqual(stderr, '');
strictEqual(stdout, 'commonjs\ntransformed!\n');
strictEqual(code, 0);
strictEqual(signal, null);
});
});
});

Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/es-module-loaders/transpile-esm-to-cjs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export async function load(url, context, next) {
const output = await next(url, context);
let source = `${output.source}`

// This is a very naive implementation for testing purposes only
if (source?.includes('import')) {
source = source.replace(/import\s+{\s*([^;]+)\s*}\s+from\s+(['"])(.*?)\2;/g, 'const { $1 } = require($2$3$2);')

source += '\nconsole.log("transformed!");'; // To verify the hook ran

output.source = source;
output.format = 'commonjs';
}

return output;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { version } from 'node:process';
console.log(this === undefined ? 'module' : 'commonjs');

0 comments on commit 7964fe8

Please sign in to comment.