Skip to content

Commit 3f1b7e6

Browse files
committed
Store relative path cache on compilation to avoid leaking memory.
1 parent 164ce36 commit 3f1b7e6

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

lib/optimize/AggressiveSplittingPlugin.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class AggressiveSplittingPlugin {
5656

5757
const nameToModuleMap = new Map();
5858
chunk.forEachModule(m => {
59-
const name = identifierUtils.makePathsRelative(compiler.context, m.identifier());
59+
const name = identifierUtils.makePathsRelative(compiler.context, m.identifier(), compilation.cache);
6060
nameToModuleMap.set(name, m);
6161
});
6262

@@ -127,7 +127,7 @@ class AggressiveSplittingPlugin {
127127
newChunk.origins = chunk.origins.map(copyWithReason);
128128
chunk.origins = chunk.origins.map(copyWithReason);
129129
compilation._aggressiveSplittingSplits = (compilation._aggressiveSplittingSplits || []).concat({
130-
modules: newChunk.mapModules(m => identifierUtils.makePathsRelative(compiler.context, m.identifier()))
130+
modules: newChunk.mapModules(m => identifierUtils.makePathsRelative(compiler.context, m.identifier(), compilation.cache))
131131
});
132132
return true;
133133
} else {
@@ -144,7 +144,7 @@ class AggressiveSplittingPlugin {
144144
if(chunk.hasEntryModule()) return;
145145
const size = chunk.size(this.options);
146146
const incorrectSize = size < minSize;
147-
const modules = chunk.mapModules(m => identifierUtils.makePathsRelative(compiler.context, m.identifier()));
147+
const modules = chunk.mapModules(m => identifierUtils.makePathsRelative(compiler.context, m.identifier(), compilation.cache));
148148
if(typeof chunk._fromAggressiveSplittingIndex === "undefined") {
149149
if(incorrectSize) return;
150150
chunk.recorded = true;

lib/util/identifier.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@ const looksLikeAbsolutePath = (maybeAbsolutePath) => {
77

88
const normalizePathSeparator = (p) => p.replace(/\\/g, "/");
99

10-
/* A map from context dir strings to maps from identifier names to relative paths */
11-
const relativePaths = new Map();
10+
const _makePathsRelative = (context, identifier) => {
11+
return identifier
12+
.split(/([|! ])/)
13+
.map(str => looksLikeAbsolutePath(str) ?
14+
normalizePathSeparator(path.relative(context, str)) : str)
15+
.join("");
16+
}
1217

13-
exports.makePathsRelative = (context, identifier) => {
18+
exports.makePathsRelative = (context, identifier, cache) => {
19+
if(!cache) return _makePathsRelative(context, identifier);
20+
21+
const relativePaths = cache.relativePaths || (cache.relativePaths = new Map());
1422
if(!relativePaths.has(context)) {
1523
relativePaths.set(context, new Map());
1624
}
@@ -20,11 +28,7 @@ exports.makePathsRelative = (context, identifier) => {
2028
if(contextCache.has(identifier)) {
2129
return contextCache.get(identifier);
2230
} else {
23-
var relativePath = identifier
24-
.split(/([|! ])/)
25-
.map(str => looksLikeAbsolutePath(str) ?
26-
normalizePathSeparator(path.relative(context, str)) : str)
27-
.join("");
31+
let relativePath = _makePathsRelative(context, identifier);
2832
contextCache.set(identifier, relativePath);
2933
return relativePath;
3034
}

0 commit comments

Comments
 (0)