diff --git a/test/__snapshots__/ecma.test.js.snap b/test/__snapshots__/ecma.test.js.snap deleted file mode 100644 index 15a3f5e2..00000000 --- a/test/__snapshots__/ecma.test.js.snap +++ /dev/null @@ -1,346 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`when applied with uglifyOptions.ecma matches snapshot for ecma 5: errors 1`] = `Array []`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for ecma 5: main.js 1`] = ` -"webpackJsonp([ 0 ], [ function(module, exports) { - var Person = { - firstName: null, - lastName: null - }, Employee = Object.create(Person, { - id: { - value: null, - enumerable: !0, - configurable: !0, - writable: !0 - } - }), Manager = Object.create(Employee, { - department: { - value: null, - enumerable: !0, - configurable: !0, - writable: !0 - } - }); - module.exports = { - Person: Person, - Employee: Employee, - Manager: Manager - }; -} ], [ 0 ]);" -`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for ecma 5: manifest.js 1`] = ` -"!function(modules) { - var parentJsonpFunction = window.webpackJsonp; - window.webpackJsonp = function(chunkIds, moreModules, executeModules) { - for (var moduleId, chunkId, result, i = 0, resolves = []; i < chunkIds.length; i++) chunkId = chunkIds[i], - installedChunks[chunkId] && resolves.push(installedChunks[chunkId][0]), installedChunks[chunkId] = 0; - for (moduleId in moreModules) Object.prototype.hasOwnProperty.call(moreModules, moduleId) && (modules[moduleId] = moreModules[moduleId]); - for (parentJsonpFunction && parentJsonpFunction(chunkIds, moreModules, executeModules); resolves.length; ) resolves.shift()(); - if (executeModules) for (i = 0; i < executeModules.length; i++) result = __webpack_require__(__webpack_require__.s = executeModules[i]); - return result; - }; - var installedModules = {}, installedChunks = { - 1: 0 - }; - function __webpack_require__(moduleId) { - if (installedModules[moduleId]) return installedModules[moduleId].exports; - var module = installedModules[moduleId] = { - i: moduleId, - l: !1, - exports: {} - }; - return modules[moduleId].call(module.exports, module, module.exports, __webpack_require__), - module.l = !0, module.exports; - } - __webpack_require__.m = modules, __webpack_require__.c = installedModules, __webpack_require__.d = function(exports, name, getter) { - __webpack_require__.o(exports, name) || Object.defineProperty(exports, name, { - configurable: !1, - enumerable: !0, - get: getter - }); - }, __webpack_require__.n = function(module) { - var getter = module && module.__esModule ? function() { - return module.default; - } : function() { - return module; - }; - return __webpack_require__.d(getter, \\"a\\", getter), getter; - }, __webpack_require__.o = function(object, property) { - return Object.prototype.hasOwnProperty.call(object, property); - }, __webpack_require__.p = \\"\\", __webpack_require__.oe = function(err) { - throw console.error(err), err; - }; -}([]);" -`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for ecma 5: warnings 1`] = `Array []`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for ecma 6: errors 1`] = `Array []`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for ecma 6: main.js 1`] = ` -"webpackJsonp([ 0 ], [ function(module, exports) { - module.exports = class { - constructor(principal, years, rate) { - this.principal = principal, this.years = years, this.rate = rate; - } - get monthlyPayment() { - let monthlyRate = this.rate / 100 / 12; - return this.principal * monthlyRate / (1 - Math.pow(1 / (1 + monthlyRate), 12 * this.years)); - } - get amortization() { - let monthlyPayment = this.monthlyPayment, monthlyRate = this.rate / 100 / 12, balance = this.principal, amortization = []; - for (let y = 0; y < this.years; y++) { - let interestY = 0, principalY = 0; - for (let m = 0; m < 12; m++) { - let interestM = balance * monthlyRate, principalM = monthlyPayment - interestM; - interestY += interestM, principalY += principalM, balance -= principalM; - } - amortization.push({ - principalY, - interestY, - balance - }); - } - return amortization; - } - }; -} ], [ 0 ]);" -`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for ecma 6: manifest.js 1`] = ` -"!function(modules) { - var parentJsonpFunction = window.webpackJsonp; - window.webpackJsonp = function(chunkIds, moreModules, executeModules) { - for (var moduleId, chunkId, result, i = 0, resolves = []; i < chunkIds.length; i++) chunkId = chunkIds[i], - installedChunks[chunkId] && resolves.push(installedChunks[chunkId][0]), installedChunks[chunkId] = 0; - for (moduleId in moreModules) Object.prototype.hasOwnProperty.call(moreModules, moduleId) && (modules[moduleId] = moreModules[moduleId]); - for (parentJsonpFunction && parentJsonpFunction(chunkIds, moreModules, executeModules); resolves.length; ) resolves.shift()(); - if (executeModules) for (i = 0; i < executeModules.length; i++) result = __webpack_require__(__webpack_require__.s = executeModules[i]); - return result; - }; - var installedModules = {}, installedChunks = { - 1: 0 - }; - function __webpack_require__(moduleId) { - if (installedModules[moduleId]) return installedModules[moduleId].exports; - var module = installedModules[moduleId] = { - i: moduleId, - l: !1, - exports: {} - }; - return modules[moduleId].call(module.exports, module, module.exports, __webpack_require__), - module.l = !0, module.exports; - } - __webpack_require__.m = modules, __webpack_require__.c = installedModules, __webpack_require__.d = function(exports, name, getter) { - __webpack_require__.o(exports, name) || Object.defineProperty(exports, name, { - configurable: !1, - enumerable: !0, - get: getter - }); - }, __webpack_require__.n = function(module) { - var getter = module && module.__esModule ? function() { - return module.default; - } : function() { - return module; - }; - return __webpack_require__.d(getter, \\"a\\", getter), getter; - }, __webpack_require__.o = function(object, property) { - return Object.prototype.hasOwnProperty.call(object, property); - }, __webpack_require__.p = \\"\\", __webpack_require__.oe = function(err) { - throw console.error(err), err; - }; -}([]);" -`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for ecma 6: warnings 1`] = `Array []`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for ecma 7: errors 1`] = `Array []`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for ecma 7: main.js 1`] = ` -"webpackJsonp([ 0 ], [ function(module, exports) { - module.exports = function(base, exponent) { - return base ** exponent; - }; -} ], [ 0 ]);" -`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for ecma 7: manifest.js 1`] = ` -"!function(modules) { - var parentJsonpFunction = window.webpackJsonp; - window.webpackJsonp = function(chunkIds, moreModules, executeModules) { - for (var moduleId, chunkId, result, i = 0, resolves = []; i < chunkIds.length; i++) chunkId = chunkIds[i], - installedChunks[chunkId] && resolves.push(installedChunks[chunkId][0]), installedChunks[chunkId] = 0; - for (moduleId in moreModules) Object.prototype.hasOwnProperty.call(moreModules, moduleId) && (modules[moduleId] = moreModules[moduleId]); - for (parentJsonpFunction && parentJsonpFunction(chunkIds, moreModules, executeModules); resolves.length; ) resolves.shift()(); - if (executeModules) for (i = 0; i < executeModules.length; i++) result = __webpack_require__(__webpack_require__.s = executeModules[i]); - return result; - }; - var installedModules = {}, installedChunks = { - 1: 0 - }; - function __webpack_require__(moduleId) { - if (installedModules[moduleId]) return installedModules[moduleId].exports; - var module = installedModules[moduleId] = { - i: moduleId, - l: !1, - exports: {} - }; - return modules[moduleId].call(module.exports, module, module.exports, __webpack_require__), - module.l = !0, module.exports; - } - __webpack_require__.m = modules, __webpack_require__.c = installedModules, __webpack_require__.d = function(exports, name, getter) { - __webpack_require__.o(exports, name) || Object.defineProperty(exports, name, { - configurable: !1, - enumerable: !0, - get: getter - }); - }, __webpack_require__.n = function(module) { - var getter = module && module.__esModule ? function() { - return module.default; - } : function() { - return module; - }; - return __webpack_require__.d(getter, \\"a\\", getter), getter; - }, __webpack_require__.o = function(object, property) { - return Object.prototype.hasOwnProperty.call(object, property); - }, __webpack_require__.p = \\"\\", __webpack_require__.oe = function(err) { - throw console.error(err), err; - }; -}([]);" -`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for ecma 7: warnings 1`] = `Array []`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for ecma 8: errors 1`] = `Array []`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for ecma 8: main.js 1`] = ` -"webpackJsonp([ 0 ], [ function(module, exports) { - module.exports = async function(url) { - try { - let request = await fetch(url), text = await request.text(); - return JSON.parse(text); - } catch (error) { - console.log(\`ERROR: \${error.stack}\`); - } - }; -} ], [ 0 ]);" -`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for ecma 8: manifest.js 1`] = ` -"!function(modules) { - var parentJsonpFunction = window.webpackJsonp; - window.webpackJsonp = function(chunkIds, moreModules, executeModules) { - for (var moduleId, chunkId, result, i = 0, resolves = []; i < chunkIds.length; i++) chunkId = chunkIds[i], - installedChunks[chunkId] && resolves.push(installedChunks[chunkId][0]), installedChunks[chunkId] = 0; - for (moduleId in moreModules) Object.prototype.hasOwnProperty.call(moreModules, moduleId) && (modules[moduleId] = moreModules[moduleId]); - for (parentJsonpFunction && parentJsonpFunction(chunkIds, moreModules, executeModules); resolves.length; ) resolves.shift()(); - if (executeModules) for (i = 0; i < executeModules.length; i++) result = __webpack_require__(__webpack_require__.s = executeModules[i]); - return result; - }; - var installedModules = {}, installedChunks = { - 1: 0 - }; - function __webpack_require__(moduleId) { - if (installedModules[moduleId]) return installedModules[moduleId].exports; - var module = installedModules[moduleId] = { - i: moduleId, - l: !1, - exports: {} - }; - return modules[moduleId].call(module.exports, module, module.exports, __webpack_require__), - module.l = !0, module.exports; - } - __webpack_require__.m = modules, __webpack_require__.c = installedModules, __webpack_require__.d = function(exports, name, getter) { - __webpack_require__.o(exports, name) || Object.defineProperty(exports, name, { - configurable: !1, - enumerable: !0, - get: getter - }); - }, __webpack_require__.n = function(module) { - var getter = module && module.__esModule ? function() { - return module.default; - } : function() { - return module; - }; - return __webpack_require__.d(getter, \\"a\\", getter), getter; - }, __webpack_require__.o = function(object, property) { - return Object.prototype.hasOwnProperty.call(object, property); - }, __webpack_require__.p = \\"\\", __webpack_require__.oe = function(err) { - throw console.error(err), err; - }; -}([]);" -`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for ecma 8: warnings 1`] = `Array []`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for import and export: errors 1`] = `Array []`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for import and export: main.js 1`] = ` -"webpackJsonp([ 0 ], [ function(module, __webpack_exports__, __webpack_require__) { - \\"use strict\\"; - Object.defineProperty(__webpack_exports__, \\"__esModule\\", { - value: !0 - }); - var __WEBPACK_IMPORTED_MODULE_0__dep__ = __webpack_require__(1); - __webpack_exports__.default = function() { - const b = __WEBPACK_IMPORTED_MODULE_0__dep__.b, baz = \`baz\${Math.random()}\`; - return () => ({ - a: b + __WEBPACK_IMPORTED_MODULE_0__dep__.a + baz, - b: b, - baz: baz - }); - }; -}, function(module, __webpack_exports__, __webpack_require__) { - \\"use strict\\"; - __webpack_exports__.a = \\"bar\\", __webpack_exports__.b = \\"foo\\"; -} ], [ 0 ]);" -`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for import and export: manifest.js 1`] = ` -"!function(modules) { - var parentJsonpFunction = window.webpackJsonp; - window.webpackJsonp = function(chunkIds, moreModules, executeModules) { - for (var moduleId, chunkId, result, i = 0, resolves = []; i < chunkIds.length; i++) chunkId = chunkIds[i], - installedChunks[chunkId] && resolves.push(installedChunks[chunkId][0]), installedChunks[chunkId] = 0; - for (moduleId in moreModules) Object.prototype.hasOwnProperty.call(moreModules, moduleId) && (modules[moduleId] = moreModules[moduleId]); - for (parentJsonpFunction && parentJsonpFunction(chunkIds, moreModules, executeModules); resolves.length; ) resolves.shift()(); - if (executeModules) for (i = 0; i < executeModules.length; i++) result = __webpack_require__(__webpack_require__.s = executeModules[i]); - return result; - }; - var installedModules = {}, installedChunks = { - 1: 0 - }; - function __webpack_require__(moduleId) { - if (installedModules[moduleId]) return installedModules[moduleId].exports; - var module = installedModules[moduleId] = { - i: moduleId, - l: !1, - exports: {} - }; - return modules[moduleId].call(module.exports, module, module.exports, __webpack_require__), - module.l = !0, module.exports; - } - __webpack_require__.m = modules, __webpack_require__.c = installedModules, __webpack_require__.d = function(exports, name, getter) { - __webpack_require__.o(exports, name) || Object.defineProperty(exports, name, { - configurable: !1, - enumerable: !0, - get: getter - }); - }, __webpack_require__.n = function(module) { - var getter = module && module.__esModule ? function() { - return module.default; - } : function() { - return module; - }; - return __webpack_require__.d(getter, \\"a\\", getter), getter; - }, __webpack_require__.o = function(object, property) { - return Object.prototype.hasOwnProperty.call(object, property); - }, __webpack_require__.p = \\"\\", __webpack_require__.oe = function(err) { - throw console.error(err), err; - }; -}([]);" -`; - -exports[`when applied with uglifyOptions.ecma matches snapshot for import and export: warnings 1`] = `Array []`; diff --git a/test/__snapshots__/exclude-option.test.js.snap b/test/__snapshots__/exclude-option.test.js.snap index 4d3fd4cc..cb1859ba 100644 --- a/test/__snapshots__/exclude-option.test.js.snap +++ b/test/__snapshots__/exclude-option.test.js.snap @@ -1,10 +1,10 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`when applied with exclude option matches snapshot for a single exclude: entry.a3533847fa24ef98733c.js 1`] = `"webpackJsonp([2],{2:function(o,n){o.exports=function(){console.log(7)}}},[2]);"`; +exports[`when applied with exclude option matches snapshot for a single exclude value: entry.a3533847fa24ef98733c.js 1`] = `"webpackJsonp([2],{2:function(o,n){o.exports=function(){console.log(7)}}},[2]);"`; -exports[`when applied with exclude option matches snapshot for a single exclude: errors 1`] = `Array []`; +exports[`when applied with exclude option matches snapshot for a single exclude value: errors 1`] = `Array []`; -exports[`when applied with exclude option matches snapshot for a single exclude: excluded1.4d3a1b43eccbc2acc9d6.js 1`] = ` +exports[`when applied with exclude option matches snapshot for a single exclude value: excluded1.4d3a1b43eccbc2acc9d6.js 1`] = ` "webpackJsonp([1],[ /* 0 */ /***/ (function(module, exports) { @@ -18,17 +18,17 @@ module.exports = function Bar1() { ],[0]);" `; -exports[`when applied with exclude option matches snapshot for a single exclude: excluded2.a96f544a34079b25c7b4.js 1`] = `"webpackJsonp([0],[,function(o,n){o.exports=function(){console.log(7)}}],[1]);"`; +exports[`when applied with exclude option matches snapshot for a single exclude value: excluded2.a96f544a34079b25c7b4.js 1`] = `"webpackJsonp([0],[,function(o,n){o.exports=function(){console.log(7)}}],[1]);"`; -exports[`when applied with exclude option matches snapshot for a single exclude: manifest.d37b2b873f771997c745.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a{console.log(\\"Good\\")}),o.default=\\"Awesome\\"}},[4]);"`; +/***/ 5: +/***/ (function(module, __webpack_exports__, __webpack_require__) { -exports[`when applied with test option with empty value: errors 1`] = `Array []`; +\\"use strict\\"; +Object.defineProperty(__webpack_exports__, \\"__esModule\\", { value: true }); +/* harmony default export */ __webpack_exports__[\\"default\\"] = (\\"async-dep\\"); -exports[`when applied with test option with empty value: importExport.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([1],[,,function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(3);a.default=function(){const t=n.b,a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[2]);"`; -exports[`when applied with test option with empty value: js.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([3],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; +/***/ }) -exports[`when applied with test option with empty value: manifest.js?var=6294434024e08d3418c3 1`] = `"!function(e){var r=window.webpackJsonp;window.webpackJsonp=function(n,c,u){for(var i,a,f,l=0,s=[];l{console.log(\\"Good\\")}),o.default=\\"Awesome\\"}},[4]);"`; -exports[`when applied with test option with empty value: warnings 1`] = `Array []`; +exports[`when applied with test option matches snapshot for a multiple test values: errors 1`] = `Array []`; + +exports[`when applied with test option matches snapshot for a multiple test values: importExport.js?var=6294434024e08d3418c3 1`] = ` +"webpackJsonp([1],[ +/* 0 */, +/* 1 */, +/* 2 */ +/***/ (function(module, __webpack_exports__, __webpack_require__) { + +\\"use strict\\"; +Object.defineProperty(__webpack_exports__, \\"__esModule\\", { value: true }); +/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__dep__ = __webpack_require__(3); + + +function Foo() { + const b = __WEBPACK_IMPORTED_MODULE_0__dep__[\\"b\\" /* default */]; + const baz = \`baz\${Math.random()}\`; + return () => { + return { + a: b + __WEBPACK_IMPORTED_MODULE_0__dep__[\\"a\\" /* bar */] + baz, + b, + baz, + }; + }; +} + +/* harmony default export */ __webpack_exports__[\\"default\\"] = (Foo); + + +/***/ }), +/* 3 */ +/***/ (function(module, __webpack_exports__, __webpack_require__) { + +\\"use strict\\"; +const bar = 'bar'; +/* harmony export (immutable) */ __webpack_exports__[\\"a\\"] = bar; + +/* harmony default export */ __webpack_exports__[\\"b\\"] = ('foo'); + + +/***/ }) +],[2]);" +`; + +exports[`when applied with test option matches snapshot for a multiple test values: js.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([3],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; + +exports[`when applied with test option matches snapshot for a multiple test values: manifest.js?var=6294434024e08d3418c3 1`] = ` +"/******/ (function(modules) { // webpackBootstrap +/******/ // install a JSONP callback for chunk loading +/******/ var parentJsonpFunction = window[\\"webpackJsonp\\"]; +/******/ window[\\"webpackJsonp\\"] = function webpackJsonpCallback(chunkIds, moreModules, executeModules) { +/******/ // add \\"moreModules\\" to the modules object, +/******/ // then flag all \\"chunkIds\\" as loaded and fire callback +/******/ var moduleId, chunkId, i = 0, resolves = [], result; +/******/ for(;i < chunkIds.length; i++) { +/******/ chunkId = chunkIds[i]; +/******/ if(installedChunks[chunkId]) { +/******/ resolves.push(installedChunks[chunkId][0]); +/******/ } +/******/ installedChunks[chunkId] = 0; +/******/ } +/******/ for(moduleId in moreModules) { +/******/ if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) { +/******/ modules[moduleId] = moreModules[moduleId]; +/******/ } +/******/ } +/******/ if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules, executeModules); +/******/ while(resolves.length) { +/******/ resolves.shift()(); +/******/ } +/******/ if(executeModules) { +/******/ for(i=0; i < executeModules.length; i++) { +/******/ result = __webpack_require__(__webpack_require__.s = executeModules[i]); +/******/ } +/******/ } +/******/ return result; +/******/ }; +/******/ +/******/ // The module cache +/******/ var installedModules = {}; +/******/ +/******/ // objects to store loaded and loading chunks +/******/ var installedChunks = { +/******/ 5: 0 +/******/ }; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ +/******/ // Check if module is in cache +/******/ if(installedModules[moduleId]) { +/******/ return installedModules[moduleId].exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = installedModules[moduleId] = { +/******/ i: moduleId, +/******/ l: false, +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); +/******/ +/******/ // Flag the module as loaded +/******/ module.l = true; +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/******/ // This file contains only the entry chunk. +/******/ // The chunk loading function for additional chunks +/******/ __webpack_require__.e = function requireEnsure(chunkId) { +/******/ var installedChunkData = installedChunks[chunkId]; +/******/ if(installedChunkData === 0) { +/******/ return new Promise(function(resolve) { resolve(); }); +/******/ } +/******/ +/******/ // a Promise means \\"currently loading\\". +/******/ if(installedChunkData) { +/******/ return installedChunkData[2]; +/******/ } +/******/ +/******/ // setup Promise in chunk cache +/******/ var promise = new Promise(function(resolve, reject) { +/******/ installedChunkData = installedChunks[chunkId] = [resolve, reject]; +/******/ }); +/******/ installedChunkData[2] = promise; +/******/ +/******/ // start chunk loading +/******/ var head = document.getElementsByTagName('head')[0]; +/******/ var script = document.createElement('script'); +/******/ script.type = \\"text/javascript\\"; +/******/ script.charset = 'utf-8'; +/******/ script.async = true; +/******/ script.timeout = 120000; +/******/ +/******/ if (__webpack_require__.nc) { +/******/ script.setAttribute(\\"nonce\\", __webpack_require__.nc); +/******/ } +/******/ script.src = __webpack_require__.p + \\"\\" + chunkId + \\".\\" + ({}[chunkId]||chunkId) + \\".js?ver=\\" + \\"6294434024e08d3418c3\\" + \\"\\"; +/******/ var timeout = setTimeout(onScriptComplete, 120000); +/******/ script.onerror = script.onload = onScriptComplete; +/******/ function onScriptComplete() { +/******/ // avoid mem leaks in IE. +/******/ script.onerror = script.onload = null; +/******/ clearTimeout(timeout); +/******/ var chunk = installedChunks[chunkId]; +/******/ if(chunk !== 0) { +/******/ if(chunk) { +/******/ chunk[1](new Error('Loading chunk ' + chunkId + ' failed.')); +/******/ } +/******/ installedChunks[chunkId] = undefined; +/******/ } +/******/ }; +/******/ head.appendChild(script); +/******/ +/******/ return promise; +/******/ }; +/******/ +/******/ // expose the modules object (__webpack_modules__) +/******/ __webpack_require__.m = modules; +/******/ +/******/ // expose the module cache +/******/ __webpack_require__.c = installedModules; +/******/ +/******/ // define getter function for harmony exports +/******/ __webpack_require__.d = function(exports, name, getter) { +/******/ if(!__webpack_require__.o(exports, name)) { +/******/ Object.defineProperty(exports, name, { +/******/ configurable: false, +/******/ enumerable: true, +/******/ get: getter +/******/ }); +/******/ } +/******/ }; +/******/ +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __webpack_require__.n = function(module) { +/******/ var getter = module && module.__esModule ? +/******/ function getDefault() { return module['default']; } : +/******/ function getModuleExports() { return module; }; +/******/ __webpack_require__.d(getter, 'a', getter); +/******/ return getter; +/******/ }; +/******/ +/******/ // Object.prototype.hasOwnProperty.call +/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; +/******/ +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = \\"\\"; +/******/ +/******/ // on error function for async loading +/******/ __webpack_require__.oe = function(err) { console.error(err); throw err; }; +/******/ }) +/************************************************************************/ +/******/ ([]);" +`; + +exports[`when applied with test option matches snapshot for a multiple test values: mjs.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([2],[,function(o,n){o.exports=function(){console.log(7)}}],[1]);"`; + +exports[`when applied with test option matches snapshot for a multiple test values: warnings 1`] = `Array []`; + +exports[`when applied with test option matches snapshot for a single test value: 0.0.js?ver=6294434024e08d3418c3 1`] = ` +"webpackJsonp([0],{ + +/***/ 5: +/***/ (function(module, __webpack_exports__, __webpack_require__) { + +\\"use strict\\"; +Object.defineProperty(__webpack_exports__, \\"__esModule\\", { value: true }); +/* harmony default export */ __webpack_exports__[\\"default\\"] = (\\"async-dep\\"); + + +/***/ }) + +});" +`; + +exports[`when applied with test option matches snapshot for a single test value: AsyncImportExport.js?var=6294434024e08d3418c3 1`] = ` +"webpackJsonp([4],{ + +/***/ 4: +/***/ (function(module, __webpack_exports__, __webpack_require__) { + +\\"use strict\\"; +Object.defineProperty(__webpack_exports__, \\"__esModule\\", { value: true }); +__webpack_require__.e/* import() */(0).then(__webpack_require__.bind(null, 5)).then(() => { + console.log('Good') +}); + +/* harmony default export */ __webpack_exports__[\\"default\\"] = (\\"Awesome\\"); + + +/***/ }) + +},[4]);" +`; + +exports[`when applied with test option matches snapshot for a single test value: errors 1`] = `Array []`; + +exports[`when applied with test option matches snapshot for a single test value: importExport.js?var=6294434024e08d3418c3 1`] = ` +"webpackJsonp([1],[ +/* 0 */, +/* 1 */, +/* 2 */ +/***/ (function(module, __webpack_exports__, __webpack_require__) { + +\\"use strict\\"; +Object.defineProperty(__webpack_exports__, \\"__esModule\\", { value: true }); +/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__dep__ = __webpack_require__(3); + + +function Foo() { + const b = __WEBPACK_IMPORTED_MODULE_0__dep__[\\"b\\" /* default */]; + const baz = \`baz\${Math.random()}\`; + return () => { + return { + a: b + __WEBPACK_IMPORTED_MODULE_0__dep__[\\"a\\" /* bar */] + baz, + b, + baz, + }; + }; +} + +/* harmony default export */ __webpack_exports__[\\"default\\"] = (Foo); + + +/***/ }), +/* 3 */ +/***/ (function(module, __webpack_exports__, __webpack_require__) { + +\\"use strict\\"; +const bar = 'bar'; +/* harmony export (immutable) */ __webpack_exports__[\\"a\\"] = bar; + +/* harmony default export */ __webpack_exports__[\\"b\\"] = ('foo'); + + +/***/ }) +],[2]);" +`; + +exports[`when applied with test option matches snapshot for a single test value: js.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([3],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; + +exports[`when applied with test option matches snapshot for a single test value: manifest.js?var=6294434024e08d3418c3 1`] = ` +"/******/ (function(modules) { // webpackBootstrap +/******/ // install a JSONP callback for chunk loading +/******/ var parentJsonpFunction = window[\\"webpackJsonp\\"]; +/******/ window[\\"webpackJsonp\\"] = function webpackJsonpCallback(chunkIds, moreModules, executeModules) { +/******/ // add \\"moreModules\\" to the modules object, +/******/ // then flag all \\"chunkIds\\" as loaded and fire callback +/******/ var moduleId, chunkId, i = 0, resolves = [], result; +/******/ for(;i < chunkIds.length; i++) { +/******/ chunkId = chunkIds[i]; +/******/ if(installedChunks[chunkId]) { +/******/ resolves.push(installedChunks[chunkId][0]); +/******/ } +/******/ installedChunks[chunkId] = 0; +/******/ } +/******/ for(moduleId in moreModules) { +/******/ if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) { +/******/ modules[moduleId] = moreModules[moduleId]; +/******/ } +/******/ } +/******/ if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules, executeModules); +/******/ while(resolves.length) { +/******/ resolves.shift()(); +/******/ } +/******/ if(executeModules) { +/******/ for(i=0; i < executeModules.length; i++) { +/******/ result = __webpack_require__(__webpack_require__.s = executeModules[i]); +/******/ } +/******/ } +/******/ return result; +/******/ }; +/******/ +/******/ // The module cache +/******/ var installedModules = {}; +/******/ +/******/ // objects to store loaded and loading chunks +/******/ var installedChunks = { +/******/ 5: 0 +/******/ }; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ +/******/ // Check if module is in cache +/******/ if(installedModules[moduleId]) { +/******/ return installedModules[moduleId].exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = installedModules[moduleId] = { +/******/ i: moduleId, +/******/ l: false, +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); +/******/ +/******/ // Flag the module as loaded +/******/ module.l = true; +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/******/ // This file contains only the entry chunk. +/******/ // The chunk loading function for additional chunks +/******/ __webpack_require__.e = function requireEnsure(chunkId) { +/******/ var installedChunkData = installedChunks[chunkId]; +/******/ if(installedChunkData === 0) { +/******/ return new Promise(function(resolve) { resolve(); }); +/******/ } +/******/ +/******/ // a Promise means \\"currently loading\\". +/******/ if(installedChunkData) { +/******/ return installedChunkData[2]; +/******/ } +/******/ +/******/ // setup Promise in chunk cache +/******/ var promise = new Promise(function(resolve, reject) { +/******/ installedChunkData = installedChunks[chunkId] = [resolve, reject]; +/******/ }); +/******/ installedChunkData[2] = promise; +/******/ +/******/ // start chunk loading +/******/ var head = document.getElementsByTagName('head')[0]; +/******/ var script = document.createElement('script'); +/******/ script.type = \\"text/javascript\\"; +/******/ script.charset = 'utf-8'; +/******/ script.async = true; +/******/ script.timeout = 120000; +/******/ +/******/ if (__webpack_require__.nc) { +/******/ script.setAttribute(\\"nonce\\", __webpack_require__.nc); +/******/ } +/******/ script.src = __webpack_require__.p + \\"\\" + chunkId + \\".\\" + ({}[chunkId]||chunkId) + \\".js?ver=\\" + \\"6294434024e08d3418c3\\" + \\"\\"; +/******/ var timeout = setTimeout(onScriptComplete, 120000); +/******/ script.onerror = script.onload = onScriptComplete; +/******/ function onScriptComplete() { +/******/ // avoid mem leaks in IE. +/******/ script.onerror = script.onload = null; +/******/ clearTimeout(timeout); +/******/ var chunk = installedChunks[chunkId]; +/******/ if(chunk !== 0) { +/******/ if(chunk) { +/******/ chunk[1](new Error('Loading chunk ' + chunkId + ' failed.')); +/******/ } +/******/ installedChunks[chunkId] = undefined; +/******/ } +/******/ }; +/******/ head.appendChild(script); +/******/ +/******/ return promise; +/******/ }; +/******/ +/******/ // expose the modules object (__webpack_modules__) +/******/ __webpack_require__.m = modules; +/******/ +/******/ // expose the module cache +/******/ __webpack_require__.c = installedModules; +/******/ +/******/ // define getter function for harmony exports +/******/ __webpack_require__.d = function(exports, name, getter) { +/******/ if(!__webpack_require__.o(exports, name)) { +/******/ Object.defineProperty(exports, name, { +/******/ configurable: false, +/******/ enumerable: true, +/******/ get: getter +/******/ }); +/******/ } +/******/ }; +/******/ +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __webpack_require__.n = function(module) { +/******/ var getter = module && module.__esModule ? +/******/ function getDefault() { return module['default']; } : +/******/ function getModuleExports() { return module; }; +/******/ __webpack_require__.d(getter, 'a', getter); +/******/ return getter; +/******/ }; +/******/ +/******/ // Object.prototype.hasOwnProperty.call +/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; +/******/ +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = \\"\\"; +/******/ +/******/ // on error function for async loading +/******/ __webpack_require__.oe = function(err) { console.error(err); throw err; }; +/******/ }) +/************************************************************************/ +/******/ ([]);" +`; + +exports[`when applied with test option matches snapshot for a single test value: mjs.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([2],[,function(o,n){o.exports=function(){console.log(7)}}],[1]);"`; + +exports[`when applied with test option matches snapshot for a single test value: warnings 1`] = `Array []`; + +exports[`when applied with test option matches snapshot with empty value: 0.0.js?ver=6294434024e08d3418c3 1`] = `"webpackJsonp([0],{5:function(e,t,c){\\"use strict\\";Object.defineProperty(t,\\"__esModule\\",{value:!0}),t.default=\\"async-dep\\"}});"`; + +exports[`when applied with test option matches snapshot with empty value: AsyncImportExport.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([4],{4:function(e,o,n){\\"use strict\\";Object.defineProperty(o,\\"__esModule\\",{value:!0}),n.e(0).then(n.bind(null,5)).then(()=>{console.log(\\"Good\\")}),o.default=\\"Awesome\\"}},[4]);"`; + +exports[`when applied with test option matches snapshot with empty value: errors 1`] = `Array []`; + +exports[`when applied with test option matches snapshot with empty value: importExport.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([1],[,,function(t,a,e){\\"use strict\\";Object.defineProperty(a,\\"__esModule\\",{value:!0});var n=e(3);a.default=function(){const t=n.b,a=\`baz\${Math.random()}\`;return()=>({a:t+n.a+a,b:t,baz:a})}},function(t,a,e){\\"use strict\\";a.a=\\"bar\\",a.b=\\"foo\\"}],[2]);"`; + +exports[`when applied with test option matches snapshot with empty value: js.js?var=6294434024e08d3418c3 1`] = `"webpackJsonp([3],[function(o,n){o.exports=function(){console.log(7)}}],[0]);"`; + +exports[`when applied with test option matches snapshot with empty value: manifest.js?var=6294434024e08d3418c3 1`] = `"!function(e){var r=window.webpackJsonp;window.webpackJsonp=function(n,c,u){for(var i,a,f,l=0,s=[];l { - it('matches snapshot for import and export', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/import-export/entry.js`, - output: { - path: `${__dirname}/dist-import-export`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); - - new UglifyJsPlugin({ - uglifyOptions: { - ecma: 5, - mangle: false, - warnings: true, - output: { - beautify: true, - }, - }, - }).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); - } - } - }); - }); - - it('matches snapshot for ecma 5', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/ecma-5/entry.js`, - output: { - path: `${__dirname}/dist-ecma-5`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); - - new UglifyJsPlugin({ - uglifyOptions: { - ecma: 5, - mangle: false, - warnings: true, - output: { - beautify: true, - }, - }, - }).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); - } - } - }); - }); - - it('matches snapshot for ecma 6', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/ecma-6/entry.js`, - output: { - path: `${__dirname}/dist-ecma-6`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); - - new UglifyJsPlugin({ - uglifyOptions: { - ecma: 6, - mangle: false, - warnings: true, - output: { - beautify: true, - }, - }, - }).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); - } - } - }); - }); - - it('matches snapshot for ecma 7', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/ecma-7/entry.js`, - output: { - path: `${__dirname}/dist-ecma-7`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); - new UglifyJsPlugin({ - uglifyOptions: { - ecma: 7, - mangle: false, - warnings: true, - output: { - beautify: true, - }, - }, - }).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); - } - } - }); - }); - - it('matches snapshot for ecma 8', () => { - const compiler = createCompiler({ - entry: `${__dirname}/fixtures/ecma-8/entry.js`, - output: { - path: `${__dirname}/dist-ecma-8`, - filename: '[name].js', - chunkFilename: '[id].[name].js', - }, - }); - - new UglifyJsPlugin({ - uglifyOptions: { - ecma: 8, - mangle: false, - warnings: true, - output: { - beautify: true, - }, - }, - }).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); - } - } - }); - }); -}); diff --git a/test/exclude-option.test.js b/test/exclude-option.test.js index 39bfb1c6..a0524251 100644 --- a/test/exclude-option.test.js +++ b/test/exclude-option.test.js @@ -7,6 +7,7 @@ import { describe('when applied with exclude option', () => { let compiler; + beforeEach(() => { compiler = createCompiler({ entry: { @@ -17,9 +18,9 @@ describe('when applied with exclude option', () => { }); }); - it('matches snapshot for a single exclude', () => { + it('matches snapshot for a single exclude value', () => { new UglifyJsPlugin({ - exclude: /excluded1/, + exclude: /excluded1/i, }).apply(compiler); @@ -38,11 +39,11 @@ describe('when applied with exclude option', () => { }); }); - it('matches snapshot for multiple excludes', () => { + it('matches snapshot for multiple exclude values', () => { new UglifyJsPlugin({ exclude: [ - /excluded1/, - /excluded2/, + /excluded1/i, + /excluded2/i, ], }).apply(compiler); diff --git a/test/extractComments-option-set-to-a-single-file.test.js b/test/extractComments-option-set-to-a-single-file.test.js deleted file mode 100644 index da82292e..00000000 --- a/test/extractComments-option-set-to-a-single-file.test.js +++ /dev/null @@ -1,107 +0,0 @@ -import UglifyJsPlugin from '../src/index'; -import { PluginEnvironment } from './helpers'; - -describe('when applied with extract option set to a single file', () => { - let eventBindings; - let eventBinding; - - beforeEach(() => { - const pluginEnvironment = new PluginEnvironment(); - const compilerEnv = pluginEnvironment.getEnvironmentStub(); - compilerEnv.context = ''; - - const plugin = new UglifyJsPlugin({ - uglifyOptions: { - output: { - comments: 'all', - }, - }, - extractComments: { - condition: /.*/, - filename: 'extracted-comments.js', - }, - }); - plugin.apply(compilerEnv); - eventBindings = pluginEnvironment.getEventBindings(); - }); - - it('binds one event handler', () => { - expect(eventBindings.length).toBe(1); - }); - - describe('compilation handler', () => { - beforeEach(() => { - [eventBinding] = eventBindings; - }); - - it('binds to compilation event', () => { - expect(eventBinding.name).toBe('compilation'); - }); - - describe('when called', () => { - let chunkPluginEnvironment; - let compilationEventBindings; - let compilationEventBinding; - let compilation; - - beforeEach(() => { - chunkPluginEnvironment = new PluginEnvironment(); - compilation = chunkPluginEnvironment.getEnvironmentStub(); - compilation.assets = { - 'test.js': { - source: () => '/* This is a comment from test.js */ function foo(bar) { return bar; }', - }, - 'test2.js': { - source: () => '// This is a comment from test2.js\nfunction foo2(bar) { return bar; }', - }, - 'test3.js': { - source: () => '/* This is a comment from test3.js */ function foo3(bar) { return bar; }\n// This is another comment from test3.js\nfunction foobar3(baz) { return baz; }', - }, - }; - compilation.errors = []; - compilation.warnings = []; - - eventBinding.handler(compilation); - compilationEventBindings = chunkPluginEnvironment.getEventBindings(); - }); - - it('binds one event handler', () => { - expect(compilationEventBindings.length).toBe(1); - }); - - describe('optimize-chunk-assets handler', () => { - beforeEach(() => { - [compilationEventBinding] = compilationEventBindings; - }); - - it('binds to optimize-chunk-assets event', () => { - expect(compilationEventBinding.name).toEqual('optimize-chunk-assets'); - }); - - it('preserves comments', () => { - compilationEventBinding.handler([{ - files: ['test.js', 'test2.js', 'test3.js'], - }], () => { - expect(compilation.assets['test.js'].source()).toEqual(expect.stringContaining('/*')); - expect(compilation.assets['test2.js'].source()).toEqual(expect.stringContaining('//')); - expect(compilation.assets['test3.js'].source()).toEqual(expect.stringContaining('/*')); - expect(compilation.assets['test3.js'].source()).toEqual(expect.stringContaining('//')); - }); - }); - - it('extracts comments to specified file', () => { - compilationEventBinding.handler([{ - files: ['test.js', 'test2.js', 'test3.js'], - }], () => { - expect(compilation.errors.length).toBe(0); - expect(compilation.assets['extracted-comments.js'].source()).toEqual(expect.stringContaining('/* This is a comment from test.js */')); - expect(compilation.assets['extracted-comments.js'].source()).toEqual(expect.stringContaining('// This is a comment from test2.js')); - expect(compilation.assets['extracted-comments.js'].source()).toEqual(expect.stringContaining('/* This is a comment from test3.js */')); - expect(compilation.assets['extracted-comments.js'].source()).toEqual(expect.stringContaining('// This is another comment from test3.js')); - expect(compilation.assets['extracted-comments.js'].source()).not.toEqual(expect.stringContaining('function')); - }); - }); - }); - }); - }); -}); diff --git a/test/extractComments-option.test.js b/test/extractComments-option.test.js index fc0cc61b..8639b923 100644 --- a/test/extractComments-option.test.js +++ b/test/extractComments-option.test.js @@ -381,4 +381,109 @@ describe('when options.extractComments', () => { expect(compilation.warnings).toMatchSnapshot('warnings'); }); }); + + describe('when applied with extract option set to a single file', () => { + let eventBindings; + let eventBinding; + + beforeEach(() => { + const pluginEnvironment = new PluginEnvironment(); + const compilerEnv = pluginEnvironment.getEnvironmentStub(); + compilerEnv.context = ''; + + const plugin = new UglifyJsPlugin({ + uglifyOptions: { + output: { + comments: 'all', + }, + }, + extractComments: { + condition: /.*/, + filename: 'extracted-comments.js', + }, + }); + plugin.apply(compilerEnv); + eventBindings = pluginEnvironment.getEventBindings(); + }); + + it('binds one event handler', () => { + expect(eventBindings.length).toBe(1); + }); + + describe('compilation handler', () => { + beforeEach(() => { + [eventBinding] = eventBindings; + }); + + it('binds to compilation event', () => { + expect(eventBinding.name).toBe('compilation'); + }); + + describe('when called', () => { + let chunkPluginEnvironment; + let compilationEventBindings; + let compilationEventBinding; + let compilation; + + beforeEach(() => { + chunkPluginEnvironment = new PluginEnvironment(); + compilation = chunkPluginEnvironment.getEnvironmentStub(); + compilation.assets = { + 'test.js': { + source: () => '/* This is a comment from test.js */ function foo(bar) { return bar; }', + }, + 'test2.js': { + source: () => '// This is a comment from test2.js\nfunction foo2(bar) { return bar; }', + }, + 'test3.js': { + source: () => '/* This is a comment from test3.js */ function foo3(bar) { return bar; }\n// This is another comment from test3.js\nfunction foobar3(baz) { return baz; }', + }, + }; + compilation.errors = []; + compilation.warnings = []; + + eventBinding.handler(compilation); + compilationEventBindings = chunkPluginEnvironment.getEventBindings(); + }); + + it('binds one event handler', () => { + expect(compilationEventBindings.length).toBe(1); + }); + + describe('optimize-chunk-assets handler', () => { + beforeEach(() => { + [compilationEventBinding] = compilationEventBindings; + }); + + it('binds to optimize-chunk-assets event', () => { + expect(compilationEventBinding.name).toEqual('optimize-chunk-assets'); + }); + + it('preserves comments', () => { + compilationEventBinding.handler([{ + files: ['test.js', 'test2.js', 'test3.js'], + }], () => { + expect(compilation.assets['test.js'].source()).toEqual(expect.stringContaining('/*')); + expect(compilation.assets['test2.js'].source()).toEqual(expect.stringContaining('//')); + expect(compilation.assets['test3.js'].source()).toEqual(expect.stringContaining('/*')); + expect(compilation.assets['test3.js'].source()).toEqual(expect.stringContaining('//')); + }); + }); + + it('extracts comments to specified file', () => { + compilationEventBinding.handler([{ + files: ['test.js', 'test2.js', 'test3.js'], + }], () => { + expect(compilation.errors.length).toBe(0); + expect(compilation.assets['extracted-comments.js'].source()).toEqual(expect.stringContaining('/* This is a comment from test.js */')); + expect(compilation.assets['extracted-comments.js'].source()).toEqual(expect.stringContaining('// This is a comment from test2.js')); + expect(compilation.assets['extracted-comments.js'].source()).toEqual(expect.stringContaining('/* This is a comment from test3.js */')); + expect(compilation.assets['extracted-comments.js'].source()).toEqual(expect.stringContaining('// This is another comment from test3.js')); + expect(compilation.assets['extracted-comments.js'].source()).not.toEqual(expect.stringContaining('function')); + }); + }); + }); + }); + }); + }); }); diff --git a/test/include-option.test.js b/test/include-option.test.js index e517002e..dbee027f 100644 --- a/test/include-option.test.js +++ b/test/include-option.test.js @@ -7,6 +7,7 @@ import { describe('when applied with include option', () => { let compiler; + beforeEach(() => { compiler = createCompiler({ entry: { @@ -17,9 +18,9 @@ describe('when applied with include option', () => { }); }); - it('matches snapshot for a single include', () => { + it('matches snapshot for a single include value', () => { new UglifyJsPlugin({ - include: /included1/, + include: /included1/i, }).apply(compiler); @@ -38,11 +39,11 @@ describe('when applied with include option', () => { }); }); - it('matches snapshot for multiple includes', () => { + it('matches snapshot for multiple include values', () => { new UglifyJsPlugin({ include: [ - /included1/, - /included2/, + /included1/i, + /included2/i, ], }).apply(compiler); diff --git a/test/parallel-option.test.js b/test/parallel-option.test.js index e64a7fc5..b6f53287 100644 --- a/test/parallel-option.test.js +++ b/test/parallel-option.test.js @@ -2,7 +2,6 @@ import os from 'os'; import workerFarm from 'worker-farm'; import UglifyJsPlugin from '../src/index'; import { - PluginEnvironment, createCompiler, compile, cleanErrorStack, @@ -23,342 +22,87 @@ jest.mock('worker-farm', () => { return mock; }); -describe('when options.parallel', () => { - const assets = { - 'test.js': { - source: () => 'function test(foo) { foo = 1; }', - }, - 'test1.js': { - source: () => 'function test1(foo) { foo = 1; }', - }, - 'test2.js': { - source: () => 'function test2(foo) { foo = 1; }', - }, - 'test3.js': { - source: () => 'function test3(foo) { foo = 1; }', - }, - }; +describe('when applied with parallel option', () => { + let compiler; - describe('false', () => { - let eventBindings; - let eventBinding; + beforeEach(() => { + workerFarm.mockClear(); + workerFarm.end.mockClear(); - beforeEach(() => { - const pluginEnvironment = new PluginEnvironment(); - const compilerEnv = pluginEnvironment.getEnvironmentStub(); - compilerEnv.context = ''; - - const plugin = new UglifyJsPlugin({ - parallel: false, - }); - plugin.apply(compilerEnv); - eventBindings = pluginEnvironment.getEventBindings(); - }); - - it('binds one event handler', () => { - expect(eventBindings.length).toBe(1); + compiler = createCompiler({ + entry: { + one: `${__dirname}/fixtures/entry.js`, + two: `${__dirname}/fixtures/entry.js`, + three: `${__dirname}/fixtures/entry.js`, + four: `${__dirname}/fixtures/entry.js`, + }, }); + }); - describe('compilation handler', () => { - beforeEach(() => { - [eventBinding] = eventBindings; - }); - - it('binds to compilation event', () => { - expect(eventBinding.name).toBe('compilation'); - }); - - describe('when called', () => { - let chunkPluginEnvironment; - let compilationEventBindings; - let compilationEventBinding; - let compilation; - let callback; - - beforeEach(() => { - chunkPluginEnvironment = new PluginEnvironment(); - compilation = chunkPluginEnvironment.getEnvironmentStub(); - compilation.assets = Object.assign({}, assets); - compilation.errors = []; - - workerFarm.mockClear(); - workerFarm.end.mockClear(); - - eventBinding.handler(compilation); - compilationEventBindings = chunkPluginEnvironment.getEventBindings(); - }); - - it('binds one event handler', () => { - expect(compilationEventBindings.length).toBe(1); - }); - - describe('optimize-chunk-assets handler', () => { - beforeEach(() => { - [compilationEventBinding] = compilationEventBindings; - }); - - it('binds to optimize-chunk-assets event', () => { - expect(compilationEventBinding.name).toEqual('optimize-chunk-assets'); - }); - - it('only calls callback once', (done) => { - callback = jest.fn(); - compilationEventBinding.handler([''], () => { - callback(); - expect(callback.mock.calls.length).toBe(1); - done(); - }); - }); - - it('parallelization', (done) => { - compilationEventBinding.handler([{ - files: ['test.js', 'test1.js', 'test2.js', 'test3.js'], - }], () => { - expect(workerFarm.mock.calls.length).toBe(0); - expect(workerFarm.end.mock.calls.length).toBe(0); - - done(); - }); - }); - }); - }); - }); + it('matches snapshot with false value', () => { + new UglifyJsPlugin({ parallel: false }).apply(compiler); - it('matches snapshot', () => { - const compiler = createCompiler(); - new UglifyJsPlugin({ parallel: false }).apply(compiler); + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); - return compile(compiler).then((stats) => { - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); + expect(workerFarm.mock.calls.length).toBe(0); + expect(workerFarm.end.mock.calls.length).toBe(0); - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); + 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); - } + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); } - }); + } }); }); - describe('true', () => { - let eventBindings; - let eventBinding; - - beforeEach(() => { - const pluginEnvironment = new PluginEnvironment(); - const compilerEnv = pluginEnvironment.getEnvironmentStub(); - compilerEnv.context = ''; - - const plugin = new UglifyJsPlugin({ - parallel: true, - }); - plugin.apply(compilerEnv); - eventBindings = pluginEnvironment.getEventBindings(); - }); - - it('binds one event handler', () => { - expect(eventBindings.length).toBe(1); - }); - - describe('compilation handler', () => { - beforeEach(() => { - [eventBinding] = eventBindings; - }); - - it('binds to compilation event', () => { - expect(eventBinding.name).toBe('compilation'); - }); - - describe('when called', () => { - let chunkPluginEnvironment; - let compilationEventBindings; - let compilationEventBinding; - let compilation; - let callback; - - beforeEach(() => { - chunkPluginEnvironment = new PluginEnvironment(); - compilation = chunkPluginEnvironment.getEnvironmentStub(); - compilation.assets = Object.assign({}, assets); - compilation.warnings = []; - compilation.errors = []; - - workerFarm.mockClear(); - workerFarm.end.mockClear(); + it('matches snapshot with true value', () => { + new UglifyJsPlugin({ parallel: true }).apply(compiler); - eventBinding.handler(compilation); - compilationEventBindings = chunkPluginEnvironment.getEventBindings(); - }); + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); - it('binds one event handler', () => { - expect(compilationEventBindings.length).toBe(1); - }); + expect(workerFarm.mock.calls.length).toBe(1); + expect(workerFarm.mock.calls[0][0].maxConcurrentWorkers).toBe(os.cpus().length - 1); + expect(workerFarmMock.mock.calls.length).toBe(Object.keys(stats.compilation.assets).length); + expect(workerFarm.end.mock.calls.length).toBe(1); - describe('optimize-chunk-assets handler', () => { - beforeEach(() => { - [compilationEventBinding] = compilationEventBindings; - }); + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); - it('binds to optimize-chunk-assets event', () => { - expect(compilationEventBinding.name).toEqual('optimize-chunk-assets'); - }); - - it('only calls callback once', (done) => { - callback = jest.fn(); - compilationEventBinding.handler([''], () => { - callback(); - expect(callback.mock.calls.length).toBe(1); - done(); - }); - }); - - it('parallelization', (done) => { - compilationEventBinding.handler([{ - files: ['test.js', 'test1.js', 'test2.js', 'test3.js'], - }], () => { - expect(workerFarm.mock.calls.length).toBe(1); - expect(workerFarm.mock.calls[0][0].maxConcurrentWorkers).toBe(os.cpus().length - 1); - expect(workerFarmMock.mock.calls.length).toBe(4); - expect(workerFarm.end.mock.calls.length).toBe(1); - - done(); - }); - }); - }); - }); - }); - - it('matches snapshot', () => { - const compiler = createCompiler(); - new UglifyJsPlugin({ parallel: true }).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); - } + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); } - }); + } }); }); - describe('number', () => { - let eventBindings; - let eventBinding; - - beforeEach(() => { - const pluginEnvironment = new PluginEnvironment(); - const compilerEnv = pluginEnvironment.getEnvironmentStub(); - compilerEnv.context = ''; - - const plugin = new UglifyJsPlugin({ - parallel: 2, - }); - plugin.apply(compilerEnv); - eventBindings = pluginEnvironment.getEventBindings(); - }); - - it('binds one event handler', () => { - expect(eventBindings.length).toBe(1); - }); - - describe('compilation handler', () => { - beforeEach(() => { - [eventBinding] = eventBindings; - }); - - it('binds to compilation event', () => { - expect(eventBinding.name).toBe('compilation'); - }); - - describe('when called', () => { - let chunkPluginEnvironment; - let compilationEventBindings; - let compilationEventBinding; - let compilation; - let callback; - - beforeEach(() => { - chunkPluginEnvironment = new PluginEnvironment(); - compilation = chunkPluginEnvironment.getEnvironmentStub(); - compilation.assets = Object.assign({}, assets); - compilation.warnings = []; - compilation.errors = []; - - workerFarm.mockClear(); - workerFarm.end.mockClear(); - - eventBinding.handler(compilation); - compilationEventBindings = chunkPluginEnvironment.getEventBindings(); - }); - - it('binds one event handler', () => { - expect(compilationEventBindings.length).toBe(1); - }); - - describe('optimize-chunk-assets handler', () => { - beforeEach(() => { - [compilationEventBinding] = compilationEventBindings; - }); - - it('binds to optimize-chunk-assets event', () => { - expect(compilationEventBinding.name).toEqual('optimize-chunk-assets'); - }); - - it('only calls callback once', (done) => { - callback = jest.fn(); - compilationEventBinding.handler([''], () => { - callback(); - expect(callback.mock.calls.length).toBe(1); - done(); - }); - }); - - it('parallelization', (done) => { - compilationEventBinding.handler([{ - files: ['test.js', 'test1.js', 'test2.js', 'test3.js'], - }], () => { - expect(workerFarm.mock.calls.length).toBe(1); - // Appveyor give only one core - expect(workerFarm.mock.calls[0][0].maxConcurrentWorkers) - .toBe(Math.min(Number(2) || 0, os.cpus().length - 1)); - expect(workerFarmMock.mock.calls.length).toBe(4); - expect(workerFarm.end.mock.calls.length).toBe(1); - - done(); - }); - }); - }); - }); - }); + it('matches snapshot with 2 value (number)', () => { + new UglifyJsPlugin({ parallel: 2 }).apply(compiler); - it('matches snapshot', () => { - const compiler = createCompiler(); - new UglifyJsPlugin({ parallel: true }).apply(compiler); + return compile(compiler).then((stats) => { + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); - return compile(compiler).then((stats) => { - const errors = stats.compilation.errors.map(cleanErrorStack); - const warnings = stats.compilation.warnings.map(cleanErrorStack); + expect(workerFarm.mock.calls.length).toBe(1); + expect(workerFarm.mock.calls[0][0].maxConcurrentWorkers).toBe(2); + expect(workerFarmMock.mock.calls.length).toBe(Object.keys(stats.compilation.assets).length); + expect(workerFarm.end.mock.calls.length).toBe(1); - expect(errors).toMatchSnapshot('errors'); - expect(warnings).toMatchSnapshot('warnings'); + 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); - } + for (const file in stats.compilation.assets) { + if (Object.prototype.hasOwnProperty.call(stats.compilation.assets, file)) { + expect(stats.compilation.assets[file].source()).toMatchSnapshot(file); } - }); + } }); }); }); diff --git a/test/test-option.test.js b/test/test-option.test.js index 39433d45..1f204f11 100644 --- a/test/test-option.test.js +++ b/test/test-option.test.js @@ -6,8 +6,10 @@ import { } from './helpers'; describe('when applied with test option', () => { - it('with empty value', () => { - const compiler = createCompiler({ + let compiler; + + beforeEach(() => { + compiler = createCompiler({ entry: { js: `${__dirname}/fixtures/entry.js`, mjs: `${__dirname}/fixtures/entry.mjs`, @@ -20,7 +22,9 @@ describe('when applied with test option', () => { chunkFilename: '[id].[name].js?ver=[hash]', }, }); + }); + it('matches snapshot with empty value', () => { new UglifyJsPlugin().apply(compiler); return compile(compiler).then((stats) => { @@ -37,4 +41,47 @@ describe('when applied with test option', () => { } }); }); + + it('matches snapshot for a single test value', () => { + new UglifyJsPlugin({ + test: /(m)?js\.js(\?.*)?$/i, + }).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); + } + } + }); + }); + + it('matches snapshot for a multiple test values', () => { + new UglifyJsPlugin({ + test: [ + /(m)?js\.js(\?.*)?$/i, + /AsyncImportExport\.js(\?.*)?$/i, + ], + }).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); + } + } + }); + }); }); diff --git a/test/uglifyOptions-option.test.js b/test/uglifyOptions-option.test.js index 7e77ae87..6c46a92c 100644 --- a/test/uglifyOptions-option.test.js +++ b/test/uglifyOptions-option.test.js @@ -6,6 +6,149 @@ import { } from './helpers'; describe('when applied with uglify-es options', () => { + it('matches snapshot for ecma 5', () => { + const compiler = createCompiler({ + entry: `${__dirname}/fixtures/ecma-5/entry.js`, + output: { + path: `${__dirname}/dist-ecma-5`, + filename: '[name].js', + chunkFilename: '[id].[name].js', + }, + }); + + new UglifyJsPlugin({ + uglifyOptions: { + ecma: 5, + mangle: false, + warnings: true, + output: { + beautify: true, + }, + }, + }).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); + } + } + }); + }); + + it('matches snapshot for ecma 6', () => { + const compiler = createCompiler({ + entry: `${__dirname}/fixtures/ecma-6/entry.js`, + output: { + path: `${__dirname}/dist-ecma-6`, + filename: '[name].js', + chunkFilename: '[id].[name].js', + }, + }); + + new UglifyJsPlugin({ + uglifyOptions: { + ecma: 6, + mangle: false, + warnings: true, + output: { + beautify: true, + }, + }, + }).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); + } + } + }); + }); + + it('matches snapshot for ecma 7', () => { + const compiler = createCompiler({ + entry: `${__dirname}/fixtures/ecma-7/entry.js`, + output: { + path: `${__dirname}/dist-ecma-7`, + filename: '[name].js', + chunkFilename: '[id].[name].js', + }, + }); + new UglifyJsPlugin({ + uglifyOptions: { + ecma: 7, + mangle: false, + warnings: true, + output: { + beautify: true, + }, + }, + }).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); + } + } + }); + }); + + it('matches snapshot for ecma 8', () => { + const compiler = createCompiler({ + entry: `${__dirname}/fixtures/ecma-8/entry.js`, + output: { + path: `${__dirname}/dist-ecma-8`, + filename: '[name].js', + chunkFilename: '[id].[name].js', + }, + }); + + new UglifyJsPlugin({ + uglifyOptions: { + ecma: 8, + mangle: false, + warnings: true, + output: { + beautify: true, + }, + }, + }).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); + } + } + }); + }); + it('matches snapshot for `parse` options', () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/import-export/entry.js`,