Skip to content

Commit

Permalink
fix: compatibility with webpack@5
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Sep 15, 2020
1 parent 0e21d92 commit f5bd8f8
Show file tree
Hide file tree
Showing 8 changed files with 358 additions and 133 deletions.
29 changes: 24 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,26 @@ class TerserPlugin {
}, Promise.resolve());
}

static getEcmaVersion(environment) {
// ES 6th
if (
environment.arrowFunction ||
environment.const ||
environment.destructuring ||
environment.forOf ||
environment.module
) {
return 2015;
}

// ES 11th
if (environment.bigIntLiteral || environment.dynamicImport) {
return 2020;
}

return 5;
}

apply(compiler) {
const { devtool, output, plugins } = compiler.options;

Expand Down Expand Up @@ -596,11 +616,10 @@ class TerserPlugin {
this.options.terserOptions.module = output.module;
}

if (
typeof this.options.terserOptions.ecma === 'undefined' &&
typeof output.ecmaVersion !== 'undefined'
) {
this.options.terserOptions.ecma = output.ecmaVersion;
if (typeof this.options.terserOptions.ecma === 'undefined') {
this.options.terserOptions.ecma = TerserPlugin.getEcmaVersion(
output.environment || {}
);
}

const pluginName = this.constructor.name;
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack5
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.7a5aa966f89edb794c2b.79f16c1f21fad67b6c12.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.8b91e181e1be64da9007.f93bd1e36ba6b34bdc22.79f16c1f21fad67b6c12.js": "(()=>{\\"use strict\\";var e,r,t={},o={};function a(e){if(o[e])return o[e].exports;var r=o[e]={exports:{}};return t[e](r,r.exports,a),r.exports}a.m=t,a.d=(e,r)=>{for(var t in r)a.o(r,t)&&!a.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},a.f={},a.e=e=>Promise.all(Object.keys(a.f).reduce((r,t)=>(a.f[t](e,r),r),[])),a.u=e=>e+\\".d955a8689b2acafd4711.7a5aa966f89edb794c2b.\\"+a.h()+\\".js\\",a.h=()=>\\"79f16c1f21fad67b6c12\\",a.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),e={},r=\\"terser-webpack-plugin:\\",a.l=(t,o,n)=>{if(e[t])e[t].push(o);else{var i,l;if(void 0!==n)for(var u=document.getElementsByTagName(\\"script\\"),s=0;s<u.length;s++){var d=u[s];if(d.getAttribute(\\"src\\")==t||d.getAttribute(\\"data-webpack\\")==r+n){i=d;break}}i||(l=!0,(i=document.createElement(\\"script\\")).charset=\\"utf-8\\",i.timeout=120,a.nc&&i.setAttribute(\\"nonce\\",a.nc),i.setAttribute(\\"data-webpack\\",r+n),i.src=t),e[t]=[o];var c=(r,o)=>{i.onerror=i.onload=null,clearTimeout(p);var a=e[t];if(delete e[t],i.parentNode&&i.parentNode.removeChild(i),a&&a.forEach(e=>e(o)),r)return r(o)},p=setTimeout(c.bind(null,void 0,{type:\\"timeout\\",target:i}),12e4);i.onerror=c.bind(null,i.onerror),i.onload=c.bind(null,i.onload),l&&document.head.appendChild(i)}},a.r=e=>{\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},a.p=\\"\\",(()=>{var e={143:0};a.f.j=(r,t)=>{var o=a.o(e,r)?e[r]:void 0;if(0!==o)if(o)t.push(o[2]);else{var n=new Promise((t,a)=>{o=e[r]=[t,a]});t.push(o[2]=n);var i=a.p+a.u(r),l=new Error;a.l(i,t=>{if(a.o(e,r)&&(0!==(o=e[r])&&(e[r]=void 0),o)){var n=t&&(\\"load\\"===t.type?\\"missing\\":t.type),i=t&&t.target&&t.target.src;l.message=\\"Loading chunk \\"+r+\\" failed.\\\\n(\\"+n+\\": \\"+i+\\")\\",l.name=\\"ChunkLoadError\\",l.type=n,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,n,[i,l,u]=r,s=0,d=[];s<i.length;s++)n=i[s],a.o(e,n)&&e[n]&&d.push(e[n][0]),e[n]=0;for(o in l)a.o(l,o)&&(a.m[o]=l[o]);for(u&&u(a),t(r);d.length;)d.shift()()}})(),a.e(598).then(a.bind(a,598)).then(()=>{console.log(\\"Good\\")})})();",
"598.d955a8689b2acafd4711.89807e0f911d1e455979.1ee5b2af6f83a763b1e8.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.6eecc533b1d9f74bcbbf.20c71ff8e3dadc3fcbd8.1ee5b2af6f83a763b1e8.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.89807e0f911d1e455979.\\"+n.h()+\\".js\\",n.h=()=>\\"1ee5b2af6f83a763b1e8\\",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\\"),s=0;s<u.length;s++){var d=u[s];if(d.getAttribute(\\"src\\")==t||d.getAttribute(\\"data-webpack\\")==r+a){i=d;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(f);var n=e[t];if(delete e[t],i.parentNode&&i.parentNode.removeChild(i),n&&n.forEach(e=>e(o)),r)return r(o)},f=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})},n.p=\\"\\",(()=>{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,s=0,d=[];s<i.length;s++)a=i[s],n.o(e,a)&&e[a]&&d.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);d.length;)d.shift()()}})(),n.e(598).then(n.bind(n,598)).then(()=>{console.log(\\"Good\\")})})();",
}
`;

Expand Down
Loading

0 comments on commit f5bd8f8

Please sign in to comment.