Skip to content

Commit

Permalink
fix build issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gdborton committed Dec 5, 2020
1 parent dcdcaba commit 4d95f85
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
33 changes: 19 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
const uglifier = require('./lib/uglifier');
const uglifier = require("./lib/uglifier");

function sourceMapError(lib) {
return `You should not pass options.${lib}.sourceMap, did you mean options.sourceMap?`;
}

function ParallelUglifyPlugin(options) {
if (options.uglifyJS && options.terser) {
throw new TypeError('You cannot use both uglifyJS and terser for the same plugin.');
throw new TypeError(
"You cannot use both uglifyJS and terser for the same plugin."
);
}

if (options.uglifyJS && options.uglifyJS.sourceMap) {
throw new TypeError(sourceMapError('uglifyJS'));
throw new TypeError(sourceMapError("uglifyJS"));
}

if (options.terser && options.terser.sourceMap) {
throw new TypeError(sourceMapError('terser'));
throw new TypeError(sourceMapError("terser"));
}
this.options = options;

Expand All @@ -24,18 +26,21 @@ function ParallelUglifyPlugin(options) {
}

ParallelUglifyPlugin.prototype.apply = function apply(compiler) {
compiler.hooks.compilation.tap('ParallelUglifyPlugin', (compilation) => {
compilation.hooks.processAssets.tapAsync({
name: 'ParallelUglifyPlugin',
stage: compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
}, (chunks, callback) => {
uglifier.processAssets(compilation, this.options).then(() => {
callback();
});
});
compiler.hooks.compilation.tap("ParallelUglifyPlugin", (compilation) => {
compilation.hooks.processAssets.tapAsync(
{
name: "ParallelUglifyPlugin",
stage: compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
},
(chunks, callback) => {
uglifier.processAssets(compilation, this.options).then(() => {
callback();
});
}
);
});

compiler.hooks.done.tap('ParallelUglifyPlugin', () => {
compiler.hooks.done.tap("ParallelUglifyPlugin", () => {
uglifier.pruneCache(this.options);
});
};
Expand Down
30 changes: 23 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,32 @@ test('providing no uglify options defaults to uglifyJS: {}', (t) => {
t.deepEqual(plugin.options, { uglifyJS: {} });
});

function FakeCompilation() {
this.hooks = {
processAssets: {
tapAsync: () => {

}
}
}
}

function FakeCompiler() {
const callbacks = {};
const fakeCompilation = new FakeCompilation();
this.assets = [];

this.plugin = (event, callback) => {
callbacks[event] = callback;
};
this.hooks = {
compilation: {
tap: (pluginName, callback) => {
callbacks['compilation'] = () => callback(fakeCompilation);
}
},
done: {
tap: (pluginName, callback) => {
callbacks['done'] = callback;
}
}
}

this.fireEvent = (event, ...args) => {
callbacks[event].apply(this, args);
Expand Down Expand Up @@ -82,10 +101,7 @@ test('deleting unused cache files after all asset optimizations', (t) => {
const compiler = new FakeCompiler();
uglifyPlugin.apply(compiler);
compiler.fireEvent('compilation', compiler);
compiler.fireEvent('optimize-chunk-assets', null, () => {});
compiler.fireEvent('optimize-chunk-assets', null, () => {});
t.is(fs.unlinkSync.callCount, 0, 'Cache should not be cleared by optimize-chunk-assets');

compiler.fireEvent('done');
t.deepEqual(
fs.unlinkSync.args,
Expand Down

0 comments on commit 4d95f85

Please sign in to comment.