From 82c549e5333a9d05591a5ec03c6267c0166720c4 Mon Sep 17 00:00:00 2001 From: unbyte Date: Tue, 6 Feb 2024 23:44:59 +0800 Subject: [PATCH 1/7] test: add tests for map url extract behavior --- test/es-module/test-esm-source-map.mjs | 65 +++++++++++++++++++ .../extract-url/cjs-url-in-middle.js | 5 ++ .../extract-url/cjs-url-in-string.js | 5 ++ .../extract-url/esm-url-in-middle.mjs | 5 ++ .../extract-url/esm-url-in-string.mjs | 5 ++ 5 files changed, 85 insertions(+) create mode 100644 test/es-module/test-esm-source-map.mjs create mode 100644 test/fixtures/source-map/extract-url/cjs-url-in-middle.js create mode 100644 test/fixtures/source-map/extract-url/cjs-url-in-string.js create mode 100644 test/fixtures/source-map/extract-url/esm-url-in-middle.mjs create mode 100644 test/fixtures/source-map/extract-url/esm-url-in-string.mjs diff --git a/test/es-module/test-esm-source-map.mjs b/test/es-module/test-esm-source-map.mjs new file mode 100644 index 00000000000000..5136c145ff140c --- /dev/null +++ b/test/es-module/test-esm-source-map.mjs @@ -0,0 +1,65 @@ +import { spawnPromisified } from '../common/index.mjs'; +import * as fixtures from '../common/fixtures.mjs'; +import assert from 'node:assert'; +import { execPath } from 'node:process'; +import { describe, it } from 'node:test'; + +describe('esm source-map', { concurrency: true }, () => { + // Issue: https://github.com/nodejs/node/issues/51522 + + it('should extract source map url in middle from esm', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ + '--no-warnings', + '--enable-source-maps', + fixtures.path('source-map/extract-url/esm-url-in-middle.mjs'), + ]); + + assert.strictEqual(stdout, ''); + assert.match(stderr, /index\.ts/); + assert.strictEqual(code, 1); + assert.strictEqual(signal, null); + }); + + it('should extract source map url in middle from cjs imported by esm', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ + '--no-warnings', + '--enable-source-maps', + '--input-type=module', + '--eval', + `import ${JSON.stringify(fixtures.fileURL('source-map/extract-url/cjs-url-in-middle.js'))}` + ]); + + assert.strictEqual(stdout, ''); + assert.match(stderr, /index\.ts/); + assert.strictEqual(code, 1); + assert.strictEqual(signal, null); + }); + + it('should not extract source map url inside string from esm', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ + '--no-warnings', + '--enable-source-maps', + fixtures.path('source-map/extract-url/esm-url-in-string.mjs'), + ]); + + assert.strictEqual(stdout, ''); + assert.doesNotMatch(stderr, /index\.ts/); + assert.strictEqual(code, 1); + assert.strictEqual(signal, null); + }); + + it('should not extract source map url inside string from cjs imported by esm', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ + '--no-warnings', + '--enable-source-maps', + '--input-type=module', + '--eval', + `import ${JSON.stringify(fixtures.fileURL('source-map/extract-url/cjs-url-in-string.js'))}` + ]); + + assert.strictEqual(stdout, ''); + assert.doesNotMatch(stderr, /index\.ts/); + assert.strictEqual(code, 1); + assert.strictEqual(signal, null); + }); +}); \ No newline at end of file diff --git a/test/fixtures/source-map/extract-url/cjs-url-in-middle.js b/test/fixtures/source-map/extract-url/cjs-url-in-middle.js new file mode 100644 index 00000000000000..da99cb47fb25d6 --- /dev/null +++ b/test/fixtures/source-map/extract-url/cjs-url-in-middle.js @@ -0,0 +1,5 @@ + +throw new Error("Hello world!"); +//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vcHJvamVjdC9pbmRleC50cyJdLAogICJzb3VyY2VzQ29udGVudCI6IFsidGhyb3cgbmV3IEVycm9yKFwiSGVsbG8gd29ybGQhXCIpXG4iXSwKICAibWFwcGluZ3MiOiAiO0FBQUEsTUFBTSxJQUFJLE1BQU0sY0FBYzsiLAogICJuYW1lcyI6IFtdCn0K +console.log(1); +// \ No newline at end of file diff --git a/test/fixtures/source-map/extract-url/cjs-url-in-string.js b/test/fixtures/source-map/extract-url/cjs-url-in-string.js new file mode 100644 index 00000000000000..742f81c61dbcc3 --- /dev/null +++ b/test/fixtures/source-map/extract-url/cjs-url-in-string.js @@ -0,0 +1,5 @@ + +throw new Error("Hello world!");` +//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vcHJvamVjdC9pbmRleC50cyJdLAogICJzb3VyY2VzQ29udGVudCI6IFsidGhyb3cgbmV3IEVycm9yKFwiSGVsbG8gd29ybGQhXCIpXG4iXSwKICAibWFwcGluZ3MiOiAiO0FBQUEsTUFBTSxJQUFJLE1BQU0sY0FBYzsiLAogICJuYW1lcyI6IFtdCn0K +console.log(1); +//` \ No newline at end of file diff --git a/test/fixtures/source-map/extract-url/esm-url-in-middle.mjs b/test/fixtures/source-map/extract-url/esm-url-in-middle.mjs new file mode 100644 index 00000000000000..da99cb47fb25d6 --- /dev/null +++ b/test/fixtures/source-map/extract-url/esm-url-in-middle.mjs @@ -0,0 +1,5 @@ + +throw new Error("Hello world!"); +//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vcHJvamVjdC9pbmRleC50cyJdLAogICJzb3VyY2VzQ29udGVudCI6IFsidGhyb3cgbmV3IEVycm9yKFwiSGVsbG8gd29ybGQhXCIpXG4iXSwKICAibWFwcGluZ3MiOiAiO0FBQUEsTUFBTSxJQUFJLE1BQU0sY0FBYzsiLAogICJuYW1lcyI6IFtdCn0K +console.log(1); +// \ No newline at end of file diff --git a/test/fixtures/source-map/extract-url/esm-url-in-string.mjs b/test/fixtures/source-map/extract-url/esm-url-in-string.mjs new file mode 100644 index 00000000000000..742f81c61dbcc3 --- /dev/null +++ b/test/fixtures/source-map/extract-url/esm-url-in-string.mjs @@ -0,0 +1,5 @@ + +throw new Error("Hello world!");` +//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vcHJvamVjdC9pbmRleC50cyJdLAogICJzb3VyY2VzQ29udGVudCI6IFsidGhyb3cgbmV3IEVycm9yKFwiSGVsbG8gd29ybGQhXCIpXG4iXSwKICAibWFwcGluZ3MiOiAiO0FBQUEsTUFBTSxJQUFJLE1BQU0sY0FBYzsiLAogICJuYW1lcyI6IFtdCn0K +console.log(1); +//` \ No newline at end of file From b202149dd1dfbdea054f84fa4661b89c0570c219 Mon Sep 17 00:00:00 2001 From: unbyte Date: Wed, 7 Feb 2024 01:19:12 +0800 Subject: [PATCH 2/7] feat: get source mapping url from v8 --- lib/internal/modules/esm/translators.js | 19 ++- lib/internal/source_map/source_map_cache.js | 19 --- src/module_wrap.cc | 7 ++ test/es-module/test-esm-source-map.mjs | 131 ++++++++++++-------- 4 files changed, 101 insertions(+), 75 deletions(-) diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 1268e6adec9435..05d3494ad60fcc 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -164,9 +164,12 @@ async function importModuleDynamically(specifier, { url }, attributes) { translators.set('module', async function moduleStrategy(url, source, isMain) { assertBufferSource(source, true, 'load'); source = stringify(source); - maybeCacheSourceMap(url, source); debug(`Translating StandardModule ${url}`); const module = new ModuleWrap(url, undefined, source, 0, 0); + // Cache the source map for the module if present. + if (module.sourceMapURL) { + maybeCacheSourceMap(url, source, null, false, url, module.sourceMapURL); + } const { registerModule } = require('internal/modules/esm/utils'); registerModule(module, { __proto__: null, @@ -206,11 +209,11 @@ function enrichCJSError(err, content, filename) { * @param {string} filename - The filename of the module. */ function loadCJSModule(module, source, url, filename) { - let compiledWrapper; + let compileResult; const hostDefinedOptionId = vm_dynamic_import_default_internal; const importModuleDynamically = vm_dynamic_import_default_internal; try { - compiledWrapper = internalCompileFunction( + compileResult = internalCompileFunction( source, // code, filename, // filename 0, // lineOffset @@ -228,11 +231,17 @@ function loadCJSModule(module, source, url, filename) { ], hostDefinedOptionId, // hostDefinedOptionsId importModuleDynamically, // importModuleDynamically - ).function; + ); } catch (err) { enrichCJSError(err, source, filename); throw err; } + // Cache the source map for the cjs module if present. + if (compileResult.sourceMapURL) { + maybeCacheSourceMap(url, source, null, false, url, compileResult.sourceMapURL); + } + + const compiledWrapper = compileResult.function; const __dirname = dirname(filename); // eslint-disable-next-line func-name-matching,func-style @@ -290,8 +299,6 @@ 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') ? diff --git a/lib/internal/source_map/source_map_cache.js b/lib/internal/source_map/source_map_cache.js index fb411487336c29..5d243411f9bb76 100644 --- a/lib/internal/source_map/source_map_cache.js +++ b/lib/internal/source_map/source_map_cache.js @@ -38,7 +38,6 @@ const esmSourceMapCache = new SafeMap(); // The generated sources is not mutable, so we can use a Map without memory concerns: const generatedSourceMapCache = new SafeMap(); const kLeadingProtocol = /^\w+:\/\//; -const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=(?[^\s]+)/g; const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=(?[^\s]+)/g; const { isAbsolute } = require('path'); @@ -90,20 +89,6 @@ function extractSourceURLMagicComment(content) { return sourceURL; } -function extractSourceMapURLMagicComment(content) { - let match; - let lastMatch; - // A while loop is used here to get the last occurrence of sourceMappingURL. - // This is needed so that we don't match sourceMappingURL in string literals. - while ((match = RegExpPrototypeExec(kSourceMappingURLMagicComment, content))) { - lastMatch = match; - } - if (lastMatch == null) { - return null; - } - return lastMatch.groups.sourceMappingURL; -} - function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSource, sourceURL, sourceMapURL) { const sourceMapsEnabled = getSourceMapsEnabled(); if (!(process.env.NODE_V8_COVERAGE || sourceMapsEnabled)) return; @@ -114,10 +99,6 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo return; } - if (sourceMapURL === undefined) { - sourceMapURL = extractSourceMapURLMagicComment(content); - } - // Bail out when there is no source map url. if (typeof sourceMapURL !== 'string') { return; diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 6f5074ddeb9f19..58ebe7b837af5f 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -226,6 +226,13 @@ void ModuleWrap::New(const FunctionCallbackInfo& args) { try_catch.ReThrow(); return; } + + if (that->Set(context, + realm->env()->source_map_url_string(), + module->GetUnboundModuleScript()->GetSourceMappingURL()) + .IsNothing()) { + return; + } } } diff --git a/test/es-module/test-esm-source-map.mjs b/test/es-module/test-esm-source-map.mjs index 5136c145ff140c..b3a41251768675 100644 --- a/test/es-module/test-esm-source-map.mjs +++ b/test/es-module/test-esm-source-map.mjs @@ -7,59 +7,90 @@ import { describe, it } from 'node:test'; describe('esm source-map', { concurrency: true }, () => { // Issue: https://github.com/nodejs/node/issues/51522 - it('should extract source map url in middle from esm', async () => { - const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ - '--no-warnings', - '--enable-source-maps', - fixtures.path('source-map/extract-url/esm-url-in-middle.mjs'), - ]); + [ + [ + 'in middle from esm', + 'source-map/extract-url/esm-url-in-middle.mjs', + true, + ], + [ + 'inside string from esm', + 'source-map/extract-url/esm-url-in-string.mjs', + false, + ], + ].forEach(([name, path, shouldExtract]) => { + it((shouldExtract ? 'should extract source map url' : 'should not extract source map url') + name, async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ + '--no-warnings', + '--enable-source-maps', + fixtures.path(path), + ]); - assert.strictEqual(stdout, ''); - assert.match(stderr, /index\.ts/); - assert.strictEqual(code, 1); - assert.strictEqual(signal, null); + assert.strictEqual(stdout, ''); + if (shouldExtract) { + assert.match(stderr, /index\.ts/); + } else { + assert.doesNotMatch(stderr, /index\.ts/); + } + assert.strictEqual(code, 1); + assert.strictEqual(signal, null); + }); }); - it('should extract source map url in middle from cjs imported by esm', async () => { - const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ - '--no-warnings', - '--enable-source-maps', - '--input-type=module', - '--eval', - `import ${JSON.stringify(fixtures.fileURL('source-map/extract-url/cjs-url-in-middle.js'))}` - ]); + [ + [ + 'in middle from esm imported by esm', + `import ${JSON.stringify(fixtures.fileURL('source-map/extract-url/esm-url-in-middle.mjs'))}`, + true, + ], + [ + 'in middle from cjs imported by esm', + `import ${JSON.stringify(fixtures.fileURL('source-map/extract-url/cjs-url-in-middle.js'))}`, + true, + ], + [ + 'in middle from cjs required by esm', + "import { createRequire } from 'module';" + + 'const require = createRequire(import.meta.url);' + + `require(${JSON.stringify(fixtures.path('source-map/extract-url/cjs-url-in-middle.js'))})`, + true, + ], - assert.strictEqual(stdout, ''); - assert.match(stderr, /index\.ts/); - assert.strictEqual(code, 1); - assert.strictEqual(signal, null); - }); - - it('should not extract source map url inside string from esm', async () => { - const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ - '--no-warnings', - '--enable-source-maps', - fixtures.path('source-map/extract-url/esm-url-in-string.mjs'), - ]); - - assert.strictEqual(stdout, ''); - assert.doesNotMatch(stderr, /index\.ts/); - assert.strictEqual(code, 1); - assert.strictEqual(signal, null); - }); - - it('should not extract source map url inside string from cjs imported by esm', async () => { - const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ - '--no-warnings', - '--enable-source-maps', - '--input-type=module', - '--eval', - `import ${JSON.stringify(fixtures.fileURL('source-map/extract-url/cjs-url-in-string.js'))}` - ]); + [ + 'inside string from esm imported by esm', + `import ${JSON.stringify(fixtures.fileURL('source-map/extract-url/esm-url-in-string.mjs'))}`, + false, + ], + [ + 'inside string from cjs imported by esm', + `import ${JSON.stringify(fixtures.fileURL('source-map/extract-url/cjs-url-in-string.js'))}`, + false, + ], + [ + 'inside string from cjs required by esm', + "import { createRequire } from 'module';" + + 'const require = createRequire(import.meta.url);' + + `require(${JSON.stringify(fixtures.path('source-map/extract-url/cjs-url-in-string.js'))})`, + false, + ], + ].forEach(([name, evalCode, shouldExtract]) => { + it((shouldExtract ? 'should extract source map url' : 'should not extract source map url') + name, async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ + '--no-warnings', + '--enable-source-maps', + '--input-type=module', + '--eval', + evalCode, + ]); - assert.strictEqual(stdout, ''); - assert.doesNotMatch(stderr, /index\.ts/); - assert.strictEqual(code, 1); - assert.strictEqual(signal, null); + assert.strictEqual(stdout, ''); + if (shouldExtract) { + assert.match(stderr, /index\.ts/); + } else { + assert.doesNotMatch(stderr, /index\.ts/); + } + assert.strictEqual(code, 1); + assert.strictEqual(signal, null); + }); }); -}); \ No newline at end of file +}); From 6a5ada065696f7743b5e35d7885cbbde9e007cb6 Mon Sep 17 00:00:00 2001 From: unbyte Date: Wed, 7 Feb 2024 01:37:33 +0800 Subject: [PATCH 3/7] fix: add back extract source map url using regex --- lib/internal/source_map/source_map_cache.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/internal/source_map/source_map_cache.js b/lib/internal/source_map/source_map_cache.js index 5d243411f9bb76..fb411487336c29 100644 --- a/lib/internal/source_map/source_map_cache.js +++ b/lib/internal/source_map/source_map_cache.js @@ -38,6 +38,7 @@ const esmSourceMapCache = new SafeMap(); // The generated sources is not mutable, so we can use a Map without memory concerns: const generatedSourceMapCache = new SafeMap(); const kLeadingProtocol = /^\w+:\/\//; +const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=(?[^\s]+)/g; const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=(?[^\s]+)/g; const { isAbsolute } = require('path'); @@ -89,6 +90,20 @@ function extractSourceURLMagicComment(content) { return sourceURL; } +function extractSourceMapURLMagicComment(content) { + let match; + let lastMatch; + // A while loop is used here to get the last occurrence of sourceMappingURL. + // This is needed so that we don't match sourceMappingURL in string literals. + while ((match = RegExpPrototypeExec(kSourceMappingURLMagicComment, content))) { + lastMatch = match; + } + if (lastMatch == null) { + return null; + } + return lastMatch.groups.sourceMappingURL; +} + function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSource, sourceURL, sourceMapURL) { const sourceMapsEnabled = getSourceMapsEnabled(); if (!(process.env.NODE_V8_COVERAGE || sourceMapsEnabled)) return; @@ -99,6 +114,10 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo return; } + if (sourceMapURL === undefined) { + sourceMapURL = extractSourceMapURLMagicComment(content); + } + // Bail out when there is no source map url. if (typeof sourceMapURL !== 'string') { return; From 2acc9491874368c044d48e61ceb23aaf74065a89 Mon Sep 17 00:00:00 2001 From: unbyte Date: Wed, 7 Feb 2024 21:49:44 +0800 Subject: [PATCH 4/7] test: add trailing newlines --- test/fixtures/source-map/extract-url/cjs-url-in-middle.js | 2 +- test/fixtures/source-map/extract-url/cjs-url-in-string.js | 2 +- test/fixtures/source-map/extract-url/esm-url-in-middle.mjs | 2 +- test/fixtures/source-map/extract-url/esm-url-in-string.mjs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/fixtures/source-map/extract-url/cjs-url-in-middle.js b/test/fixtures/source-map/extract-url/cjs-url-in-middle.js index da99cb47fb25d6..62f1ce898c8307 100644 --- a/test/fixtures/source-map/extract-url/cjs-url-in-middle.js +++ b/test/fixtures/source-map/extract-url/cjs-url-in-middle.js @@ -2,4 +2,4 @@ throw new Error("Hello world!"); //# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vcHJvamVjdC9pbmRleC50cyJdLAogICJzb3VyY2VzQ29udGVudCI6IFsidGhyb3cgbmV3IEVycm9yKFwiSGVsbG8gd29ybGQhXCIpXG4iXSwKICAibWFwcGluZ3MiOiAiO0FBQUEsTUFBTSxJQUFJLE1BQU0sY0FBYzsiLAogICJuYW1lcyI6IFtdCn0K console.log(1); -// \ No newline at end of file +// diff --git a/test/fixtures/source-map/extract-url/cjs-url-in-string.js b/test/fixtures/source-map/extract-url/cjs-url-in-string.js index 742f81c61dbcc3..fcbe35e8788383 100644 --- a/test/fixtures/source-map/extract-url/cjs-url-in-string.js +++ b/test/fixtures/source-map/extract-url/cjs-url-in-string.js @@ -2,4 +2,4 @@ throw new Error("Hello world!");` //# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vcHJvamVjdC9pbmRleC50cyJdLAogICJzb3VyY2VzQ29udGVudCI6IFsidGhyb3cgbmV3IEVycm9yKFwiSGVsbG8gd29ybGQhXCIpXG4iXSwKICAibWFwcGluZ3MiOiAiO0FBQUEsTUFBTSxJQUFJLE1BQU0sY0FBYzsiLAogICJuYW1lcyI6IFtdCn0K console.log(1); -//` \ No newline at end of file +//` diff --git a/test/fixtures/source-map/extract-url/esm-url-in-middle.mjs b/test/fixtures/source-map/extract-url/esm-url-in-middle.mjs index da99cb47fb25d6..62f1ce898c8307 100644 --- a/test/fixtures/source-map/extract-url/esm-url-in-middle.mjs +++ b/test/fixtures/source-map/extract-url/esm-url-in-middle.mjs @@ -2,4 +2,4 @@ throw new Error("Hello world!"); //# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vcHJvamVjdC9pbmRleC50cyJdLAogICJzb3VyY2VzQ29udGVudCI6IFsidGhyb3cgbmV3IEVycm9yKFwiSGVsbG8gd29ybGQhXCIpXG4iXSwKICAibWFwcGluZ3MiOiAiO0FBQUEsTUFBTSxJQUFJLE1BQU0sY0FBYzsiLAogICJuYW1lcyI6IFtdCn0K console.log(1); -// \ No newline at end of file +// diff --git a/test/fixtures/source-map/extract-url/esm-url-in-string.mjs b/test/fixtures/source-map/extract-url/esm-url-in-string.mjs index 742f81c61dbcc3..fcbe35e8788383 100644 --- a/test/fixtures/source-map/extract-url/esm-url-in-string.mjs +++ b/test/fixtures/source-map/extract-url/esm-url-in-string.mjs @@ -2,4 +2,4 @@ throw new Error("Hello world!");` //# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vcHJvamVjdC9pbmRleC50cyJdLAogICJzb3VyY2VzQ29udGVudCI6IFsidGhyb3cgbmV3IEVycm9yKFwiSGVsbG8gd29ybGQhXCIpXG4iXSwKICAibWFwcGluZ3MiOiAiO0FBQUEsTUFBTSxJQUFJLE1BQU0sY0FBYzsiLAogICJuYW1lcyI6IFtdCn0K console.log(1); -//` \ No newline at end of file +//` From 97b5556224a3bdae77515d465bfcfeadfff8a5ab Mon Sep 17 00:00:00 2001 From: unbyte Date: Thu, 8 Feb 2024 00:53:15 +0800 Subject: [PATCH 5/7] fix: extract sourceURL from esm code text --- lib/internal/modules/esm/translators.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 05d3494ad60fcc..ca547699d00ed1 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -168,7 +168,7 @@ translators.set('module', async function moduleStrategy(url, source, isMain) { const module = new ModuleWrap(url, undefined, source, 0, 0); // Cache the source map for the module if present. if (module.sourceMapURL) { - maybeCacheSourceMap(url, source, null, false, url, module.sourceMapURL); + maybeCacheSourceMap(url, source, null, false, undefined, module.sourceMapURL); } const { registerModule } = require('internal/modules/esm/utils'); registerModule(module, { @@ -238,7 +238,7 @@ function loadCJSModule(module, source, url, filename) { } // Cache the source map for the cjs module if present. if (compileResult.sourceMapURL) { - maybeCacheSourceMap(url, source, null, false, url, compileResult.sourceMapURL); + maybeCacheSourceMap(url, source, null, false, undefined, compileResult.sourceMapURL); } const compiledWrapper = compileResult.function; From 2f6ffe83580b5aba1f56948173308a9cc0283b38 Mon Sep 17 00:00:00 2001 From: unbyte Date: Thu, 8 Feb 2024 01:22:51 +0800 Subject: [PATCH 6/7] chore: add fixme comment --- lib/internal/source_map/source_map_cache.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/internal/source_map/source_map_cache.js b/lib/internal/source_map/source_map_cache.js index fb411487336c29..338f434a24d566 100644 --- a/lib/internal/source_map/source_map_cache.js +++ b/lib/internal/source_map/source_map_cache.js @@ -123,6 +123,8 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo return; } + // FIXME: callers should obtain sourceURL from v8 and pass it + // rather than leaving it undefined and extract by regex. if (sourceURL === undefined) { sourceURL = extractSourceURLMagicComment(content); } From b446b4c6c8bfa365e9526f4c7fdfe7d88ed99744 Mon Sep 17 00:00:00 2001 From: unbyte Date: Thu, 8 Feb 2024 01:35:38 +0800 Subject: [PATCH 7/7] chore: fix lint issue --- lib/internal/source_map/source_map_cache.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/source_map/source_map_cache.js b/lib/internal/source_map/source_map_cache.js index 338f434a24d566..53c3374fc09176 100644 --- a/lib/internal/source_map/source_map_cache.js +++ b/lib/internal/source_map/source_map_cache.js @@ -123,7 +123,7 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo return; } - // FIXME: callers should obtain sourceURL from v8 and pass it + // FIXME: callers should obtain sourceURL from v8 and pass it // rather than leaving it undefined and extract by regex. if (sourceURL === undefined) { sourceURL = extractSourceURLMagicComment(content);