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

Commit

Permalink
refactor: tests (#336)
Browse files Browse the repository at this point in the history
* refactor: tests

* refactor: `extractComments` tests

* refactor: ignore some lines to coverage

* tests: minify error

* refactor: `RequestShortener`

* refactor: `sourceMap` tests

* refactor: same order for tests

* refactor: move `validation` test to own file

* refactor: basic test

* refactor: more tests

* refactor: more tests

* refactor: tests

* refactor: tests

* fix: missing snapshot

* test: `error` in `cacheKeys` option

* test: invalid source map

* style: code
  • Loading branch information
evilebottnawi authored Aug 1, 2018
1 parent 77d957a commit c67566e
Show file tree
Hide file tree
Showing 21 changed files with 3,481 additions and 1,708 deletions.
51 changes: 29 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ class UglifyJsPlugin {
column: err.col,
});

if (original && original.source) {
if (original && original.source && requestShortener) {
return new Error(`${file} from UglifyJs\n${err.message} [${requestShortener.shorten(original.source)}:${original.line},${original.column}][${file}:${err.line},${err.col}]`);
}

return new Error(`${file} from UglifyJs\n${err.message} [${file}:${err.line},${err.col}]`);
} else if (err.stack) {
return new Error(`${file} from UglifyJs\n${err.stack}`);
Expand All @@ -93,33 +94,34 @@ class UglifyJsPlugin {

static buildWarning(warning, file, sourceMap, warningsFilter, requestShortener) {
if (!file || !sourceMap) {
return warning;
return `UglifyJs Plugin: ${warning}`;
}

let warningMessage = warning;

const match = warningRegex.exec(warning);
const line = +match[1];
const column = +match[2];
const original = sourceMap.originalPositionFor({
line,
column,
});

if (!warningsFilter(original.source)) {
return null;
}

let warningMessage = warning.replace(warningRegex, '');
if (match) {
const line = +match[1];
const column = +match[2];
const original = sourceMap.originalPositionFor({
line,
column,
});

if (warningsFilter && !warningsFilter(original.source)) {
return null;
}

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

return `UglifyJs Plugin: ${warningMessage} in ${file}`;
}

apply(compiler) {
const requestShortener = new RequestShortener(compiler.context);

const buildModuleFn = (moduleArg) => {
// to get detailed location info about errors
moduleArg.useSourceMap = true;
Expand Down Expand Up @@ -158,6 +160,7 @@ class UglifyJsPlugin {
inputSourceMap = map;
} else {
inputSourceMap = map;

compilation.warnings.push(
new Error(`${file} contains invalid source map`),
);
Expand Down Expand Up @@ -209,7 +212,7 @@ class UglifyJsPlugin {
error,
file,
UglifyJsPlugin.buildSourceMap(inputSourceMap),
requestShortener,
new RequestShortener(compiler.context),
),
);
}
Expand Down Expand Up @@ -240,7 +243,7 @@ class UglifyJsPlugin {
error,
file,
sourceMap,
requestShortener,
new RequestShortener(compiler.context),
),
);

Expand Down Expand Up @@ -272,7 +275,8 @@ class UglifyJsPlugin {

if (banner) {
outputSource = new ConcatSource(
`/*! ${banner} */\n`, outputSource,
`/*! ${banner} */\n`,
outputSource,
);
}
}
Expand All @@ -286,7 +290,9 @@ class UglifyJsPlugin {
compilation.assets[commentsFile].add(commentsSource);
} else {
compilation.assets[commentsFile] = new ConcatSource(
compilation.assets[commentsFile], '\n', commentsSource,
compilation.assets[commentsFile],
'\n',
commentsSource,
);
}
} else {
Expand All @@ -305,7 +311,7 @@ class UglifyJsPlugin {
file,
sourceMap,
this.options.warningsFilter,
requestShortener,
new RequestShortener(compiler.context),
);

if (builtWarning) {
Expand All @@ -321,6 +327,7 @@ class UglifyJsPlugin {
});
};

/* istanbul ignore if */
if (compiler.hooks) {
const plugin = { name: 'UglifyJSPlugin' };

Expand Down
5 changes: 3 additions & 2 deletions src/uglify/Runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default class Runner {
}

runTasks(tasks, callback) {
/* istanbul ignore if */
if (!tasks.length) {
callback(null, []);
return;
Expand All @@ -33,8 +34,8 @@ export default class Runner {
this.boundWorkers = (options, cb) => {
try {
cb(null, minify(options));
} catch (errors) {
cb(errors);
} catch (error) {
cb(error);
}
};
}
Expand Down
Loading

0 comments on commit c67566e

Please sign in to comment.