Skip to content

Commit

Permalink
fixup! src,lib: retrieve parsed source map url from v8
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Oct 5, 2022
1 parent 86de757 commit 3566c6f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 39 deletions.
14 changes: 13 additions & 1 deletion doc/api/vm.md
Expand Up @@ -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(`
Expand Down
10 changes: 8 additions & 2 deletions lib/internal/modules/cjs/loader.js
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/process/pre_execution.js
Expand Up @@ -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() {
Expand Down
73 changes: 38 additions & 35 deletions lib/internal/source_map/source_map_cache.js
Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 3566c6f

Please sign in to comment.