From 8a2c7920b717175e6e368769ec214b34c885256e Mon Sep 17 00:00:00 2001
From: evilebottnawi <sheo13666q@gmail.com>
Date: Mon, 17 Dec 2018 19:11:09 +0300
Subject: [PATCH] feat: uglify `mjs` by default

---
 README.md                                   |  2 +-
 src/index.js                                |  2 +-
 test/__snapshots__/test-option.test.js.snap | 14 +++++++++
 test/test-option.test.js                    | 34 +++++++++++++++++++++
 4 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index ece20031..a7fa649d 100644
--- a/README.md
+++ b/README.md
@@ -49,7 +49,7 @@ And run `webpack` via your preferred method.
 ### `test`
 
 Type: `String|RegExp|Array<String|RegExp>`
-Default: `/\.js(\?.*)?$/i`
+Default: `/\.m?js(\?.*)?$/i`
 
 Test to match files against.
 
diff --git a/src/index.js b/src/index.js
index 8e945520..8827e7f8 100644
--- a/src/index.js
+++ b/src/index.js
@@ -22,7 +22,7 @@ class TerserPlugin {
     const {
       minify,
       terserOptions = {},
-      test = /\.js(\?.*)?$/i,
+      test = /\.m?js(\?.*)?$/i,
       warningsFilter = () => true,
       extractComments = false,
       sourceMap = false,
diff --git a/test/__snapshots__/test-option.test.js.snap b/test/__snapshots__/test-option.test.js.snap
index 88b922d9..3bb33cda 100644
--- a/test/__snapshots__/test-option.test.js.snap
+++ b/test/__snapshots__/test-option.test.js.snap
@@ -1,5 +1,19 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
+exports[`when applied with \`test\` option matches snapshot and uglify \`mjs\`: 4.4.mjs?ver=84fd840418fcb797a306 1`] = `"(window.webpackJsonp=window.webpackJsonp||[]).push([[4],{4:function(n,p,s){\\"use strict\\";s.r(p),p.default=\\"async-dep\\"}}]);"`;
+
+exports[`when applied with \`test\` option matches snapshot and uglify \`mjs\`: AsyncImportExport.mjs?var=84fd840418fcb797a306 1`] = `"!function(e){function t(t){for(var n,o,u=t[0],i=t[1],a=0,l=[];a<u.length;a++)o=u[a],r[o]&&l.push(r[o][0]),r[o]=0;for(n in i)Object.prototype.hasOwnProperty.call(i,n)&&(e[n]=i[n]);for(c&&c(t);l.length;)l.shift()()}var n={},r={0:0};function o(t){if(n[t])return n[t].exports;var r=n[t]={i:t,l:!1,exports:{}};return e[t].call(r.exports,r,r.exports,o),r.l=!0,r.exports}o.e=function(e){var t=[],n=r[e];if(0!==n)if(n)t.push(n[2]);else{var u=new Promise(function(t,o){n=r[e]=[t,o]});t.push(n[2]=u);var i,a=document.getElementsByTagName(\\"head\\")[0],c=document.createElement(\\"script\\");c.charset=\\"utf-8\\",c.timeout=120,o.nc&&c.setAttribute(\\"nonce\\",o.nc),c.src=function(e){return o.p+\\"\\"+e+\\".\\"+({}[e]||e)+\\".mjs?ver=84fd840418fcb797a306\\"}(e),i=function(t){c.onerror=c.onload=null,clearTimeout(l);var n=r[e];if(0!==n){if(n){var o=t&&(\\"load\\"===t.type?\\"missing\\":t.type),u=t&&t.target&&t.target.src,i=new Error(\\"Loading chunk \\"+e+\\" failed.\\\\n(\\"+o+\\": \\"+u+\\")\\");i.type=o,i.request=u,n[1](i)}r[e]=void 0}};var l=setTimeout(function(){i({type:\\"timeout\\",target:c})},12e4);c.onerror=c.onload=i,a.appendChild(c)}return Promise.all(t)},o.m=e,o.c=n,o.d=function(e,t,n){o.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},o.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},o.t=function(e,t){if(1&t&&(e=o(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(o.r(n),Object.defineProperty(n,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var r in e)o.d(n,r,function(t){return e[t]}.bind(null,r));return n},o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,\\"a\\",t),t},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.p=\\"\\",o.oe=function(e){throw console.error(e),e};var u=window.webpackJsonp=window.webpackJsonp||[],i=u.push.bind(u);u.push=t,u=u.slice();for(var a=0;a<u.length;a++)t(u[a]);var c=i;o(o.s=2)}({2:function(e,t,n){\\"use strict\\";n.r(t),n.e(4).then(n.bind(null,4)).then(()=>{console.log(\\"Good\\")}),t.default=\\"Awesome\\"}});"`;
+
+exports[`when applied with \`test\` option matches snapshot and uglify \`mjs\`: errors 1`] = `Array []`;
+
+exports[`when applied with \`test\` option matches snapshot and uglify \`mjs\`: importExport.mjs?var=84fd840418fcb797a306 1`] = `"!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,\\"a\\",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p=\\"\\",r(r.s=3)}({3:function(e,t,r){\\"use strict\\";r.r(t);const n=\\"bar\\";var o=\\"foo\\";t.default=function(){const e=o,t=\`baz\${Math.random()}\`;return()=>({a:e+n+t,b:e,baz:t})}}});"`;
+
+exports[`when applied with \`test\` option matches snapshot and uglify \`mjs\`: js.mjs?var=84fd840418fcb797a306 1`] = `"!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,\\"a\\",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p=\\"\\",n(n.s=0)}([function(e,t){e.exports=function(){console.log(7)}}]);"`;
+
+exports[`when applied with \`test\` option matches snapshot and uglify \`mjs\`: mjs.mjs?var=84fd840418fcb797a306 1`] = `"!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,\\"a\\",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p=\\"\\",r(r.s=1)}([,function(e,t,r){\\"use strict\\";r.r(t);module.exports=function(){console.log(7)}}]);"`;
+
+exports[`when applied with \`test\` option matches snapshot and uglify \`mjs\`: warnings 1`] = `Array []`;
+
 exports[`when applied with \`test\` option matches snapshot for a single \`test\` value ({RegExp}): 4.4.js?ver=9d8052575dfb70071b28 1`] = `
 "(window[\\"webpackJsonp\\"] = window[\\"webpackJsonp\\"] || []).push([[4],{
 
diff --git a/test/test-option.test.js b/test/test-option.test.js
index f6f83ead..72ea0cb7 100644
--- a/test/test-option.test.js
+++ b/test/test-option.test.js
@@ -128,4 +128,38 @@ describe('when applied with `test` option', () => {
       }
     });
   });
+
+  it('matches snapshot and uglify `mjs`', () => {
+    compiler = createCompiler({
+      entry: {
+        js: `${__dirname}/fixtures/entry.js`,
+        mjs: `${__dirname}/fixtures/entry.mjs`,
+        importExport: `${__dirname}/fixtures/import-export/entry.js`,
+        AsyncImportExport: `${__dirname}/fixtures/async-import-export/entry.js`,
+      },
+      output: {
+        path: `${__dirname}/dist`,
+        filename: '[name].mjs?var=[hash]',
+        chunkFilename: '[id].[name].mjs?ver=[hash]',
+      },
+    });
+
+    new TerserPlugin().apply(compiler);
+
+    return compile(compiler).then((stats) => {
+      const errors = stats.compilation.errors.map(cleanErrorStack);
+      const warnings = stats.compilation.warnings.map(cleanErrorStack);
+
+      expect(errors).toMatchSnapshot('errors');
+      expect(warnings).toMatchSnapshot('warnings');
+
+      for (const file in stats.compilation.assets) {
+        if (
+          Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)
+        ) {
+          expect(stats.compilation.assets[file].source()).toMatchSnapshot(file);
+        }
+      }
+    });
+  });
 });