From 3566c6f1a4d6b079d3faa449d5f6c5b8b9bc683b Mon Sep 17 00:00:00 2001 From: legendecas Date: Wed, 5 Oct 2022 15:46:12 +0800 Subject: [PATCH] fixup! src,lib: retrieve parsed source map url from v8 --- doc/api/vm.md | 14 +++- lib/internal/modules/cjs/loader.js | 10 ++- lib/internal/process/pre_execution.js | 4 +- lib/internal/source_map/source_map_cache.js | 73 +++++++++++---------- 4 files changed, 62 insertions(+), 39 deletions(-) diff --git a/doc/api/vm.md b/doc/api/vm.md index c79f8bc104a579..91f77cfc044a9f 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -355,7 +355,19 @@ added: REPLACEME When the script is compiled from a source that contains a source map magic comment, this property will be set to the URL of the source map. -```js +```mjs +import vm from 'node:vm'; + +const script = new vm.Script(` +function myFunc() {} +//# sourceMappingURL=sourcemap.json +`); + +console.log(script.sourceMapURL); +// Prints: sourcemap.json +``` + +```cjs const vm = require('node:vm'); const script = new vm.Script(` diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 219f3844b1ed7a..7e6cad0f6f3388 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -1084,7 +1084,10 @@ function wrapSafe(filename, content, cjsModuleInstance) { }, }); - maybeCacheSourceMap(filename, content, this, false, undefined, script.sourceMapURL); + // Cache the source map for the module if present. + if (script.sourceMapURL) { + maybeCacheSourceMap(filename, content, this, false, undefined, script.sourceMapURL); + } return script.runInThisContext({ displayErrors: true, @@ -1107,7 +1110,10 @@ function wrapSafe(filename, content, cjsModuleInstance) { }, }); - maybeCacheSourceMap(filename, content, this, false, undefined, result.sourceMapURL); + // Cache the source map for the module if present. + if (result.sourceMapURL) { + maybeCacheSourceMap(filename, content, this, false, undefined, result.sourceMapURL); + } return result.function; } catch (err) { diff --git a/lib/internal/process/pre_execution.js b/lib/internal/process/pre_execution.js index 6ad5502b61ded1..067d70a11d379f 100644 --- a/lib/internal/process/pre_execution.js +++ b/lib/internal/process/pre_execution.js @@ -579,9 +579,11 @@ function initializeESMLoader() { } function initializeSourceMapsHandlers() { - const { setSourceMapsEnabled } = + const { setSourceMapsEnabled, getSourceMapsEnabled } = require('internal/source_map/source_map_cache'); process.setSourceMapsEnabled = setSourceMapsEnabled; + // Initialize the environment flag of source maps. + getSourceMapsEnabled(); } function initializeFrozenIntrinsics() { diff --git a/lib/internal/source_map/source_map_cache.js b/lib/internal/source_map/source_map_cache.js index ab077b67b22eeb..656c4e3f296e94 100644 --- a/lib/internal/source_map/source_map_cache.js +++ b/lib/internal/source_map/source_map_cache.js @@ -126,45 +126,48 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo sourceMapURL = extractSourceMapURLMagicComment(content); } + // Bail out when there is no source map url. + if (typeof sourceMapURL !== 'string') { + return; + } + if (sourceURL === undefined) { sourceURL = extractSourceURLMagicComment(content); } - if (sourceMapURL) { - const data = dataFromUrl(filename, sourceMapURL); - const url = data ? null : sourceMapURL; - if (cjsModuleInstance) { - cjsSourceMapCache.set(cjsModuleInstance, { - filename, - lineLengths: lineLengths(content), - data, - url, - sourceURL, - }); - } else if (isGeneratedSource) { - const entry = { - lineLengths: lineLengths(content), - data, - url, - sourceURL - }; - generatedSourceMapCache.set(filename, entry); - if (sourceURL) { - generatedSourceMapCache.set(sourceURL, entry); - } - } else { - // If there is no cjsModuleInstance and is not generated source assume we are in a - // "modules/esm" context. - const entry = { - lineLengths: lineLengths(content), - data, - url, - sourceURL, - }; - esmSourceMapCache.set(filename, entry); - if (sourceURL) { - esmSourceMapCache.set(sourceURL, entry); - } + const data = dataFromUrl(filename, sourceMapURL); + const url = data ? null : sourceMapURL; + if (cjsModuleInstance) { + cjsSourceMapCache.set(cjsModuleInstance, { + filename, + lineLengths: lineLengths(content), + data, + url, + sourceURL, + }); + } else if (isGeneratedSource) { + const entry = { + lineLengths: lineLengths(content), + data, + url, + sourceURL + }; + generatedSourceMapCache.set(filename, entry); + if (sourceURL) { + generatedSourceMapCache.set(sourceURL, entry); + } + } else { + // If there is no cjsModuleInstance and is not generated source assume we are in a + // "modules/esm" context. + const entry = { + lineLengths: lineLengths(content), + data, + url, + sourceURL, + }; + esmSourceMapCache.set(filename, entry); + if (sourceURL) { + esmSourceMapCache.set(sourceURL, entry); } } }