Skip to content

Commit

Permalink
feat: uglify mjs by default (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Dec 17, 2018
1 parent e835b19 commit 1644620
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TerserPlugin {
const {
minify,
terserOptions = {},
test = /\.js(\?.*)?$/i,
test = /\.m?js(\?.*)?$/i,
warningsFilter = () => true,
extractComments = false,
sourceMap = false,
Expand Down
14 changes: 14 additions & 0 deletions test/__snapshots__/test-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -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],{
Expand Down
34 changes: 34 additions & 0 deletions test/test-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
});
});
});

0 comments on commit 1644620

Please sign in to comment.