From 960d24924c6776126ad0568e729ad9433ce8c636 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Wed, 4 Sep 2019 15:32:07 +0300 Subject: [PATCH] feat: enable the cache option by default (#132) BREAKING CHANGE: the `cache` option is `true` by default --- README.md | 21 +-- src/TaskRunner.js | 17 +- src/index.js | 2 +- test/TerserPlugin.test.js | 12 +- test/__snapshots__/cache-option.test.js.snap | 52 ++++-- test/cache-option.test.js | 186 ++++++++++++------- test/chunkFilter-option.test.js | 12 +- test/exclude-option.test.js | 12 +- test/extractComments-option.test.js | 12 +- test/helpers/getCacheDirectory.js | 9 + test/helpers/index.js | 12 +- test/helpers/removeCache.js | 11 ++ test/include-option.test.js | 12 +- test/minify-option.test.js | 12 +- test/parallel-option-failure.test.js | 15 +- test/parallel-option.test.js | 12 +- test/sourceMap-option.test.js | 12 +- test/supports-multicompiler.test.js | 5 + test/terserOptions-option.test.js | 12 +- test/test-option.test.js | 12 +- test/warningsFilter-option.test.js | 12 +- 21 files changed, 334 insertions(+), 128 deletions(-) create mode 100644 test/helpers/getCacheDirectory.js create mode 100644 test/helpers/removeCache.js diff --git a/README.md b/README.md index cab065d1..ec7059c1 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ module.exports = { ### `cache` Type: `Boolean|String` -Default: `false` +Default: `true` Enable file caching. Default path to cache directory: `node_modules/.cache/terser-webpack-plugin`. @@ -599,25 +599,6 @@ module.exports = { ## Examples -### Cache And Parallel - -Enable cache and multi-process parallel running. - -**webpack.config.js** - -```js -module.exports = { - optimization: { - minimizer: [ - new TerserPlugin({ - cache: true, - parallel: true, - }), - ], - }, -}; -``` - ### Preserve Comments Extract all legal comments (i.e. `/^\**!|@preserve|@license|@cc_on/i`) and preserve `/@license/i` comments. diff --git a/src/TaskRunner.js b/src/TaskRunner.js index 87b4c580..bf6989c0 100644 --- a/src/TaskRunner.js +++ b/src/TaskRunner.js @@ -13,16 +13,23 @@ const worker = require.resolve('./worker'); export default class TaskRunner { constructor(options = {}) { const { cache, parallel } = options; - this.cacheDir = - cache === true - ? findCacheDir({ name: 'terser-webpack-plugin' }) || os.tmpdir() - : cache; + + this.cacheDir = cache === true ? TaskRunner.getCacheDirectory() : cache; + this.maxConcurrentWorkers = TaskRunner.getMaxConcurrentWorkers(parallel); + } + + static getCacheDirectory() { + return findCacheDir({ name: 'terser-webpack-plugin' }) || os.tmpdir(); + } + + static getMaxConcurrentWorkers(parallel) { // In some cases cpus() returns undefined // https://github.com/nodejs/node/issues/19022 const cpus = os.cpus() || { length: 1 }; + // WSL sometimes freezes, error seems to be on the WSL side // https://github.com/webpack-contrib/terser-webpack-plugin/issues/21 - this.maxConcurrentWorkers = isWsl + return isWsl ? 1 : parallel === true ? cpus.length - 1 diff --git a/src/index.js b/src/index.js index 677313fc..32a3f12e 100644 --- a/src/index.js +++ b/src/index.js @@ -29,7 +29,7 @@ class TerserPlugin { warningsFilter = () => true, extractComments = false, sourceMap = false, - cache = false, + cache = true, cacheKeys = (defaultCacheKeys) => defaultCacheKeys, parallel = true, include, diff --git a/test/TerserPlugin.test.js b/test/TerserPlugin.test.js index ead2315c..1a22946b 100644 --- a/test/TerserPlugin.test.js +++ b/test/TerserPlugin.test.js @@ -4,7 +4,13 @@ import ChunkTemplate from 'webpack/lib/ChunkTemplate'; import TerserPlugin from '../src/index'; -import { cleanErrorStack, compile, createCompiler, getAssets } from './helpers'; +import { + cleanErrorStack, + compile, + createCompiler, + getAssets, + removeCache, +} from './helpers'; describe('TerserPlugin', () => { const rawSourceMap = { @@ -22,6 +28,10 @@ describe('TerserPlugin', () => { mappings: '', }; + beforeEach(() => Promise.all([removeCache()])); + + afterEach(() => Promise.all([removeCache()])); + it('should work (without options)', async () => { const compiler = createCompiler(); diff --git a/test/__snapshots__/cache-option.test.js.snap b/test/__snapshots__/cache-option.test.js.snap index 6f66f754..fe3b5a10 100644 --- a/test/__snapshots__/cache-option.test.js.snap +++ b/test/__snapshots__/cache-option.test.js.snap @@ -59,7 +59,7 @@ exports[`cache option should match snapshot for the "other-cache-directory" valu exports[`cache option should match snapshot for the "other-cache-directory" value: warnings 2`] = `Array []`; -exports[`cache option should match snapshot for the "true" value when "cacheKey" is custom "function": assets 1`] = ` +exports[`cache option should match snapshot for the "true" value: assets 1`] = ` Object { "five.js": "!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=4)}({4:function(e,t){e.exports=function(){console.log(7)}}});", "four.js": "!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){e.exports=class{}}});", @@ -69,7 +69,7 @@ Object { } `; -exports[`cache option should match snapshot for the "true" value when "cacheKey" is custom "function": assets 2`] = ` +exports[`cache option should match snapshot for the "true" value: assets 2`] = ` Object { "five.js": "!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=4)}({4:function(e,t){e.exports=function(){console.log(7)}}});", "four.js": "!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){e.exports=class{}}});", @@ -79,15 +79,15 @@ Object { } `; -exports[`cache option should match snapshot for the "true" value when "cacheKey" is custom "function": errors 1`] = `Array []`; +exports[`cache option should match snapshot for the "true" value: errors 1`] = `Array []`; -exports[`cache option should match snapshot for the "true" value when "cacheKey" is custom "function": errors 2`] = `Array []`; +exports[`cache option should match snapshot for the "true" value: errors 2`] = `Array []`; -exports[`cache option should match snapshot for the "true" value when "cacheKey" is custom "function": warnings 1`] = `Array []`; +exports[`cache option should match snapshot for the "true" value: warnings 1`] = `Array []`; -exports[`cache option should match snapshot for the "true" value when "cacheKey" is custom "function": warnings 2`] = `Array []`; +exports[`cache option should match snapshot for the "true" value: warnings 2`] = `Array []`; -exports[`cache option should match snapshot for the "true" value: assets 1`] = ` +exports[`cache option should match snapshot when "cacheKey" is custom "function": assets 1`] = ` Object { "five.js": "!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=4)}({4:function(e,t){e.exports=function(){console.log(7)}}});", "four.js": "!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){e.exports=class{}}});", @@ -97,7 +97,7 @@ Object { } `; -exports[`cache option should match snapshot for the "true" value: assets 2`] = ` +exports[`cache option should match snapshot when "cacheKey" is custom "function": assets 2`] = ` Object { "five.js": "!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=4)}({4:function(e,t){e.exports=function(){console.log(7)}}});", "four.js": "!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){e.exports=class{}}});", @@ -107,10 +107,38 @@ Object { } `; -exports[`cache option should match snapshot for the "true" value: errors 1`] = `Array []`; +exports[`cache option should match snapshot when "cacheKey" is custom "function": errors 1`] = `Array []`; -exports[`cache option should match snapshot for the "true" value: errors 2`] = `Array []`; +exports[`cache option should match snapshot when "cacheKey" is custom "function": errors 2`] = `Array []`; -exports[`cache option should match snapshot for the "true" value: warnings 1`] = `Array []`; +exports[`cache option should match snapshot when "cacheKey" is custom "function": warnings 1`] = `Array []`; -exports[`cache option should match snapshot for the "true" value: warnings 2`] = `Array []`; +exports[`cache option should match snapshot when "cacheKey" is custom "function": warnings 2`] = `Array []`; + +exports[`cache option should match snapshot when a value is not specify: assets 1`] = ` +Object { + "five.js": "!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=4)}({4:function(e,t){e.exports=function(){console.log(7)}}});", + "four.js": "!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){e.exports=class{}}});", + "one.js": "!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)}}]);", + "three.js": "!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=2)}({2:function(e,t){e.exports=()=>/test/}});", + "two.js": "!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){e.exports=\\"string\\"}]);", +} +`; + +exports[`cache option should match snapshot when a value is not specify: assets 2`] = ` +Object { + "five.js": "!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=4)}({4:function(e,t){e.exports=function(){console.log(7)}}});", + "four.js": "!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){e.exports=class{}}});", + "one.js": "!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)}}]);", + "three.js": "!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=2)}({2:function(e,t){e.exports=()=>/test/}});", + "two.js": "!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){e.exports=\\"string\\"}]);", +} +`; + +exports[`cache option should match snapshot when a value is not specify: errors 1`] = `Array []`; + +exports[`cache option should match snapshot when a value is not specify: errors 2`] = `Array []`; + +exports[`cache option should match snapshot when a value is not specify: warnings 1`] = `Array []`; + +exports[`cache option should match snapshot when a value is not specify: warnings 2`] = `Array []`; diff --git a/test/cache-option.test.js b/test/cache-option.test.js index 8038dc69..8532b33c 100644 --- a/test/cache-option.test.js +++ b/test/cache-option.test.js @@ -1,17 +1,34 @@ import cacache from 'cacache'; import findCacheDir from 'find-cache-dir'; +import TaskRunner from '../src/TaskRunner'; import TerserPlugin from '../src/index'; -import { createCompiler, compile, cleanErrorStack, getAssets } from './helpers'; - -const cacheDir = findCacheDir({ name: 'terser-webpack-plugin' }); +import { + createCompiler, + compile, + cleanErrorStack, + getAssets, + removeCache, +} from './helpers'; + +const uniqueCacheDirectory = findCacheDir({ name: 'unique-cache-directory' }); +const uniqueOtherDirectory = findCacheDir({ + name: 'unique-other-cache-directory', +}); const otherCacheDir = findCacheDir({ name: 'other-cache-directory' }); +const otherOtherCacheDir = findCacheDir({ + name: 'other-other-cache-directory', +}); + +jest.setTimeout(30000); describe('cache option', () => { let compiler; beforeEach(() => { + jest.clearAllMocks(); + compiler = createCompiler({ entry: { one: `${__dirname}/fixtures/cache.js`, @@ -23,20 +40,33 @@ describe('cache option', () => { }); return Promise.all([ - cacache.rm.all(cacheDir), - cacache.rm.all(otherCacheDir), + removeCache(), + removeCache(uniqueCacheDirectory), + removeCache(uniqueOtherDirectory), + removeCache(otherCacheDir), + removeCache(otherOtherCacheDir), ]); }); - afterEach(() => - Promise.all([cacache.rm.all(cacheDir), cacache.rm.all(otherCacheDir)]) - ); + afterEach(() => { + return Promise.all([ + removeCache(), + removeCache(uniqueCacheDirectory), + removeCache(uniqueOtherDirectory), + removeCache(otherCacheDir), + removeCache(otherOtherCacheDir), + ]); + }); - it('should match snapshot for the "false" value', async () => { - new TerserPlugin({ cache: false }).apply(compiler); + it('should match snapshot when a value is not specify', async () => { + const cacacheGetSpy = jest.spyOn(cacache, 'get'); + const cacachePutSpy = jest.spyOn(cacache, 'put'); - cacache.get = jest.fn(cacache.get); - cacache.put = jest.fn(cacache.put); + jest.spyOn(TaskRunner, 'getCacheDirectory').mockImplementation(() => { + return uniqueCacheDirectory; + }); + + new TerserPlugin({ cache: true }).apply(compiler); const stats = await compile(compiler); @@ -47,21 +77,62 @@ describe('cache option', () => { expect(warnings).toMatchSnapshot('warnings'); expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); - // Cache disabled so we don't run `get` or `put` - expect(cacache.get.mock.calls.length).toBe(0); - expect(cacache.put.mock.calls.length).toBe(0); + const countAssets = Object.keys(stats.compilation.assets).length; + + // Try to found cached files, but we don't have their in cache + expect(cacacheGetSpy).toHaveBeenCalledTimes(countAssets); + // Put files in cache + expect(cacachePutSpy).toHaveBeenCalledTimes(countAssets); + + cacache.get.mockClear(); + cacache.put.mockClear(); + + const newStats = await compile(compiler); + + const newErrors = newStats.compilation.errors.map(cleanErrorStack); + const newWarnings = newStats.compilation.warnings.map(cleanErrorStack); + + expect(newErrors).toMatchSnapshot('errors'); + expect(newWarnings).toMatchSnapshot('warnings'); + + expect(getAssets(newStats, compiler)).toMatchSnapshot('assets'); + + const newCountAssets = Object.keys(newStats.compilation.assets).length; + + // Now we have cached files so we get them and don't put new + expect(cacacheGetSpy).toHaveBeenCalledTimes(newCountAssets); + expect(cacachePutSpy).toHaveBeenCalledTimes(0); + }); + + it('should match snapshot for the "false" value', async () => { + const cacacheGetSpy = jest.spyOn(cacache, 'get'); + const cacachePutSpy = jest.spyOn(cacache, 'put'); + + new TerserPlugin({ cache: false }).apply(compiler); + + const stats = await compile(compiler); + + const errors = stats.compilation.errors.map(cleanErrorStack); + const warnings = stats.compilation.warnings.map(cleanErrorStack); - const cacheEntriesList = await cacache.ls(cacheDir); - const cacheKeys = Object.keys(cacheEntriesList); + expect(errors).toMatchSnapshot('errors'); + expect(warnings).toMatchSnapshot('warnings'); + expect(getAssets(stats, compiler)).toMatchSnapshot('assets'); - expect(cacheKeys.length).toBe(0); + // Cache disabled so we don't run `get` or `put` + expect(cacacheGetSpy).toHaveBeenCalledTimes(0); + expect(cacachePutSpy).toHaveBeenCalledTimes(0); }); it('should match snapshot for the "true" value', async () => { - new TerserPlugin({ cache: true }).apply(compiler); + const cacacheGetSpy = jest.spyOn(cacache, 'get'); + const cacachePutSpy = jest.spyOn(cacache, 'put'); + + jest.spyOn(TaskRunner, 'getCacheDirectory').mockImplementation(() => { + return uniqueOtherDirectory; + }); - cacache.get = jest.fn(cacache.get); - cacache.put = jest.fn(cacache.put); + new TerserPlugin({ cache: true }).apply(compiler); const stats = await compile(compiler); @@ -75,16 +146,9 @@ describe('cache option', () => { const countAssets = Object.keys(stats.compilation.assets).length; // Try to found cached files, but we don't have their in cache - expect(cacache.get.mock.calls.length).toBe(countAssets); + expect(cacacheGetSpy).toHaveBeenCalledTimes(countAssets); // Put files in cache - expect(cacache.put.mock.calls.length).toBe(countAssets); - - const cacheEntriesList = await cacache.ls(cacheDir); - - const cacheKeys = Object.keys(cacheEntriesList); - - // Make sure that we cached files - expect(cacheKeys.length).toBe(countAssets); + expect(cacachePutSpy).toHaveBeenCalledTimes(countAssets); cacache.get.mockClear(); cacache.put.mockClear(); @@ -102,15 +166,15 @@ describe('cache option', () => { const newCountAssets = Object.keys(newStats.compilation.assets).length; // Now we have cached files so we get them and don't put new - expect(cacache.get.mock.calls.length).toBe(newCountAssets); - expect(cacache.put.mock.calls.length).toBe(0); + expect(cacacheGetSpy).toHaveBeenCalledTimes(newCountAssets); + expect(cacachePutSpy).toHaveBeenCalledTimes(0); }); it('should match snapshot for the "other-cache-directory" value', async () => { - new TerserPlugin({ cache: otherCacheDir }).apply(compiler); + const cacacheGetSpy = jest.spyOn(cacache, 'get'); + const cacachePutSpy = jest.spyOn(cacache, 'put'); - cacache.get = jest.fn(cacache.get); - cacache.put = jest.fn(cacache.put); + new TerserPlugin({ cache: otherCacheDir }).apply(compiler); const stats = await compile(compiler); @@ -124,15 +188,9 @@ describe('cache option', () => { const countAssets = Object.keys(stats.compilation.assets).length; // Try to found cached files, but we don't have their in cache - expect(cacache.get.mock.calls.length).toBe(countAssets); + expect(cacacheGetSpy).toHaveBeenCalledTimes(countAssets); // Put files in cache - expect(cacache.put.mock.calls.length).toBe(countAssets); - - const cacheEntriesList = await cacache.ls(otherCacheDir); - const cacheKeys = Object.keys(cacheEntriesList); - - // Make sure that we cached files - expect(cacheKeys.length).toBe(countAssets); + expect(cacachePutSpy).toHaveBeenCalledTimes(countAssets); cacache.get.mockClear(); cacache.put.mockClear(); @@ -149,14 +207,17 @@ describe('cache option', () => { const newCountAssets = Object.keys(newStats.compilation.assets).length; - // Now we have cached files so we get their and don't put - expect(cacache.get.mock.calls.length).toBe(newCountAssets); - expect(cacache.put.mock.calls.length).toBe(0); + // Now we have cached files so we get them and don't put new + expect(cacacheGetSpy).toHaveBeenCalledTimes(newCountAssets); + expect(cacachePutSpy).toHaveBeenCalledTimes(0); }); - it('should match snapshot for the "true" value when "cacheKey" is custom "function"', async () => { + it('should match snapshot when "cacheKey" is custom "function"', async () => { + const cacacheGetSpy = jest.spyOn(cacache, 'get'); + const cacachePutSpy = jest.spyOn(cacache, 'put'); + new TerserPlugin({ - cache: true, + cache: otherOtherCacheDir, cacheKeys: (defaultCacheKeys, file) => { // eslint-disable-next-line no-param-reassign defaultCacheKeys.myCacheKey = 1; @@ -167,9 +228,6 @@ describe('cache option', () => { }, }).apply(compiler); - cacache.get = jest.fn(cacache.get); - cacache.put = jest.fn(cacache.put); - const stats = await compile(compiler); const errors = stats.compilation.errors.map(cleanErrorStack); @@ -182,25 +240,9 @@ describe('cache option', () => { const countAssets = Object.keys(stats.compilation.assets).length; // Try to found cached files, but we don't have their in cache - expect(cacache.get.mock.calls.length).toBe(countAssets); + expect(cacacheGetSpy).toHaveBeenCalledTimes(countAssets); // Put files in cache - expect(cacache.put.mock.calls.length).toBe(countAssets); - - const cacheEntriesList = await cacache.ls(cacheDir); - const cacheKeys = Object.keys(cacheEntriesList); - - // Make sure that we cached files - expect(cacheKeys.length).toBe(countAssets); - - cacheKeys.forEach((cacheEntry) => { - // eslint-disable-next-line no-new-func - const cacheEntryOptions = new Function( - `'use strict'\nreturn ${cacheEntry}` - )(); - - expect(cacheEntryOptions.myCacheKey).toBe(1); - expect(cacheEntryOptions.myCacheKeyBasedOnFile).toMatch(/file-(.+)?\.js/); - }); + expect(cacachePutSpy).toHaveBeenCalledTimes(countAssets); cacache.get.mockClear(); cacache.put.mockClear(); @@ -216,9 +258,9 @@ describe('cache option', () => { const newCountAssets = Object.keys(newStats.compilation.assets).length; - // Now we have cached files so we get their and don't put - expect(cacache.get.mock.calls.length).toBe(newCountAssets); - expect(cacache.put.mock.calls.length).toBe(0); + // Now we have cached files so we get them and don't put new + expect(cacacheGetSpy).toHaveBeenCalledTimes(newCountAssets); + expect(cacachePutSpy).toHaveBeenCalledTimes(0); }); it('should match snapshot for errors into the "cacheKeys" option', async () => { diff --git a/test/chunkFilter-option.test.js b/test/chunkFilter-option.test.js index 46e40050..b9902482 100644 --- a/test/chunkFilter-option.test.js +++ b/test/chunkFilter-option.test.js @@ -1,6 +1,12 @@ import TerserPlugin from '../src/index'; -import { cleanErrorStack, createCompiler, compile, getAssets } from './helpers'; +import { + cleanErrorStack, + createCompiler, + compile, + getAssets, + removeCache, +} from './helpers'; describe('chunkFilter option', () => { let compiler; @@ -12,8 +18,12 @@ describe('chunkFilter option', () => { entry: `${__dirname}/fixtures/entry.js`, }, }); + + return Promise.all([removeCache()]); }); + afterEach(() => Promise.all([removeCache()])); + it('should match snapshot for a Function value', async () => { new TerserPlugin({ chunkFilter: (chunk) => { diff --git a/test/exclude-option.test.js b/test/exclude-option.test.js index 640f0c4c..5d3120d0 100644 --- a/test/exclude-option.test.js +++ b/test/exclude-option.test.js @@ -1,6 +1,12 @@ import TerserPlugin from '../src/index'; -import { cleanErrorStack, createCompiler, compile, getAssets } from './helpers'; +import { + cleanErrorStack, + createCompiler, + compile, + getAssets, + removeCache, +} from './helpers'; describe('exclude option', () => { let compiler; @@ -13,8 +19,12 @@ describe('exclude option', () => { entry: `${__dirname}/fixtures/entry.js`, }, }); + + return Promise.all([removeCache()]); }); + afterEach(() => Promise.all([removeCache()])); + it('should match snapshot for a single RegExp value', async () => { new TerserPlugin({ exclude: /excluded1/i, diff --git a/test/extractComments-option.test.js b/test/extractComments-option.test.js index ee548072..7d05e786 100644 --- a/test/extractComments-option.test.js +++ b/test/extractComments-option.test.js @@ -1,6 +1,12 @@ import TerserPlugin from '../src/index'; -import { cleanErrorStack, compile, createCompiler, getAssets } from './helpers'; +import { + cleanErrorStack, + compile, + createCompiler, + getAssets, + removeCache, +} from './helpers'; describe('extractComments option', () => { let compiler; @@ -18,8 +24,12 @@ describe('extractComments option', () => { chunkFilename: 'chunks/[id].[name].[chunkhash].js', }, }); + + return Promise.all([removeCache()]); }); + afterEach(() => Promise.all([removeCache()])); + it('should match snapshot when a value is not specify', async () => { new TerserPlugin().apply(compiler); diff --git a/test/helpers/getCacheDirectory.js b/test/helpers/getCacheDirectory.js new file mode 100644 index 00000000..550823fd --- /dev/null +++ b/test/helpers/getCacheDirectory.js @@ -0,0 +1,9 @@ +import os from 'os'; + +import findCacheDir from 'find-cache-dir'; + +function getCacheDirectory() { + return findCacheDir({ name: 'terser-webpack-plugin' }) || os.tmpdir(); +} + +export default getCacheDirectory; diff --git a/test/helpers/index.js b/test/helpers/index.js index d1ba5472..09460eeb 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -3,5 +3,15 @@ import compile from './compile'; import countPlugins from './countPlugins'; import createCompiler from './createCompiler'; import getAssets from './getAssets'; +import getCacheDirectory from './getCacheDirectory'; +import removeCache from './removeCache'; -export { cleanErrorStack, compile, countPlugins, createCompiler, getAssets }; +export { + cleanErrorStack, + compile, + countPlugins, + createCompiler, + getAssets, + getCacheDirectory, + removeCache, +}; diff --git a/test/helpers/removeCache.js b/test/helpers/removeCache.js new file mode 100644 index 00000000..05c2f819 --- /dev/null +++ b/test/helpers/removeCache.js @@ -0,0 +1,11 @@ +import cacache from 'cacache'; + +import getCacheDirectory from './getCacheDirectory'; + +async function removeCache(cacheDirectory) { + const cacheDir = cacheDirectory || getCacheDirectory(); + + return cacache.rm.all(cacheDir); +} + +export default removeCache; diff --git a/test/include-option.test.js b/test/include-option.test.js index ee14d5fd..4bca3562 100644 --- a/test/include-option.test.js +++ b/test/include-option.test.js @@ -1,6 +1,12 @@ import TerserPlugin from '../src/index'; -import { cleanErrorStack, createCompiler, compile, getAssets } from './helpers'; +import { + cleanErrorStack, + createCompiler, + compile, + getAssets, + removeCache, +} from './helpers'; describe('include option', () => { let compiler; @@ -13,8 +19,12 @@ describe('include option', () => { entry: `${__dirname}/fixtures/entry.js`, }, }); + + return Promise.all([removeCache()]); }); + afterEach(() => Promise.all([removeCache()])); + it('should match snapshot for a single RegExp value', async () => { new TerserPlugin({ include: /included1/i, diff --git a/test/minify-option.test.js b/test/minify-option.test.js index cde29412..fc93f58c 100644 --- a/test/minify-option.test.js +++ b/test/minify-option.test.js @@ -1,6 +1,12 @@ import TerserPlugin from '../src'; -import { cleanErrorStack, compile, createCompiler, getAssets } from './helpers'; +import { + cleanErrorStack, + compile, + createCompiler, + getAssets, + removeCache, +} from './helpers'; // Based on https://github.com/facebook/jest/blob/edde20f75665c2b1e3c8937f758902b5cf28a7b4/packages/jest-runner/src/__tests__/test_runner.test.js @@ -18,6 +24,10 @@ jest.mock('worker-farm', () => { }); describe('minify option', () => { + beforeEach(() => Promise.all([removeCache()])); + + afterEach(() => Promise.all([removeCache()])); + it('should snapshot for the "uglify-js" minifier', async () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/minify/es5.js`, diff --git a/test/parallel-option-failure.test.js b/test/parallel-option-failure.test.js index 2cc7735a..0ce26ff6 100644 --- a/test/parallel-option-failure.test.js +++ b/test/parallel-option-failure.test.js @@ -1,12 +1,15 @@ import os from 'os'; -import cacache from 'cacache'; import workerFarm from 'worker-farm'; -import findCacheDir from 'find-cache-dir'; import TerserPlugin from '../src/index'; -import { createCompiler, compile, cleanErrorStack } from './helpers'; +import { + createCompiler, + compile, + cleanErrorStack, + removeCache, +} from './helpers'; jest.mock('os', () => { const actualOs = require.requireActual('os'); @@ -34,8 +37,6 @@ jest.mock('worker-farm', () => { return mock; }); -const cacheDir = findCacheDir({ name: 'terser-webpack-plugin' }); - describe('parallel option', () => { let compiler; @@ -50,9 +51,11 @@ describe('parallel option', () => { }, }); - return cacache.rm.all(cacheDir); + return Promise.all([removeCache()]); }); + afterEach(() => Promise.all([removeCache()])); + it('should match snapshot for errors into the "worker-farm" package', async () => { new TerserPlugin({ parallel: true, cache: false }).apply(compiler); diff --git a/test/parallel-option.test.js b/test/parallel-option.test.js index ad22ce3d..7456a8c4 100644 --- a/test/parallel-option.test.js +++ b/test/parallel-option.test.js @@ -4,7 +4,13 @@ import workerFarm from 'worker-farm'; import TerserPlugin from '../src/index'; -import { createCompiler, compile, cleanErrorStack, getAssets } from './helpers'; +import { + createCompiler, + compile, + cleanErrorStack, + getAssets, + removeCache, +} from './helpers'; jest.mock('os', () => { const actualOs = require.requireActual('os'); @@ -49,8 +55,12 @@ describe('parallel option', () => { four: `${__dirname}/fixtures/entry.js`, }, }); + + return Promise.all([removeCache()]); }); + afterEach(() => Promise.all([removeCache()])); + it('should match snapshot when a value is not specify', async () => { new TerserPlugin().apply(compiler); diff --git a/test/sourceMap-option.test.js b/test/sourceMap-option.test.js index 4913564a..34c23d8f 100644 --- a/test/sourceMap-option.test.js +++ b/test/sourceMap-option.test.js @@ -1,6 +1,12 @@ import TerserPlugin from '../src/index'; -import { createCompiler, compile, cleanErrorStack, getAssets } from './helpers'; +import { + createCompiler, + compile, + cleanErrorStack, + getAssets, + removeCache, +} from './helpers'; expect.addSnapshotSerializer({ test: (value) => { @@ -19,6 +25,10 @@ expect.addSnapshotSerializer({ }); describe('when options.sourceMap', () => { + beforeEach(() => Promise.all([removeCache()])); + + afterEach(() => Promise.all([removeCache()])); + it('matches snapshot for a single `false` value (`devtool` is `source-map`)', async () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/entry.js`, diff --git a/test/supports-multicompiler.test.js b/test/supports-multicompiler.test.js index fab75d26..04658644 100644 --- a/test/supports-multicompiler.test.js +++ b/test/supports-multicompiler.test.js @@ -9,9 +9,14 @@ import { countPlugins, compile, getAssets, + removeCache, } from './helpers'; describe('multi-compiler mode', () => { + beforeEach(() => Promise.all([removeCache()])); + + afterEach(() => Promise.all([removeCache()])); + it('should match snapshot with empty options', async () => { const multiCompiler = createCompiler([ { diff --git a/test/terserOptions-option.test.js b/test/terserOptions-option.test.js index 2b80e3c6..78ee0d1f 100644 --- a/test/terserOptions-option.test.js +++ b/test/terserOptions-option.test.js @@ -1,8 +1,18 @@ import TerserPlugin from '../src/index'; -import { cleanErrorStack, createCompiler, compile, getAssets } from './helpers'; +import { + cleanErrorStack, + createCompiler, + compile, + getAssets, + removeCache, +} from './helpers'; describe('terserOptions option', () => { + beforeEach(() => Promise.all([removeCache()])); + + afterEach(() => Promise.all([removeCache()])); + it('should match snapshot for the "ecma" option with the "5" value', async () => { const compiler = createCompiler({ entry: `${__dirname}/fixtures/ecma-5/entry.js`, diff --git a/test/test-option.test.js b/test/test-option.test.js index 73a99df5..f098648f 100644 --- a/test/test-option.test.js +++ b/test/test-option.test.js @@ -1,6 +1,12 @@ import TerserPlugin from '../src/index'; -import { cleanErrorStack, createCompiler, compile, getAssets } from './helpers'; +import { + cleanErrorStack, + createCompiler, + compile, + getAssets, + removeCache, +} from './helpers'; describe('test option', () => { let compiler; @@ -19,8 +25,12 @@ describe('test option', () => { chunkFilename: '[id].[name].js?ver=[hash]', }, }); + + return Promise.all([removeCache()]); }); + afterEach(() => Promise.all([removeCache()])); + it('should match snapshot with empty value', async () => { new TerserPlugin().apply(compiler); diff --git a/test/warningsFilter-option.test.js b/test/warningsFilter-option.test.js index 4f125d4f..9a8851d1 100644 --- a/test/warningsFilter-option.test.js +++ b/test/warningsFilter-option.test.js @@ -1,6 +1,12 @@ import TerserPlugin from '../src/index'; -import { cleanErrorStack, createCompiler, compile, getAssets } from './helpers'; +import { + cleanErrorStack, + createCompiler, + compile, + getAssets, + removeCache, +} from './helpers'; describe('warningsFilter option', () => { let compiler; @@ -12,8 +18,12 @@ describe('warningsFilter option', () => { two: `${__dirname}/fixtures/unreachable-code-2.js`, }, }); + + return Promise.all([removeCache()]); }); + afterEach(() => Promise.all([removeCache()])); + it('should match snapshot for a "function" value and the "sourceMap" option is "false" (filter by message)', async () => { new TerserPlugin({ warningsFilter(warning) {