-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib: skip source maps in node_modules #56639
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
const { | ||
ArrayPrototypePush, | ||
JSONParse, | ||
ObjectFreeze, | ||
RegExpPrototypeExec, | ||
SafeMap, | ||
StringPrototypeCodePointAt, | ||
|
@@ -15,15 +16,15 @@ 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'); | ||
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, | ||
Comment on lines
+52
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for this one, this is probably breaking change if we keep |
||
}); | ||
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'); | ||
|
||
const { nodeModules = false, generatedCode = false } = options; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the past, we always enabled the source map for nodeModules right? If so, having the default values to |
||
validateBoolean(nodeModules, 'options.nodeModules'); | ||
validateBoolean(generatedCode, 'options.generatedCode'); | ||
|
||
setSourceMapsNative(val); | ||
if (val) { | ||
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, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's missing doc for default values I think