|
Expand Up
|
@@ -36,12 +36,12 @@ const { makePathsAbsolute } = require("./util/identifier"); |
|
|
/** |
|
|
* Defines the source map task type used by this module. |
|
|
* @typedef {object} SourceMapTask |
|
|
* @property {Source} asset |
|
|
* @property {AssetInfo} assetInfo |
|
|
* @property {(string | Module)[]} modules |
|
|
* @property {string} source |
|
|
* @property {string} file |
|
|
* @property {RawSourceMap} sourceMap |
|
|
* @property {Source} mapSource the Source instance whose `sourceAndMap` we called (the current asset or, when its map was already stripped, the pinned original from `originalSources`) — what `clearCache` should target |
|
|
* @property {ItemCacheFacade} cacheItem cache item |
|
|
*/ |
|
|
|
|
Expand Down
Expand Up
|
@@ -111,12 +111,14 @@ const getOriginalSourceRegistry = (compilation) => { |
|
|
* The returned source is read from the asset as it currently stands — that way |
|
|
* any `sourceMappingURL` comments appended by earlier plugin instances survive |
|
|
* — while the map is taken from the pinned original Source when the current |
|
|
* one no longer carries it. |
|
|
* one no longer carries it. `mapSource` identifies which Source instance was |
|
|
* actually queried for the map (the current asset, or the pinned original); |
|
|
* that's the one whose internal caches the caller should release. |
|
|
* @param {string} file file name |
|
|
* @param {Source} asset source object as currently held by the compilation |
|
|
* @param {MapOptions} options map extraction options |
|
|
* @param {Map<string, Source>} registry compilation-scoped original-source registry |
|
|
* @returns {{ source: string, sourceMap: RawSourceMap } | undefined} extracted pair or `undefined` when no map is recoverable |
|
|
* @returns {{ source: string, sourceMap: RawSourceMap, mapSource: Source } | undefined} extracted pair or `undefined` when no map is recoverable |
|
|
*/ |
|
|
const extractSourceAndMap = (file, asset, options, registry) => { |
|
|
/** @type {string | Buffer} */ |
|
Expand All
|
@@ -139,20 +141,20 @@ const extractSourceAndMap = (file, asset, options, registry) => { |
|
|
// that a later plugin instance (which will see a rewrapped asset |
|
|
// without a map) can recover it on demand. |
|
|
if (!registry.has(file)) registry.set(file, asset); |
|
|
} else { |
|
|
// The current asset (typically a `RawSource` left by an earlier |
|
|
// SourceMapDevToolPlugin instance) has no internal map. Re-extract |
|
|
// the map from the original Source we pinned earlier. We keep using |
|
|
// `source` from the current asset so that any prior wrappers (e.g. |
|
|
// appended sourceMappingURL comments) are preserved. |
|
|
const original = registry.get(file); |
|
|
if (!original) return; |
|
|
sourceMap = original.sourceAndMap |
|
|
? original.sourceAndMap(options).map |
|
|
: original.map(options); |
|
|
if (!sourceMap) return; |
|
|
return { source, sourceMap, mapSource: asset }; |
|
|
} |
|
|
return { source, sourceMap }; |
|
|
// The current asset (typically a `RawSource` left by an earlier |
|
|
// SourceMapDevToolPlugin instance) has no internal map. Re-extract |
|
|
// the map from the original Source we pinned earlier. We keep using |
|
|
// `source` from the current asset so that any prior wrappers (e.g. |
|
|
// appended sourceMappingURL comments) are preserved. |
|
|
const original = registry.get(file); |
|
|
if (!original) return; |
|
|
sourceMap = original.sourceAndMap |
|
|
? original.sourceAndMap(options).map |
|
|
: original.map(options); |
|
|
if (!sourceMap) return; |
|
|
return { source, sourceMap, mapSource: original }; |
|
|
}; |
|
|
|
|
|
/** |
|
Expand All
|
@@ -177,7 +179,7 @@ const getTaskForFile = ( |
|
|
) => { |
|
|
const extracted = extractSourceAndMap(file, asset, options, registry); |
|
|
if (!extracted) return; |
|
|
const { source, sourceMap } = extracted; |
|
|
const { source, sourceMap, mapSource } = extracted; |
|
|
const context = compilation.options.context; |
|
|
const root = compilation.compiler.root; |
|
|
const cachedAbsolutify = makePathsAbsolute.bindContextCache(context, root); |
|
Expand All
|
@@ -190,10 +192,10 @@ const getTaskForFile = ( |
|
|
|
|
|
return { |
|
|
file, |
|
|
asset, |
|
|
source: /** @type {string} */ (source), |
|
|
assetInfo, |
|
|
sourceMap, |
|
|
mapSource, |
|
|
modules, |
|
|
cacheItem |
|
|
}; |
|
Expand Down
Expand Up
|
@@ -360,6 +362,25 @@ class SourceMapDevToolPlugin { |
|
|
const tasks = []; |
|
|
let fileIndex = 0; |
|
|
|
|
|
// Shared deduplication set for `Source#clearCache` calls below. |
|
|
// Webpack chunks routinely share module-level `CachedSource` |
|
|
// instances. A per-call WeakSet would re-walk those shared |
|
|
// subtrees once per chunk — 50 chunks × thousands of shared |
|
|
// modules in dev/non-minified setups — and worse, every |
|
|
// chunk's `sourceAndMap` would have to recompute the cleared |
|
|
// caches, churning allocations (measured: +700 MB peak RSS, |
|
|
// +6 s wall time on a 50×1000 synthetic build). |
|
|
// |
|
|
// Sharing one set lets each shared subtree be walked exactly |
|
|
// once. The trade-off is that subsequent chunks' `sourceAndMap` |
|
|
// calls can repopulate a shared module's `_cachedMaps` after |
|
|
// its own clear was skipped (because the module is already in |
|
|
// the visited set), leaving at most one populated cache entry |
|
|
// per shared module at the end of the run — bounded to a few |
|
|
// MB even at the scale of #20961. That's strictly preferable |
|
|
// to the alternative's hundreds of MB of transient peak RSS. |
|
|
const clearCacheVisited = new WeakSet(); |
|
|
|
|
|
asyncLib.each( |
|
|
files, |
|
|
(file, callback) => { |
|
Expand Down
Expand Up
|
@@ -458,6 +479,44 @@ class SourceMapDevToolPlugin { |
|
|
originalSources |
|
|
); |
|
|
|
|
|
// Release the per-instance caches that `sourceAndMap` |
|
|
// just populated. The composed map (and, for |
|
|
// `SourceMapSource`, the parsed `_sourceMapAsObject` / |
|
|
// `_innerSourceMapAsObject`) otherwise sit on the |
|
|
// CachedSource — and every shared child — until phase |
|
|
// 2 replaces the asset, which is what causes the OOM |
|
|
// spike on builds with thousands of chunks |
|
|
// (webpack#20961). Keep `source` since downstream |
|
|
// consumers reading the original asset still need it; |
|
|
// `hash`/`size` default to retained because they're |
|
|
// cheap to keep but expensive to rebuild. |
|
|
// `clearCacheVisited` is shared across every call (see |
|
|
// its declaration above for the rationale). |
|
|
// |
|
|
// Target `task.mapSource` (not `asset.source`): when |
|
|
// `extractSourceAndMap` falls back to the pinned |
|
|
// original (the current asset is a `RawSource` left |
|
|
// by an earlier plugin instance), the `sourceAndMap` |
|
|
// call populated the original's caches, not the |
|
|
// current asset's. |
|
|
// |
|
|
// Feature-detected: `clearCache` landed in |
|
|
// `webpack-sources` 3.5, but `compilation.assets` may |
|
|
// hold `Source`-like instances from a third-party |
|
|
// plugin built against an older copy of |
|
|
// `webpack-sources` (or a hand-rolled implementation). |
|
|
// Calling it unconditionally would throw on those. |
|
|
if (task && typeof task.mapSource.clearCache === "function") { |
|
|
task.mapSource.clearCache( |
|
|
{ |
|
|
maps: true, |
|
|
source: false, |
|
|
parsedMap: true |
|
|
}, |
|
|
clearCacheVisited |
|
|
); |
|
|
} |
|
|
|
|
|
if (task) { |
|
|
const modules = task.modules; |
|
|
|
|
Expand Down
Expand Up
|
@@ -698,12 +757,23 @@ class SourceMapDevToolPlugin { |
|
|
outputSourceMap.debugId = debugIdValue; |
|
|
} |
|
|
|
|
|
const sourceMapString = JSON.stringify(outputSourceMap); |
|
|
if (sourceMapFilename) { |
|
|
// External `.map` file: hold the serialized map as a |
|
|
// `Buffer` instead of a V8 string. `RawSource` accepts |
|
|
// a buffer directly, and the emitted asset stays in |
|
|
// `compilation.assets` until the build finishes — so |
|
|
// storing the bytes off the V8 heap (where Buffers |
|
|
// live, accounted as `external` memory) avoids keeping |
|
|
// a large V8 string alive for the rest of the build |
|
|
// and reduces heap pressure on `--max-old-space-size`. |
|
|
const sourceMapBuffer = Buffer.from( |
|
|
JSON.stringify(outputSourceMap), |
|
|
"utf8" |
|
|
); |
Comment thread
Comment on lines
+761
to
+772
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.
Reworded in 278696d — same point as #discussion_r3287251359, addressed there.
Generated by Claude Code
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
|
const filename = file; |
|
|
const sourceMapContentHash = usesContentHash |
|
|
? createHash(compilation.outputOptions.hashFunction) |
|
|
.update(sourceMapString) |
|
|
.update(sourceMapBuffer) |
|
|
.digest("hex") |
|
|
: undefined; |
|
|
|
|
Expand Down
Expand Up
|
@@ -775,7 +845,7 @@ class SourceMapDevToolPlugin { |
|
|
assetsInfo[file] = assetInfo; |
|
|
compilation.updateAsset(file, asset, assetInfo); |
|
|
// Add source map file to compilation assets and chunk files |
|
|
const sourceMapAsset = new RawSource(sourceMapString); |
|
|
const sourceMapAsset = new RawSource(sourceMapBuffer); |
|
|
const sourceMapAssetInfo = { |
|
|
...sourceMapInfo, |
|
|
development: true |
|
Expand All
|
@@ -801,6 +871,16 @@ class SourceMapDevToolPlugin { |
|
|
`${PLUGIN_NAME}: append can't be a function when no filename is provided` |
|
|
); |
|
|
} |
|
|
// Inline data-URL form: `[map]` gets the raw JSON, `[url]` |
|
|
// gets the same JSON base64-encoded. `URL_COMMENT_REGEXP` |
|
|
// is a `/g` regex, so a user `append` template with more |
|
|
// than one `[url]` placeholder would otherwise re-encode |
|
|
// the same JSON per match. Pre-compute both once. |
|
|
const sourceMapString = JSON.stringify(outputSourceMap); |
|
|
const sourceMapBase64 = Buffer.from( |
|
|
sourceMapString, |
|
|
"utf8" |
|
|
).toString("base64"); |
|
|
/** |
|
|
* Add source map as data url to asset |
|
|
*/ |
|
Expand All
|
@@ -811,10 +891,7 @@ class SourceMapDevToolPlugin { |
|
|
.replace( |
|
|
URL_COMMENT_REGEXP, |
|
|
() => |
|
|
`data:application/json;charset=utf-8;base64,${Buffer.from( |
|
|
sourceMapString, |
|
|
"utf8" |
|
|
).toString("base64")}` |
|
|
`data:application/json;charset=utf-8;base64,${sourceMapBase64}` |
|
|
) |
|
|
); |
|
|
assets[file] = asset; |
|
Expand Down
|
|