Skip to content

Commit

Permalink
fix: terser options for es module
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Oct 23, 2020
1 parent ddff508 commit a12730f
Show file tree
Hide file tree
Showing 9 changed files with 509 additions and 469 deletions.
675 changes: 325 additions & 350 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
"webpack": "^5.1.0"
},
"dependencies": {
"jest-worker": "^26.5.0",
"jest-worker": "^26.6.1",
"p-limit": "^3.0.2",
"schema-utils": "^3.0.0",
"serialize-javascript": "^5.0.1",
"source-map": "^0.6.1",
"terser": "^5.3.5"
"terser": "^5.3.8"
},
"devDependencies": {
"@babel/cli": "^7.12.1",
Expand All @@ -56,24 +56,24 @@
"@commitlint/config-conventional": "^11.0.0",
"@webpack-contrib/defaults": "^6.3.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^26.5.2",
"babel-jest": "^26.6.1",
"copy-webpack-plugin": "^6.2.1",
"cross-env": "^7.0.2",
"del": "^6.0.0",
"del-cli": "^3.0.1",
"eslint": "^7.11.0",
"eslint-config-prettier": "^6.13.0",
"eslint-config-prettier": "^6.14.0",
"eslint-plugin-import": "^2.22.1",
"file-loader": "^6.1.1",
"husky": "^4.3.0",
"jest": "^26.5.3",
"jest": "^26.6.1",
"lint-staged": "^10.4.2",
"memfs": "^3.2.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.1.2",
"standard-version": "^9.0.0",
"uglify-js": "^3.11.2",
"webpack": "^5.1.3",
"uglify-js": "^3.11.3",
"webpack": "^5.2.0",
"worker-loader": "^3.0.5"
},
"keywords": [
Expand Down
25 changes: 12 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,23 +197,29 @@ class TerserPlugin {
let output = await cache.getPromise(name, eTag);

if (!output) {
const minimizerOptions = {
const options = {
name,
input,
inputSourceMap,
minify: this.options.minify,
minimizerOptions: this.options.terserOptions,
minimizerOptions: { ...this.options.terserOptions },
extractComments: this.options.extractComments,
};

if (/\.mjs(\?.*)?$/i.test(name)) {
this.options.terserOptions.module = true;
if (typeof options.minimizerOptions.module === 'undefined') {
if (typeof info.javascriptModule !== 'undefined') {
options.minimizerOptions.module = info.javascriptModule;
} else if (/\.mjs(\?.*)?$/i.test(name)) {
options.minimizerOptions.module = true;
} else if (/\.cjs(\?.*)?$/i.test(name)) {
options.minimizerOptions.module = false;
}
}

try {
output = await (worker
? worker.transform(serialize(minimizerOptions))
: minifyFn(minimizerOptions));
? worker.transform(serialize(options))
: minifyFn(options));
} catch (error) {
compilation.errors.push(
TerserPlugin.buildError(
Expand Down Expand Up @@ -426,13 +432,6 @@ class TerserPlugin {
apply(compiler) {
const { output } = compiler.options;

if (
typeof this.options.terserOptions.module === 'undefined' &&
typeof output.module !== 'undefined'
) {
this.options.terserOptions.module = output.module;
}

if (typeof this.options.terserOptions.ecma === 'undefined') {
this.options.terserOptions.ecma = TerserPlugin.getEcmaVersion(
output.environment || {}
Expand Down
89 changes: 71 additions & 18 deletions test/TerserPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -715,29 +715,82 @@ describe('TerserPlugin', () => {
process.stderr.write = stderrWrite;
});

it('should work with "copy-webpack-plugin"', async () => {
const compiler = getCompiler();

new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, './fixtures/copy.js'),
it('should work with ES modules', async () => {
const multiCompiler = getCompiler([
{
mode: 'production',
bail: true,
entry: path.resolve(__dirname, './fixtures/entry.js'),
optimization: {
minimize: false,
},
{
from: path.resolve(__dirname, './fixtures/copy.cjs'),
output: {
pathinfo: false,
path: path.resolve(__dirname, 'dist/a'),
filename: '[name].js',
chunkFilename: '[id].[name].js',
},
{
from: path.resolve(__dirname, './fixtures/copy.mjs'),
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, './fixtures/copy.js'),
},
{
from: path.resolve(__dirname, './fixtures/copy.cjs'),
},
{
from: path.resolve(__dirname, './fixtures/copy.mjs'),
},
],
}),
new TerserPlugin(),
],
},
{
mode: 'production',
bail: true,
entry: path.resolve(__dirname, './fixtures/entry.js'),
optimization: {
minimize: false,
},
],
}).apply(compiler);
new TerserPlugin().apply(compiler);
output: {
pathinfo: false,
path: path.resolve(__dirname, 'dist/b'),
filename: '[name].js',
chunkFilename: '[id].[name].js',
},
experiments: {
outputModule: true,
},
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, './fixtures/copy.js'),
},
{
from: path.resolve(__dirname, './fixtures/copy.cjs'),
},
{
from: path.resolve(__dirname, './fixtures/copy.mjs'),
},
],
}),
new TerserPlugin(),
],
},
]);

const stats = await compile(compiler);
const multiStats = await compile(multiCompiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
multiStats.stats.forEach((stats, index) => {
expect(
readsAssets(multiCompiler.compilers[index], stats)
).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});
});

it('should work with child compilation', async () => {
Expand Down
37 changes: 25 additions & 12 deletions test/__snapshots__/TerserPlugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ exports[`TerserPlugin should work and do not use memory cache when the "cache" o

exports[`TerserPlugin should work and generate real content hash: assets 1`] = `
Object {
"598.d955a8689b2acafd4711.4e73bd5971933bc3926e.c69b8ee272ff8c78063e.js": "(self.webpackChunkterser_webpack_plugin=self.webpackChunkterser_webpack_plugin||[]).push([[598],{598:(e,s,p)=>{\\"use strict\\";p.r(s),p.d(s,{default:()=>c});const c=\\"async-dep\\"}}]);",
"app.89d50daa9391a6f7b515.6155aef4b60989a7b3d8.c69b8ee272ff8c78063e.js": "(()=>{\\"use strict\\";var e,r,t={},o={};function n(e){if(o[e])return o[e].exports;var r=o[e]={exports:{}};return t[e](r,r.exports,n),r.exports}n.m=t,n.d=(e,r)=>{for(var t in r)n.o(r,t)&&!n.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},n.f={},n.e=e=>Promise.all(Object.keys(n.f).reduce(((r,t)=>(n.f[t](e,r),r)),[])),n.u=e=>e+\\".d955a8689b2acafd4711.4e73bd5971933bc3926e.\\"+n.h()+\\".js\\",n.h=()=>\\"c69b8ee272ff8c78063e\\",n.g=function(){if(\\"object\\"==typeof globalThis)return globalThis;try{return this||new Function(\\"return this\\")()}catch(e){if(\\"object\\"==typeof window)return window}}(),n.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),e={},r=\\"terser-webpack-plugin:\\",n.l=(t,o,a)=>{if(e[t])e[t].push(o);else{var i,c;if(void 0!==a)for(var l=document.getElementsByTagName(\\"script\\"),u=0;u<l.length;u++){var s=l[u];if(s.getAttribute(\\"src\\")==t||s.getAttribute(\\"data-webpack\\")==r+a){i=s;break}}i||(c=!0,(i=document.createElement(\\"script\\")).charset=\\"utf-8\\",i.timeout=120,n.nc&&i.setAttribute(\\"nonce\\",n.nc),i.setAttribute(\\"data-webpack\\",r+a),i.src=t),e[t]=[o];var p=(r,o)=>{i.onerror=i.onload=null,clearTimeout(d);var n=e[t];if(delete e[t],i.parentNode&&i.parentNode.removeChild(i),n&&n.forEach((e=>e(o))),r)return r(o)},d=setTimeout(p.bind(null,void 0,{type:\\"timeout\\",target:i}),12e4);i.onerror=p.bind(null,i.onerror),i.onload=p.bind(null,i.onload),c&&document.head.appendChild(i)}},n.r=e=>{\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},(()=>{var e;n.g.importScripts&&(e=n.g.location+\\"\\");var r=n.g.document;if(!e&&r&&(r.currentScript&&(e=r.currentScript.src),!e)){var t=r.getElementsByTagName(\\"script\\");t.length&&(e=t[t.length-1].src)}if(!e)throw new Error(\\"Automatic publicPath is not supported in this browser\\");e=e.replace(/#.*$/,\\"\\").replace(/\\\\?.*$/,\\"\\").replace(/\\\\/[^\\\\/]+$/,\\"/\\"),n.p=e})(),(()=>{var e={143:0};n.f.j=(r,t)=>{var o=n.o(e,r)?e[r]:void 0;if(0!==o)if(o)t.push(o[2]);else{var a=new Promise(((t,n)=>{o=e[r]=[t,n]}));t.push(o[2]=a);var i=n.p+n.u(r),c=new Error;n.l(i,(t=>{if(n.o(e,r)&&(0!==(o=e[r])&&(e[r]=void 0),o)){var a=t&&(\\"load\\"===t.type?\\"missing\\":t.type),i=t&&t.target&&t.target.src;c.message=\\"Loading chunk \\"+r+\\" failed.\\\\n(\\"+a+\\": \\"+i+\\")\\",c.name=\\"ChunkLoadError\\",c.type=a,c.request=i,o[1](c)}}),\\"chunk-\\"+r)}};var r=self.webpackChunkterser_webpack_plugin=self.webpackChunkterser_webpack_plugin||[],t=r.push.bind(r);r.push=r=>{for(var o,a,[i,c,l]=r,u=0,s=[];u<i.length;u++)a=i[u],n.o(e,a)&&e[a]&&s.push(e[a][0]),e[a]=0;for(o in c)n.o(c,o)&&(n.m[o]=c[o]);for(l&&l(n),t(r);s.length;)s.shift()()}})(),n.e(598).then(n.bind(n,598)).then((()=>{console.log(\\"Good\\")}))})();",
"598.d955a8689b2acafd4711.f8f55f964433398d4340.f7adfd362df949719a5d.js": "(self.webpackChunkterser_webpack_plugin=self.webpackChunkterser_webpack_plugin||[]).push([[598],{598:(e,s,p)=>{\\"use strict\\";p.r(s),p.d(s,{default:()=>c});const c=\\"async-dep\\"}}]);",
"app.14fa452e8ecddbca4e68.099e2102f06bcca79513.f7adfd362df949719a5d.js": "(()=>{\\"use strict\\";var e,r,t={},o={};function n(e){if(o[e])return o[e].exports;var r=o[e]={exports:{}};return t[e](r,r.exports,n),r.exports}n.m=t,n.d=(e,r)=>{for(var t in r)n.o(r,t)&&!n.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},n.f={},n.e=e=>Promise.all(Object.keys(n.f).reduce(((r,t)=>(n.f[t](e,r),r)),[])),n.u=e=>e+\\".d955a8689b2acafd4711.f8f55f964433398d4340.\\"+n.h()+\\".js\\",n.h=()=>\\"f7adfd362df949719a5d\\",n.g=function(){if(\\"object\\"==typeof globalThis)return globalThis;try{return this||new Function(\\"return this\\")()}catch(e){if(\\"object\\"==typeof window)return window}}(),n.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),e={},r=\\"terser-webpack-plugin:\\",n.l=(t,o,a)=>{if(e[t])e[t].push(o);else{var i,l;if(void 0!==a)for(var u=document.getElementsByTagName(\\"script\\"),c=0;c<u.length;c++){var s=u[c];if(s.getAttribute(\\"src\\")==t||s.getAttribute(\\"data-webpack\\")==r+a){i=s;break}}i||(l=!0,(i=document.createElement(\\"script\\")).charset=\\"utf-8\\",i.timeout=120,n.nc&&i.setAttribute(\\"nonce\\",n.nc),i.setAttribute(\\"data-webpack\\",r+a),i.src=t),e[t]=[o];var p=(r,o)=>{i.onerror=i.onload=null,clearTimeout(d);var n=e[t];if(delete e[t],i.parentNode&&i.parentNode.removeChild(i),n&&n.forEach((e=>e(o))),r)return r(o)},d=setTimeout(p.bind(null,void 0,{type:\\"timeout\\",target:i}),12e4);i.onerror=p.bind(null,i.onerror),i.onload=p.bind(null,i.onload),l&&document.head.appendChild(i)}},n.r=e=>{\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},(()=>{var e;n.g.importScripts&&(e=n.g.location+\\"\\");var r=n.g.document;if(!e&&r&&(r.currentScript&&(e=r.currentScript.src),!e)){var t=r.getElementsByTagName(\\"script\\");t.length&&(e=t[t.length-1].src)}if(!e)throw new Error(\\"Automatic publicPath is not supported in this browser\\");e=e.replace(/#.*$/,\\"\\").replace(/\\\\?.*$/,\\"\\").replace(/\\\\/[^\\\\/]+$/,\\"/\\"),n.p=e})(),(()=>{var e={143:0};n.f.j=(r,t)=>{var o=n.o(e,r)?e[r]:void 0;if(0!==o)if(o)t.push(o[2]);else{var a=new Promise(((t,n)=>{o=e[r]=[t,n]}));t.push(o[2]=a);var i=n.p+n.u(r),l=new Error;n.l(i,(t=>{if(n.o(e,r)&&(0!==(o=e[r])&&(e[r]=void 0),o)){var a=t&&(\\"load\\"===t.type?\\"missing\\":t.type),i=t&&t.target&&t.target.src;l.message=\\"Loading chunk \\"+r+\\" failed.\\\\n(\\"+a+\\": \\"+i+\\")\\",l.name=\\"ChunkLoadError\\",l.type=a,l.request=i,o[1](l)}}),\\"chunk-\\"+r)}};var r=self.webpackChunkterser_webpack_plugin=self.webpackChunkterser_webpack_plugin||[],t=r.push.bind(r);r.push=r=>{for(var o,a,[i,l,u]=r,c=0,s=[];c<i.length;c++)a=i[c],n.o(e,a)&&e[a]&&s.push(e[a][0]),e[a]=0;for(o in l)n.o(l,o)&&(n.m[o]=l[o]);for(u&&u(n),t(r);s.length;)s.shift()()}})(),n.e(598).then(n.bind(n,598)).then((()=>{console.log(\\"Good\\")}))})();",
}
`;

Expand Down Expand Up @@ -589,7 +589,18 @@ exports[`TerserPlugin should work with "asset" module type: errors 1`] = `Array

exports[`TerserPlugin should work with "asset" module type: warnings 1`] = `Array []`;

exports[`TerserPlugin should work with "copy-webpack-plugin": assets 1`] = `
exports[`TerserPlugin should work with "file-loader": assets 1`] = `
Object {
"main.js": "(()=>{\\"use strict\\";var t={};t.g=function(){if(\\"object\\"==typeof globalThis)return globalThis;try{return this||new Function(\\"return this\\")()}catch(t){if(\\"object\\"==typeof window)return window}}(),(()=>{var r;t.g.importScripts&&(r=t.g.location+\\"\\");var e=t.g.document;if(!r&&e&&(e.currentScript&&(r=e.currentScript.src),!r)){var i=e.getElementsByTagName(\\"script\\");i.length&&(r=i[i.length-1].src)}if(!r)throw new Error(\\"Automatic publicPath is not supported in this browser\\");r=r.replace(/#.*$/,\\"\\").replace(/\\\\?.*$/,\\"\\").replace(/\\\\/[^\\\\/]+$/,\\"/\\"),t.p=r})();const r=t.p+\\"test/fixtures/emitted.js\\";console.log(12,r)})();",
"test/fixtures/emitted.js": "console.log(\\"HERE\\");",
}
`;

exports[`TerserPlugin should work with "file-loader": errors 1`] = `Array []`;

exports[`TerserPlugin should work with "file-loader": warnings 1`] = `Array []`;

exports[`TerserPlugin should work with ES modules: assets 1`] = `
Object {
"copy.cjs": "var foo=12;console.log(foo);",
"copy.js": "var foo=12;console.log(foo);",
Expand All @@ -598,20 +609,22 @@ Object {
}
`;

exports[`TerserPlugin should work with "copy-webpack-plugin": errors 1`] = `Array []`;

exports[`TerserPlugin should work with "copy-webpack-plugin": warnings 1`] = `Array []`;

exports[`TerserPlugin should work with "file-loader": assets 1`] = `
exports[`TerserPlugin should work with ES modules: assets 2`] = `
Object {
"main.js": "(()=>{\\"use strict\\";var t={};t.g=function(){if(\\"object\\"==typeof globalThis)return globalThis;try{return this||new Function(\\"return this\\")()}catch(t){if(\\"object\\"==typeof window)return window}}(),(()=>{var r;t.g.importScripts&&(r=t.g.location+\\"\\");var e=t.g.document;if(!r&&e&&(e.currentScript&&(r=e.currentScript.src),!r)){var i=e.getElementsByTagName(\\"script\\");i.length&&(r=i[i.length-1].src)}if(!r)throw new Error(\\"Automatic publicPath is not supported in this browser\\");r=r.replace(/#.*$/,\\"\\").replace(/\\\\?.*$/,\\"\\").replace(/\\\\/[^\\\\/]+$/,\\"/\\"),t.p=r})();const r=t.p+\\"test/fixtures/emitted.js\\";console.log(12,r)})();",
"test/fixtures/emitted.js": "console.log(\\"HERE\\");",
"copy.cjs": "var foo=12;console.log(foo);",
"copy.js": "var foo=12;console.log(foo);",
"copy.mjs": "console.log(12);",
"main.js": "var r={791:r=>{r.exports=function(){console.log(7)}}},o={};!function t(e){if(o[e])return o[e].exports;var n=o[e]={exports:{}};return r[e](n,n.exports,t),n.exports}(791);",
}
`;

exports[`TerserPlugin should work with "file-loader": errors 1`] = `Array []`;
exports[`TerserPlugin should work with ES modules: errors 1`] = `Array []`;

exports[`TerserPlugin should work with "file-loader": warnings 1`] = `Array []`;
exports[`TerserPlugin should work with ES modules: errors 2`] = `Array []`;

exports[`TerserPlugin should work with ES modules: warnings 1`] = `Array []`;

exports[`TerserPlugin should work with ES modules: warnings 2`] = `Array []`;

exports[`TerserPlugin should work with child compilation: assets 1`] = `
Object {
Expand Down
Loading

0 comments on commit a12730f

Please sign in to comment.