Skip to content

Commit aa2be4b

Browse files
privatenumberRafaelGSS
authored andcommitted
module: load source maps in commonjs translator
PR-URL: #51033 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 04eaa5c commit aa2be4b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

‎lib/internal/modules/esm/translators.js‎

Lines changed: 2 additions & 0 deletions
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(newURL(url)).source);
291291

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

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,39 @@ describe('Loader hooks',{concurrency: true }, () =>{
747747
assert.strictEqual(signal,null);
748748
});
749749

750+
it('should support source maps in commonjs translator',async()=>{
751+
constreadFile=async()=>{};
752+
consthook=`
753+
import{readFile } from 'node:fs/promises'
754+
export ${
755+
asyncfunctionload(url,context,nextLoad){
756+
constresolved=awaitnextLoad(url,context);
757+
if(context.format==='commonjs'){
758+
resolved.source=awaitreadFile(newURL(url));
759+
}
760+
returnresolved;
761+
}
762+
}`;
763+
764+
const{ code, signal, stdout, stderr }=awaitspawnPromisified(execPath,[
765+
'--no-warnings',
766+
'--enable-source-maps',
767+
'--import',
768+
`data:text/javascript,${encodeURIComponent(`
769+
import{register } from "node:module"
770+
register(${
771+
JSON.stringify('data:text/javascript,'+encodeURIComponent(hook))
772+
});
773+
`)}`,
774+
fixtures.path('source-map/throw-on-require.js'),
775+
]);
776+
777+
assert.strictEqual(stdout,'');
778+
assert.match(stderr,/throw-on-require\.ts:9:9/);
779+
assert.strictEqual(code,1);
780+
assert.strictEqual(signal,null);
781+
});
782+
750783
it('should handle mixed of opt-in modules and non-opt-in ones',async()=>{
751784
const{ code, signal, stdout, stderr }=awaitspawnPromisified(execPath,[
752785
'--no-warnings',

0 commit comments

Comments
(0)