Skip to content

Commit a3234b7

Browse files
committed
module: load source maps in commonjs translator
1 parent 16a5479 commit a3234b7

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/internal/modules/esm/translators.js

+2
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ function createCJSModuleWrap(url, source, isMain, loadCJS = loadCJSModule) {
289289
// In case the source was not provided by the `load` step, we need fetch it now.
290290
source = stringify(source ?? getSource(new URL(url)).source);
291291

292+
maybeCacheSourceMap(url, source);
293+
292294
const { exportNames, module } = cjsPreparseModuleExports(filename, source);
293295
cjsCache.set(url, module);
294296
const namesWithDefault = exportNames.has('default') ?

test/es-module/test-esm-loader-hooks.mjs

+24
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,30 @@ describe('Loader hooks', { concurrency: true }, () => {
747747
assert.strictEqual(signal, null);
748748
});
749749

750+
it('should support source maps in commonjs translator', async () => {
751+
const readFile = async () => {};
752+
const fileURLToPath = () => {};
753+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
754+
'--no-warnings',
755+
'--enable-source-maps',
756+
'--experimental-loader',
757+
`data:text/javascript,import{readFile}from"node:fs/promises";import{fileURLToPath}from"node:url";export ${
758+
async function load(u, c, n) {
759+
const r = await n(u, c);
760+
if (c.format === 'commonjs') {
761+
r.source = await readFile(new URL(u));
762+
}
763+
return r;
764+
}}`,
765+
fixtures.path('source-map/throw-on-require.js'),
766+
]);
767+
768+
assert.strictEqual(stdout, '');
769+
assert.match(stderr, /throw-on-require\.ts:9:9/);
770+
assert.strictEqual(code, 1);
771+
assert.strictEqual(signal, null);
772+
});
773+
750774
it('should handle mixed of opt-in modules and non-opt-in ones', async () => {
751775
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
752776
'--no-warnings',

0 commit comments

Comments
 (0)