Skip to content

Commit

Permalink
Hook up terser.
Browse files Browse the repository at this point in the history
  • Loading branch information
gdborton committed Dec 5, 2020
1 parent 655f906 commit 207d7fa
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 41 deletions.
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ function sourceMapError(lib) {
}

function FasterUglifyPlugin(options) {
if (options.uglifyJS && options.uglifyES) {
throw new TypeError('You cannot use both uglifyJS and uglifyES for the same plugin.');
if (options.uglifyJS && options.terser) {
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'));
}

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

if (!(this.options.uglifyJS || this.options.uglifyES)) {
if (!(this.options.uglifyJS || this.options.terser)) {
this.options.uglifyJS = {};
}
}
Expand Down
58 changes: 33 additions & 25 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@ function minify(source, map, uglifyOptions, uglifier) {
}

const result = uglifier.minify(source, opts);
if (result.error) {
if (result.error.name === 'SyntaxError') {
const frame = codeFrame(source, result.error.line, result.error.col);
const errorMessage = `${result.error.name}: ${result.error.message}\n${frame}`;
throw new SyntaxError(errorMessage);
const prom = result.then ? result : Promise.resolve(result);
return prom.then(resolved => {
if (resolved.error) {
if (resolved.error.name === 'SyntaxError') {
const frame = codeFrame(source, resolved.error.line, resolved.error.col);
const errorMessage = `${resolved.error.name}: ${resolved.error.message}\n${frame}`;
throw new SyntaxError(errorMessage);
}

throw resolved.error;
}

throw result.error;
}

result.code = result.code.replace(new RegExp(BOGUS_SOURCEMAP_URL), '');

return result;
resolved.code = resolved.code.replace(new RegExp(BOGUS_SOURCEMAP_URL), '');

return result;
});
}

/**
Expand Down Expand Up @@ -65,21 +68,26 @@ function processMessage(msgLocation, callback) {
const cacheKey = cache.createCacheKey(source + !!map, message.options);
// We do not check the cache here because we already determined that this asset yields a cache
// miss in the parent process.
const { uglifyES } = message.options;
const { terser } = message.options;
const { uglifyJS } = message.options;
const uglifier = uglifyES ? require('uglify-es') : require('uglify-js'); // eslint-disable-line global-require, max-len
const minifiedContent = minify(source, map, uglifyES || uglifyJS, uglifier);
cache.saveToCache(cacheKey, JSON.stringify({
source: minifiedContent.code,
map: minifiedContent.map,
}), message.cacheDir);

tmpFile.update(msgLocation, JSON.stringify({
source: minifiedContent.code,
map: minifiedContent.map,
cacheKey,
}));
callback(null, msgLocation);
// eslint-disable-next-line global-require
const uglifier = terser ? require('terser') : require('uglify-js');
minify(source, map, terser || uglifyJS, uglifier)
.then(minifiedContent => {
cache.saveToCache(cacheKey, JSON.stringify({
source: minifiedContent.code,
map: minifiedContent.map,
}), message.cacheDir);

tmpFile.update(msgLocation, JSON.stringify({
source: minifiedContent.code,
map: minifiedContent.map,
cacheKey,
}));
callback(null, msgLocation);
}).catch(e => {
callback(e.message, msgLocation);
});
} catch (e) {
callback(e.message, msgLocation);
}
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('creating a WebpackParallelUglifyPlugin instance w/ both uglify options thr
t.throws(() => {
new WebpackParallelUglifyPlugin({
uglifyJS: {},
uglifyES: {},
terser: {},
});
});
});
Expand All @@ -33,7 +33,7 @@ test('creating a WebpackParallelUglifyPlugin instance with uglify.sourceMap thro

t.throws(() => {
new WebpackParallelUglifyPlugin({
uglifyES: { sourceMap: true },
terser: { sourceMap: true },
});
});
});
Expand Down
6 changes: 3 additions & 3 deletions test/lib/uglifer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function createFakeES6CompilationObject() {
assets: {
'someFile.js': {
source() {
return '() => {}';
return 'const test = () => { function asdf(){console.log("a")} asdf()}; test();';
},
map() {
return null;
Expand Down Expand Up @@ -231,11 +231,11 @@ test('Passing uglifyJS options throws an error when minifying es6', (t) => {
});
});

test('Passing uglifyES options does not throw an error when minifying es6', (t) => {
test('Passing terser options does not throw an error when minifying es6', (t) => {
const es6CompilationObject = createFakeES6CompilationObject();
return processAssets(es6CompilationObject, {
sourceMap: false,
uglifyES: {},
terser: {},
}).then(() => {
assertNoError(es6CompilationObject, t);
});
Expand Down
14 changes: 8 additions & 6 deletions test/lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,20 @@ test.afterEach(() => {

test.serial('minify should not return a map if called with a RawSource object', (t) => {
const { map } = rawSource.sourceAndMap();
const result = minify(codeSource, map, undefined, uglify);
t.is(result.map, undefined);
t.is(result.code, minifiedContent.code); // should produce the same minified content.
return minify(codeSource, map, undefined, uglify).then(result => {
t.is(result.map, undefined);
t.is(result.code, minifiedContent.code); // should produce the same minified content.
});
});

test.serial(
'minify should return a valid source map if called with an OriginalSource object',
(t) => {
const { map } = originalSource.sourceAndMap();
const result = minify(codeSource, map, undefined, uglify);
t.truthy(result.map);
t.is(result.code, minifiedContent.code); // should produce the same minified content.
return minify(codeSource, map, undefined, uglify).then(result => {
t.truthy(result.map);
t.is(result.code, minifiedContent.code); // should produce the same minified content.
});
},
);

Expand Down

0 comments on commit 207d7fa

Please sign in to comment.