From 12779b3e02308b42a2b5aaa22b02fd8b4e68542f Mon Sep 17 00:00:00 2001 From: legendecas Date: Tue, 27 Sep 2022 00:55:59 +0800 Subject: [PATCH] src,lib: retrieve parsed source map url from v8 V8 already parses the source map magic comments. Currently, only scripts and functions expose the parsed source map URLs. It is unnecessary to parse the source map magic comments again when the parsed information is available. PR-URL: https://github.com/nodejs/node/pull/44798 Reviewed-By: Joyee Cheung Reviewed-By: James M Snell Reviewed-By: Yagiz Nizipli Reviewed-By: Jan Krems --- doc/api/vm.md | 35 ++++++ lib/internal/modules/cjs/loader.js | 26 ++++- lib/internal/process/pre_execution.js | 4 +- lib/internal/source_map/source_map_cache.js | 98 +++++++++-------- lib/internal/vm.js | 113 ++++++++++++++++++++ lib/vm.js | 92 +--------------- src/env_properties.h | 1 + src/node_contextify.cc | 28 +++-- test/parallel/test-bootstrap-modules.js | 1 + test/parallel/test-vm-source-map-url.js | 27 +++++ 10 files changed, 282 insertions(+), 143 deletions(-) create mode 100644 lib/internal/vm.js create mode 100644 test/parallel/test-vm-source-map-url.js diff --git a/doc/api/vm.md b/doc/api/vm.md index f99f4966db82b6..91f77cfc044a9f 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -344,6 +344,41 @@ console.log(globalVar); // 1000 ``` +### `script.sourceMapURL` + + + +* {string|undefined} + +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. + +```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(` +function myFunc() {} +//# sourceMappingURL=sourcemap.json +`); + +console.log(script.sourceMapURL); +// Prints: sourcemap.json +``` + ## Class: `vm.Module`