mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
lib: allow skipping source maps in node_modules
Files in `node_modules` are not authored by the user directly and the original sources are less relevant to the user. Skipping source maps in `node_modules` improves the general performance. Add `module.setSourceMapsSupport(enabled, options)` to skip source maps in `node_modules` if it is needed. This moves all source maps related API to `node:module` and this a step to promote the source maps API to stable. PR-URL: https://github.com/nodejs/node/pull/56639 Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
1 parent
f885496d9c
commit
2fb007fdce
24 files changed
+491
-48
No files matched your search
@@ -3,6 +3,7 @@
|
||||
const {
|
||||
ArrayPrototypePush,
|
||||
JSONParse,
|
||||
ObjectFreeze,
|
||||
RegExpPrototypeExec,
|
||||
SafeMap,
|
||||
StringPrototypeCodePointAt,
|
||||
@@ -15,7 +16,7 @@ let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => {
|
||||
debug = fn;
|
||||
});
|
||||
|
||||
const { validateBoolean } = require('internal/validators');
|
||||
const { validateBoolean, validateObject } = require('internal/validators');
|
||||
const {
|
||||
setSourceMapsEnabled: setSourceMapsNative,
|
||||
} = internalBinding('errors');
|
||||
@@ -23,7 +24,7 @@ const {
|
||||
defaultPrepareStackTrace,
|
||||
setInternalPrepareStackTrace,
|
||||
} = require('internal/errors');
|
||||
const { getLazy } = require('internal/util');
|
||||
const { getLazy, isUnderNodeModules, kEmptyObject } = require('internal/util');
|
||||
|
||||
const getModuleSourceMapCache = getLazy(() => {
|
||||
const { SourceMapCacheMap } = require('internal/source_map/source_map_cache_map');
|
||||
@@ -45,30 +46,48 @@ const { fileURLToPath, pathToFileURL, URL, URLParse } = require('internal/url');
|
||||
let SourceMap;
|
||||
|
||||
// This is configured with --enable-source-maps during pre-execution.
|
||||
let sourceMapsEnabled = false;
|
||||
function getSourceMapsEnabled() {
|
||||
return sourceMapsEnabled;
|
||||
let sourceMapsSupport = ObjectFreeze({
|
||||
__proto__: null,
|
||||
enabled: false,
|
||||
nodeModules: false,
|
||||
generatedCode: false,
|
||||
});
|
||||
function getSourceMapsSupport() {
|
||||
// Return a read-only object.
|
||||
return sourceMapsSupport;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables or disables source maps programmatically.
|
||||
* @param {boolean} val
|
||||
* @param {boolean} enabled
|
||||
* @param {object} options
|
||||
* @param {boolean} [options.nodeModules]
|
||||
* @param {boolean} [options.generatedCode]
|
||||
*/
|
||||
function setSourceMapsEnabled(val) {
|
||||
validateBoolean(val, 'val');
|
||||
function setSourceMapsSupport(enabled, options = kEmptyObject) {
|
||||
validateBoolean(enabled, 'enabled');
|
||||
validateObject(options, 'options');
|
||||
|
||||
setSourceMapsNative(val);
|
||||
if (val) {
|
||||
const { nodeModules = false, generatedCode = false } = options;
|
||||
validateBoolean(nodeModules, 'options.nodeModules');
|
||||
validateBoolean(generatedCode, 'options.generatedCode');
|
||||
|
||||
setSourceMapsNative(enabled);
|
||||
if (enabled) {
|
||||
const {
|
||||
prepareStackTraceWithSourceMaps,
|
||||
} = require('internal/source_map/prepare_stack_trace');
|
||||
setInternalPrepareStackTrace(prepareStackTraceWithSourceMaps);
|
||||
} else if (sourceMapsEnabled !== undefined) {
|
||||
// Reset prepare stack trace callback only when disabling source maps.
|
||||
} else {
|
||||
setInternalPrepareStackTrace(defaultPrepareStackTrace);
|
||||
}
|
||||
|
||||
sourceMapsEnabled = val;
|
||||
sourceMapsSupport = ObjectFreeze({
|
||||
__proto__: null,
|
||||
enabled,
|
||||
nodeModules: nodeModules,
|
||||
generatedCode: generatedCode,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,14 +149,18 @@ function extractSourceMapURLMagicComment(content) {
|
||||
* @param {string | undefined} sourceMapURL - the source map url
|
||||
*/
|
||||
function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSource, sourceURL, sourceMapURL) {
|
||||
const sourceMapsEnabled = getSourceMapsEnabled();
|
||||
if (!(process.env.NODE_V8_COVERAGE || sourceMapsEnabled)) return;
|
||||
const support = getSourceMapsSupport();
|
||||
if (!(process.env.NODE_V8_COVERAGE || support.enabled)) return;
|
||||
const { normalizeReferrerURL } = require('internal/modules/helpers');
|
||||
filename = normalizeReferrerURL(filename);
|
||||
if (filename === undefined) {
|
||||
// This is most likely an invalid filename in sourceURL of [eval]-wrapper.
|
||||
return;
|
||||
}
|
||||
if (!support.nodeModules && isUnderNodeModules(filename)) {
|
||||
// Skip file under node_modules if not enabled.
|
||||
return;
|
||||
}
|
||||
|
||||
if (sourceMapURL === undefined) {
|
||||
sourceMapURL = extractSourceMapURLMagicComment(content);
|
||||
@@ -185,8 +208,8 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc
|
||||
* @param {string} content - the eval'd source code
|
||||
*/
|
||||
function maybeCacheGeneratedSourceMap(content) {
|
||||
const sourceMapsEnabled = getSourceMapsEnabled();
|
||||
if (!(process.env.NODE_V8_COVERAGE || sourceMapsEnabled)) return;
|
||||
const support = getSourceMapsSupport();
|
||||
if (!(process.env.NODE_V8_COVERAGE || support.enabled || support.generated)) return;
|
||||
|
||||
const sourceURL = extractSourceURLMagicComment(content);
|
||||
if (sourceURL === null) {
|
||||
@@ -352,6 +375,10 @@ function findSourceMap(sourceURL) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!getSourceMapsSupport().nodeModules && isUnderNodeModules(sourceURL)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
SourceMap ??= require('internal/source_map/source_map').SourceMap;
|
||||
try {
|
||||
if (RegExpPrototypeExec(kLeadingProtocol, sourceURL) === null) {
|
||||
@@ -377,8 +404,8 @@ function findSourceMap(sourceURL) {
|
||||
|
||||
module.exports = {
|
||||
findSourceMap,
|
||||
getSourceMapsEnabled,
|
||||
setSourceMapsEnabled,
|
||||
getSourceMapsSupport,
|
||||
setSourceMapsSupport,
|
||||
maybeCacheSourceMap,
|
||||
maybeCacheGeneratedSourceMap,
|
||||
sourceMapCacheToObject,
|
||||
|
||||
Reference in new issue
Block a user