Skip to content

Commit

Permalink
module: load source maps in commonjs translator
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Dec 3, 2023
1 parent 16a5479 commit ba29aac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ function createCJSModuleWrap(url, source, isMain, loadCJS = loadCJSModule) {
// In case the source was not provided by the `load` step, we need fetch it now.
source = stringify(source ?? getSource(new URL(url)).source);

maybeCacheSourceMap(url, source);

const { exportNames, module } = cjsPreparseModuleExports(filename, source);
cjsCache.set(url, module);
const namesWithDefault = exportNames.has('default') ?
Expand Down
27 changes: 27 additions & 0 deletions test/es-module/test-esm-loader-hooks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,33 @@ describe('Loader hooks', { concurrency: true }, () => {
assert.strictEqual(signal, null);
});

it('should support source maps in commonjs translator', async () => {
const readFile = async () => {};
const fileURLToPath = () => {};
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--enable-source-maps',
'--experimental-loader',
`data:text/javascript,import{readFile}from"node:fs/promises";import{fileURLToPath}from"node:url";export ${
async function load(u, c, n) {
const r = await n(u, c);
if (u.endsWith('/common/index.js')) {
r.source = '"use strict";module.exports=require("node:module").createRequire(' +
`${JSON.stringify(u)})(${JSON.stringify(fileURLToPath(u))});\n`;
} else if (c.format === 'commonjs') {
r.source = await readFile(new URL(u));
}
return r;
}}`,
fixtures.path('source-map/throw-on-require.js'),
]);

assert.strictEqual(stdout, '');
assert.match(stderr, /throw-on-require\.ts:9:9/);
assert.strictEqual(code, 1);
assert.strictEqual(signal, null);
});

it('should handle mixed of opt-in modules and non-opt-in ones', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
Expand Down

0 comments on commit ba29aac

Please sign in to comment.