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

fix: perf regression #305

Merged
merged 1 commit into from
Jun 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 23 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,19 @@ class UglifyJsPlugin {
...uglifyOptions,
},
};
this.sourceMapsCache = new WeakMap();
}

buildError(err, file, inputSourceMap, requestShortener) {
static buildSourceMap(inputSourceMap) {
if (!inputSourceMap || !utils.isSourceMap(inputSourceMap)) {
return null;
}

return new SourceMapConsumer(inputSourceMap);
}

static buildError(err, file, sourceMap, requestShortener) {
// Handling error which should have line, col, filename and message
if (err.line) {
const sourceMapCacheKey = { file };
let sourceMap = this.sourceMapsCache.get(sourceMapCacheKey);
if (!sourceMap) {
sourceMap = new SourceMapConsumer(inputSourceMap);
this.sourceMapsCache.set(sourceMapCacheKey, sourceMap);
}
const original = sourceMap && sourceMap.originalPositionFor({
line: err.line,
column: err.col,
Expand All @@ -74,20 +75,11 @@ class UglifyJsPlugin {
return new Error(`${file} from UglifyJs\n${err.message}`);
}

buildWarning(warning, file, inputSourceMap, warningsFilter, requestShortener) {
if (!file || !inputSourceMap) {
static buildWarning(warning, file, sourceMap, warningsFilter, requestShortener) {
if (!file || !sourceMap) {
return warning;
}

const sourceMapCacheKey = { file };

let sourceMap = this.sourceMapsCache.get(sourceMapCacheKey);

if (!sourceMap) {
sourceMap = new SourceMapConsumer(inputSourceMap);
this.sourceMapsCache.set(sourceMapCacheKey, sourceMap);
}

const match = warningRegex.exec(warning);
const line = +match[1];
const column = +match[2];
Expand Down Expand Up @@ -188,10 +180,10 @@ class UglifyJsPlugin {
tasks.push(task);
} catch (error) {
compilation.errors.push(
this.buildError(
UglifyJsPlugin.buildError(
error,
file,
inputSourceMap,
UglifyJsPlugin.buildSourceMap(inputSourceMap),
requestShortener,
),
);
Expand All @@ -208,14 +200,20 @@ class UglifyJsPlugin {
const { file, input, inputSourceMap, commentsFile } = tasks[index];
const { error, map, code, warnings, extractedComments } = data;

let sourceMap = null;

if (error || (warnings && warnings.length > 0)) {
sourceMap = UglifyJsPlugin.buildSourceMap(inputSourceMap);
}

// Handling results
// Error case: add errors, and go to next file
if (error) {
compilation.errors.push(
this.buildError(
UglifyJsPlugin.buildError(
error,
file,
inputSourceMap,
sourceMap,
requestShortener,
),
);
Expand Down Expand Up @@ -275,10 +273,10 @@ class UglifyJsPlugin {
// Handling warnings
if (warnings && warnings.length > 0) {
warnings.forEach((warning) => {
const builtWarning = this.buildWarning(
const builtWarning = UglifyJsPlugin.buildWarning(
warning,
file,
inputSourceMap,
sourceMap,
this.options.warningsFilter,
requestShortener,
);
Expand Down