Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.

Commit 0b11134

Browse files
evilebottnawijoshwiens
authored andcommitted
perf(sourcemaps): reduce memory usage (#276)
1 parent e94a401 commit 0b11134

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

Diff for: src/index.js

+27-16
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,18 @@ class UglifyJsPlugin {
4848
...uglifyOptions,
4949
},
5050
};
51+
this.sourceMapsCache = new WeakMap();
5152
}
5253

53-
static buildError(err, file, sourceMap, requestShortener) {
54+
buildError(err, file, inputSourceMap, requestShortener) {
5455
// Handling error which should have line, col, filename and message
5556
if (err.line) {
57+
const sourceMapCacheKey = { file };
58+
let sourceMap = this.sourceMapsCache.get(sourceMapCacheKey);
59+
if (!sourceMap) {
60+
sourceMap = new SourceMapConsumer(inputSourceMap);
61+
this.sourceMapsCache.set(sourceMapCacheKey, sourceMap);
62+
}
5663
const original = sourceMap && sourceMap.originalPositionFor({
5764
line: err.line,
5865
column: err.col,
@@ -67,11 +74,20 @@ class UglifyJsPlugin {
6774
return new Error(`${file} from UglifyJs\n${err.message}`);
6875
}
6976

70-
static buildWarning(warning, file, sourceMap, warningsFilter, requestShortener) {
71-
if (!file || !sourceMap) {
77+
buildWarning(warning, file, inputSourceMap, warningsFilter, requestShortener) {
78+
if (!file || !inputSourceMap) {
7279
return warning;
7380
}
7481

82+
const sourceMapCacheKey = { file };
83+
84+
let sourceMap = this.sourceMapsCache.get(sourceMapCacheKey);
85+
86+
if (!sourceMap) {
87+
sourceMap = new SourceMapConsumer(inputSourceMap);
88+
this.sourceMapsCache.set(sourceMapCacheKey, sourceMap);
89+
}
90+
7591
const match = warningRegex.exec(warning);
7692
const line = +match[1];
7793
const column = +match[2];
@@ -114,15 +130,14 @@ class UglifyJsPlugin {
114130
.concat(compilation.additionalChunkAssets || [])
115131
.filter(ModuleFilenameHelpers.matchObject.bind(null, this.options))
116132
.forEach((file) => {
117-
let sourceMap;
133+
let inputSourceMap;
118134
const asset = compilation.assets[file];
119135
if (uglifiedAssets.has(asset)) {
120136
return;
121137
}
122138

123139
try {
124140
let input;
125-
let inputSourceMap;
126141

127142
if (this.options.sourceMap && asset.sourceAndMap) {
128143
const { source, map } = asset.sourceAndMap();
@@ -131,11 +146,8 @@ class UglifyJsPlugin {
131146

132147
if (utils.isSourceMap(map)) {
133148
inputSourceMap = map;
134-
sourceMap = new SourceMapConsumer(inputSourceMap);
135149
} else {
136150
inputSourceMap = map;
137-
sourceMap = null;
138-
139151
compilation.warnings.push(
140152
new Error(`${file} contain invalid source map`),
141153
);
@@ -157,7 +169,6 @@ class UglifyJsPlugin {
157169
const task = {
158170
file,
159171
input,
160-
sourceMap,
161172
inputSourceMap,
162173
commentsFile,
163174
extractComments: this.options.extractComments,
@@ -177,10 +188,10 @@ class UglifyJsPlugin {
177188
tasks.push(task);
178189
} catch (error) {
179190
compilation.errors.push(
180-
UglifyJsPlugin.buildError(
191+
this.buildError(
181192
error,
182193
file,
183-
sourceMap,
194+
inputSourceMap,
184195
requestShortener,
185196
),
186197
);
@@ -194,17 +205,17 @@ class UglifyJsPlugin {
194205
}
195206

196207
results.forEach((data, index) => {
197-
const { file, input, sourceMap, inputSourceMap, commentsFile } = tasks[index];
208+
const { file, input, inputSourceMap, commentsFile } = tasks[index];
198209
const { error, map, code, warnings, extractedComments } = data;
199210

200211
// Handling results
201212
// Error case: add errors, and go to next file
202213
if (error) {
203214
compilation.errors.push(
204-
UglifyJsPlugin.buildError(
215+
this.buildError(
205216
error,
206217
file,
207-
sourceMap,
218+
inputSourceMap,
208219
requestShortener,
209220
),
210221
);
@@ -264,10 +275,10 @@ class UglifyJsPlugin {
264275
// Handling warnings
265276
if (warnings && warnings.length > 0) {
266277
warnings.forEach((warning) => {
267-
const builtWarning = UglifyJsPlugin.buildWarning(
278+
const builtWarning = this.buildWarning(
268279
warning,
269280
file,
270-
sourceMap,
281+
inputSourceMap,
271282
this.options.warningsFilter,
272283
requestShortener,
273284
);

0 commit comments

Comments
 (0)