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

Commit

Permalink
refactor: push each warning separately (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Apr 13, 2018
1 parent 693e45e commit e94a401
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
61 changes: 33 additions & 28 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,30 @@ class UglifyJsPlugin {
return new Error(`${file} from UglifyJs\n${err.message}`);
}

static buildWarnings(warnings, file, sourceMap, warningsFilter, requestShortener) {
if (!sourceMap) {
return warnings;
static buildWarning(warning, file, sourceMap, warningsFilter, requestShortener) {
if (!file || !sourceMap) {
return warning;
}
return warnings.reduce((accWarnings, warning) => {
const match = warningRegex.exec(warning);
const line = +match[1];
const column = +match[2];
const original = sourceMap.originalPositionFor({
line,
column,
});

if (original && original.source && original.source !== file && warningsFilter(original.source)) {
accWarnings.push(`${warning.replace(warningRegex, '')}[${requestShortener.shorten(original.source)}:${original.line},${original.column}]`);
const match = warningRegex.exec(warning);
const line = +match[1];
const column = +match[2];
const original = sourceMap.originalPositionFor({
line,
column,
});

let warningMessage = null;

if (warningsFilter(original.source)) {
warningMessage = warning.replace(warningRegex, '');

if (original && original.source && original.source !== file) {
warningMessage += `[${requestShortener.shorten(original.source)}:${original.line},${original.column}]`;
}
}

return accWarnings;
}, []);
return warningMessage;
}

apply(compiler) {
Expand Down Expand Up @@ -257,20 +262,20 @@ class UglifyJsPlugin {
uglifiedAssets.add(compilation.assets[file] = outputSource);

// Handling warnings
if (warnings) {
const warnArr = UglifyJsPlugin.buildWarnings(
warnings,
file,
sourceMap,
this.options.warningsFilter,
requestShortener,
);

if (warnArr.length > 0) {
compilation.warnings.push(
new Error(`${file} from UglifyJs\n${warnArr.join('\n')}`),
if (warnings && warnings.length > 0) {
warnings.forEach((warning) => {
const builtWarning = UglifyJsPlugin.buildWarning(
warning,
file,
sourceMap,
this.options.warningsFilter,
requestShortener,
);
}

if (builtWarning) {
compilation.warnings.push(builtWarning);
}
});
}
});

Expand Down
3 changes: 1 addition & 2 deletions test/__snapshots__/all-options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ exports[`when applied with all options matches snapshot: manifest.d6857f782c13a9

exports[`when applied with all options matches snapshot: warnings 1`] = `
Array [
"Error: main.0c220ec66316af2c1b24.js from UglifyJs
Dropping unused variable a [./test/fixtures/entry.js:4,0]",
"Dropping unused variable a [./test/fixtures/entry.js:4,0]",
]
`;

0 comments on commit e94a401

Please sign in to comment.