Skip to content

Commit 182cf52

Browse files
authored
Merge pull request webpack#7349 from webpack/refactor/source_map_dev_tool
Use WeakMaps instead of mutating assets in SourceMapDevToolPlugin
2 parents 3605035 + bfc7425 commit 182cf52

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

lib/SourceMapDevToolPlugin.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ const basename = name => {
1818
return name.substr(name.lastIndexOf("/") + 1);
1919
};
2020

21+
const assetsCache = new WeakMap();
22+
2123
const getTaskForFile = (file, chunk, options, compilation) => {
2224
const asset = compilation.assets[file];
23-
if (asset.__SourceMapDevToolFile === file && asset.__SourceMapDevToolData) {
24-
const data = asset.__SourceMapDevToolData;
25-
for (const cachedFile in data) {
26-
compilation.assets[cachedFile] = data[cachedFile];
25+
const cache = assetsCache.get(asset);
26+
if (cache && cache.file === file) {
27+
for (const cachedFile in cache.assets) {
28+
compilation.assets[cachedFile] = cache.assets[cachedFile];
2729
if (cachedFile !== file) chunk.files.push(cachedFile);
2830
}
2931
return;
@@ -207,6 +209,7 @@ class SourceMapDevToolPlugin {
207209
task.file,
208210
"attach SourceMap"
209211
);
212+
const assets = Object.create(null);
210213
const chunk = task.chunk;
211214
const file = task.file;
212215
const asset = task.asset;
@@ -222,8 +225,7 @@ class SourceMapDevToolPlugin {
222225
}
223226
sourceMap.sourceRoot = options.sourceRoot || "";
224227
sourceMap.file = file;
225-
asset.__SourceMapDevToolFile = file;
226-
asset.__SourceMapDevToolData = {};
228+
assetsCache.set(asset, { file, assets });
227229
let currentSourceMappingURLComment = sourceMappingURLComment;
228230
if (
229231
currentSourceMappingURLComment !== false &&
@@ -260,24 +262,20 @@ class SourceMapDevToolPlugin {
260262
.relative(path.dirname(file), sourceMapFile)
261263
.replace(/\\/g, "/");
262264
if (currentSourceMappingURLComment !== false) {
263-
asset.__SourceMapDevToolData[file] = compilation.assets[
264-
file
265-
] = new ConcatSource(
265+
assets[file] = compilation.assets[file] = new ConcatSource(
266266
new RawSource(source),
267267
currentSourceMappingURLComment.replace(
268268
/\[url\]/g,
269269
sourceMapUrl
270270
)
271271
);
272272
}
273-
asset.__SourceMapDevToolData[sourceMapFile] = compilation.assets[
273+
assets[sourceMapFile] = compilation.assets[
274274
sourceMapFile
275275
] = new RawSource(sourceMapString);
276276
chunk.files.push(sourceMapFile);
277277
} else {
278-
asset.__SourceMapDevToolData[file] = compilation.assets[
279-
file
280-
] = new ConcatSource(
278+
assets[file] = compilation.assets[file] = new ConcatSource(
281279
new RawSource(source),
282280
currentSourceMappingURLComment
283281
.replace(/\[map\]/g, () => sourceMapString)

0 commit comments

Comments
 (0)