Skip to content

Commit 960d249

Browse files
authoredSep 4, 2019
feat: enable the cache option by default (#132)
BREAKING CHANGE: the `cache` option is `true` by default
1 parent 7b342af commit 960d249

21 files changed

+334
-128
lines changed
 

‎README.md

+1-20
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ module.exports = {
139139
### `cache`
140140

141141
Type: `Boolean|String`
142-
Default: `false`
142+
Default: `true`
143143

144144
Enable file caching.
145145
Default path to cache directory: `node_modules/.cache/terser-webpack-plugin`.
@@ -599,25 +599,6 @@ module.exports = {
599599

600600
## Examples
601601

602-
### Cache And Parallel
603-
604-
Enable cache and multi-process parallel running.
605-
606-
**webpack.config.js**
607-
608-
```js
609-
module.exports = {
610-
optimization: {
611-
minimizer: [
612-
new TerserPlugin({
613-
cache: true,
614-
parallel: true,
615-
}),
616-
],
617-
},
618-
};
619-
```
620-
621602
### Preserve Comments
622603

623604
Extract all legal comments (i.e. `/^\**!|@preserve|@license|@cc_on/i`) and preserve `/@license/i` comments.

‎src/TaskRunner.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,23 @@ const worker = require.resolve('./worker');
1313
export default class TaskRunner {
1414
constructor(options = {}) {
1515
const { cache, parallel } = options;
16-
this.cacheDir =
17-
cache === true
18-
? findCacheDir({ name: 'terser-webpack-plugin' }) || os.tmpdir()
19-
: cache;
16+
17+
this.cacheDir = cache === true ? TaskRunner.getCacheDirectory() : cache;
18+
this.maxConcurrentWorkers = TaskRunner.getMaxConcurrentWorkers(parallel);
19+
}
20+
21+
static getCacheDirectory() {
22+
return findCacheDir({ name: 'terser-webpack-plugin' }) || os.tmpdir();
23+
}
24+
25+
static getMaxConcurrentWorkers(parallel) {
2026
// In some cases cpus() returns undefined
2127
// https://github.com/nodejs/node/issues/19022
2228
const cpus = os.cpus() || { length: 1 };
29+
2330
// WSL sometimes freezes, error seems to be on the WSL side
2431
// https://github.com/webpack-contrib/terser-webpack-plugin/issues/21
25-
this.maxConcurrentWorkers = isWsl
32+
return isWsl
2633
? 1
2734
: parallel === true
2835
? cpus.length - 1

‎src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TerserPlugin {
2929
warningsFilter = () => true,
3030
extractComments = false,
3131
sourceMap = false,
32-
cache = false,
32+
cache = true,
3333
cacheKeys = (defaultCacheKeys) => defaultCacheKeys,
3434
parallel = true,
3535
include,

‎test/TerserPlugin.test.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import ChunkTemplate from 'webpack/lib/ChunkTemplate';
44

55
import TerserPlugin from '../src/index';
66

7-
import { cleanErrorStack, compile, createCompiler, getAssets } from './helpers';
7+
import {
8+
cleanErrorStack,
9+
compile,
10+
createCompiler,
11+
getAssets,
12+
removeCache,
13+
} from './helpers';
814

915
describe('TerserPlugin', () => {
1016
const rawSourceMap = {
@@ -22,6 +28,10 @@ describe('TerserPlugin', () => {
2228
mappings: '',
2329
};
2430

31+
beforeEach(() => Promise.all([removeCache()]));
32+
33+
afterEach(() => Promise.all([removeCache()]));
34+
2535
it('should work (without options)', async () => {
2636
const compiler = createCompiler();
2737

‎test/__snapshots__/cache-option.test.js.snap

+40-12
Large diffs are not rendered by default.

‎test/cache-option.test.js

+114-72
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
import cacache from 'cacache';
22
import findCacheDir from 'find-cache-dir';
33

4+
import TaskRunner from '../src/TaskRunner';
45
import TerserPlugin from '../src/index';
56

6-
import { createCompiler, compile, cleanErrorStack, getAssets } from './helpers';
7-
8-
const cacheDir = findCacheDir({ name: 'terser-webpack-plugin' });
7+
import {
8+
createCompiler,
9+
compile,
10+
cleanErrorStack,
11+
getAssets,
12+
removeCache,
13+
} from './helpers';
14+
15+
const uniqueCacheDirectory = findCacheDir({ name: 'unique-cache-directory' });
16+
const uniqueOtherDirectory = findCacheDir({
17+
name: 'unique-other-cache-directory',
18+
});
919
const otherCacheDir = findCacheDir({ name: 'other-cache-directory' });
20+
const otherOtherCacheDir = findCacheDir({
21+
name: 'other-other-cache-directory',
22+
});
23+
24+
jest.setTimeout(30000);
1025

1126
describe('cache option', () => {
1227
let compiler;
1328

1429
beforeEach(() => {
30+
jest.clearAllMocks();
31+
1532
compiler = createCompiler({
1633
entry: {
1734
one: `${__dirname}/fixtures/cache.js`,
@@ -23,20 +40,33 @@ describe('cache option', () => {
2340
});
2441

2542
return Promise.all([
26-
cacache.rm.all(cacheDir),
27-
cacache.rm.all(otherCacheDir),
43+
removeCache(),
44+
removeCache(uniqueCacheDirectory),
45+
removeCache(uniqueOtherDirectory),
46+
removeCache(otherCacheDir),
47+
removeCache(otherOtherCacheDir),
2848
]);
2949
});
3050

31-
afterEach(() =>
32-
Promise.all([cacache.rm.all(cacheDir), cacache.rm.all(otherCacheDir)])
33-
);
51+
afterEach(() => {
52+
return Promise.all([
53+
removeCache(),
54+
removeCache(uniqueCacheDirectory),
55+
removeCache(uniqueOtherDirectory),
56+
removeCache(otherCacheDir),
57+
removeCache(otherOtherCacheDir),
58+
]);
59+
});
3460

35-
it('should match snapshot for the "false" value', async () => {
36-
new TerserPlugin({ cache: false }).apply(compiler);
61+
it('should match snapshot when a value is not specify', async () => {
62+
const cacacheGetSpy = jest.spyOn(cacache, 'get');
63+
const cacachePutSpy = jest.spyOn(cacache, 'put');
3764

38-
cacache.get = jest.fn(cacache.get);
39-
cacache.put = jest.fn(cacache.put);
65+
jest.spyOn(TaskRunner, 'getCacheDirectory').mockImplementation(() => {
66+
return uniqueCacheDirectory;
67+
});
68+
69+
new TerserPlugin({ cache: true }).apply(compiler);
4070

4171
const stats = await compile(compiler);
4272

@@ -47,21 +77,62 @@ describe('cache option', () => {
4777
expect(warnings).toMatchSnapshot('warnings');
4878
expect(getAssets(stats, compiler)).toMatchSnapshot('assets');
4979

50-
// Cache disabled so we don't run `get` or `put`
51-
expect(cacache.get.mock.calls.length).toBe(0);
52-
expect(cacache.put.mock.calls.length).toBe(0);
80+
const countAssets = Object.keys(stats.compilation.assets).length;
81+
82+
// Try to found cached files, but we don't have their in cache
83+
expect(cacacheGetSpy).toHaveBeenCalledTimes(countAssets);
84+
// Put files in cache
85+
expect(cacachePutSpy).toHaveBeenCalledTimes(countAssets);
86+
87+
cacache.get.mockClear();
88+
cacache.put.mockClear();
89+
90+
const newStats = await compile(compiler);
91+
92+
const newErrors = newStats.compilation.errors.map(cleanErrorStack);
93+
const newWarnings = newStats.compilation.warnings.map(cleanErrorStack);
94+
95+
expect(newErrors).toMatchSnapshot('errors');
96+
expect(newWarnings).toMatchSnapshot('warnings');
97+
98+
expect(getAssets(newStats, compiler)).toMatchSnapshot('assets');
99+
100+
const newCountAssets = Object.keys(newStats.compilation.assets).length;
101+
102+
// Now we have cached files so we get them and don't put new
103+
expect(cacacheGetSpy).toHaveBeenCalledTimes(newCountAssets);
104+
expect(cacachePutSpy).toHaveBeenCalledTimes(0);
105+
});
106+
107+
it('should match snapshot for the "false" value', async () => {
108+
const cacacheGetSpy = jest.spyOn(cacache, 'get');
109+
const cacachePutSpy = jest.spyOn(cacache, 'put');
110+
111+
new TerserPlugin({ cache: false }).apply(compiler);
112+
113+
const stats = await compile(compiler);
114+
115+
const errors = stats.compilation.errors.map(cleanErrorStack);
116+
const warnings = stats.compilation.warnings.map(cleanErrorStack);
53117

54-
const cacheEntriesList = await cacache.ls(cacheDir);
55-
const cacheKeys = Object.keys(cacheEntriesList);
118+
expect(errors).toMatchSnapshot('errors');
119+
expect(warnings).toMatchSnapshot('warnings');
120+
expect(getAssets(stats, compiler)).toMatchSnapshot('assets');
56121

57-
expect(cacheKeys.length).toBe(0);
122+
// Cache disabled so we don't run `get` or `put`
123+
expect(cacacheGetSpy).toHaveBeenCalledTimes(0);
124+
expect(cacachePutSpy).toHaveBeenCalledTimes(0);
58125
});
59126

60127
it('should match snapshot for the "true" value', async () => {
61-
new TerserPlugin({ cache: true }).apply(compiler);
128+
const cacacheGetSpy = jest.spyOn(cacache, 'get');
129+
const cacachePutSpy = jest.spyOn(cacache, 'put');
130+
131+
jest.spyOn(TaskRunner, 'getCacheDirectory').mockImplementation(() => {
132+
return uniqueOtherDirectory;
133+
});
62134

63-
cacache.get = jest.fn(cacache.get);
64-
cacache.put = jest.fn(cacache.put);
135+
new TerserPlugin({ cache: true }).apply(compiler);
65136

66137
const stats = await compile(compiler);
67138

@@ -75,16 +146,9 @@ describe('cache option', () => {
75146
const countAssets = Object.keys(stats.compilation.assets).length;
76147

77148
// Try to found cached files, but we don't have their in cache
78-
expect(cacache.get.mock.calls.length).toBe(countAssets);
149+
expect(cacacheGetSpy).toHaveBeenCalledTimes(countAssets);
79150
// Put files in cache
80-
expect(cacache.put.mock.calls.length).toBe(countAssets);
81-
82-
const cacheEntriesList = await cacache.ls(cacheDir);
83-
84-
const cacheKeys = Object.keys(cacheEntriesList);
85-
86-
// Make sure that we cached files
87-
expect(cacheKeys.length).toBe(countAssets);
151+
expect(cacachePutSpy).toHaveBeenCalledTimes(countAssets);
88152

89153
cacache.get.mockClear();
90154
cacache.put.mockClear();
@@ -102,15 +166,15 @@ describe('cache option', () => {
102166
const newCountAssets = Object.keys(newStats.compilation.assets).length;
103167

104168
// Now we have cached files so we get them and don't put new
105-
expect(cacache.get.mock.calls.length).toBe(newCountAssets);
106-
expect(cacache.put.mock.calls.length).toBe(0);
169+
expect(cacacheGetSpy).toHaveBeenCalledTimes(newCountAssets);
170+
expect(cacachePutSpy).toHaveBeenCalledTimes(0);
107171
});
108172

109173
it('should match snapshot for the "other-cache-directory" value', async () => {
110-
new TerserPlugin({ cache: otherCacheDir }).apply(compiler);
174+
const cacacheGetSpy = jest.spyOn(cacache, 'get');
175+
const cacachePutSpy = jest.spyOn(cacache, 'put');
111176

112-
cacache.get = jest.fn(cacache.get);
113-
cacache.put = jest.fn(cacache.put);
177+
new TerserPlugin({ cache: otherCacheDir }).apply(compiler);
114178

115179
const stats = await compile(compiler);
116180

@@ -124,15 +188,9 @@ describe('cache option', () => {
124188
const countAssets = Object.keys(stats.compilation.assets).length;
125189

126190
// Try to found cached files, but we don't have their in cache
127-
expect(cacache.get.mock.calls.length).toBe(countAssets);
191+
expect(cacacheGetSpy).toHaveBeenCalledTimes(countAssets);
128192
// Put files in cache
129-
expect(cacache.put.mock.calls.length).toBe(countAssets);
130-
131-
const cacheEntriesList = await cacache.ls(otherCacheDir);
132-
const cacheKeys = Object.keys(cacheEntriesList);
133-
134-
// Make sure that we cached files
135-
expect(cacheKeys.length).toBe(countAssets);
193+
expect(cacachePutSpy).toHaveBeenCalledTimes(countAssets);
136194

137195
cacache.get.mockClear();
138196
cacache.put.mockClear();
@@ -149,14 +207,17 @@ describe('cache option', () => {
149207

150208
const newCountAssets = Object.keys(newStats.compilation.assets).length;
151209

152-
// Now we have cached files so we get their and don't put
153-
expect(cacache.get.mock.calls.length).toBe(newCountAssets);
154-
expect(cacache.put.mock.calls.length).toBe(0);
210+
// Now we have cached files so we get them and don't put new
211+
expect(cacacheGetSpy).toHaveBeenCalledTimes(newCountAssets);
212+
expect(cacachePutSpy).toHaveBeenCalledTimes(0);
155213
});
156214

157-
it('should match snapshot for the "true" value when "cacheKey" is custom "function"', async () => {
215+
it('should match snapshot when "cacheKey" is custom "function"', async () => {
216+
const cacacheGetSpy = jest.spyOn(cacache, 'get');
217+
const cacachePutSpy = jest.spyOn(cacache, 'put');
218+
158219
new TerserPlugin({
159-
cache: true,
220+
cache: otherOtherCacheDir,
160221
cacheKeys: (defaultCacheKeys, file) => {
161222
// eslint-disable-next-line no-param-reassign
162223
defaultCacheKeys.myCacheKey = 1;
@@ -167,9 +228,6 @@ describe('cache option', () => {
167228
},
168229
}).apply(compiler);
169230

170-
cacache.get = jest.fn(cacache.get);
171-
cacache.put = jest.fn(cacache.put);
172-
173231
const stats = await compile(compiler);
174232

175233
const errors = stats.compilation.errors.map(cleanErrorStack);
@@ -182,25 +240,9 @@ describe('cache option', () => {
182240
const countAssets = Object.keys(stats.compilation.assets).length;
183241

184242
// Try to found cached files, but we don't have their in cache
185-
expect(cacache.get.mock.calls.length).toBe(countAssets);
243+
expect(cacacheGetSpy).toHaveBeenCalledTimes(countAssets);
186244
// Put files in cache
187-
expect(cacache.put.mock.calls.length).toBe(countAssets);
188-
189-
const cacheEntriesList = await cacache.ls(cacheDir);
190-
const cacheKeys = Object.keys(cacheEntriesList);
191-
192-
// Make sure that we cached files
193-
expect(cacheKeys.length).toBe(countAssets);
194-
195-
cacheKeys.forEach((cacheEntry) => {
196-
// eslint-disable-next-line no-new-func
197-
const cacheEntryOptions = new Function(
198-
`'use strict'\nreturn ${cacheEntry}`
199-
)();
200-
201-
expect(cacheEntryOptions.myCacheKey).toBe(1);
202-
expect(cacheEntryOptions.myCacheKeyBasedOnFile).toMatch(/file-(.+)?\.js/);
203-
});
245+
expect(cacachePutSpy).toHaveBeenCalledTimes(countAssets);
204246

205247
cacache.get.mockClear();
206248
cacache.put.mockClear();
@@ -216,9 +258,9 @@ describe('cache option', () => {
216258

217259
const newCountAssets = Object.keys(newStats.compilation.assets).length;
218260

219-
// Now we have cached files so we get their and don't put
220-
expect(cacache.get.mock.calls.length).toBe(newCountAssets);
221-
expect(cacache.put.mock.calls.length).toBe(0);
261+
// Now we have cached files so we get them and don't put new
262+
expect(cacacheGetSpy).toHaveBeenCalledTimes(newCountAssets);
263+
expect(cacachePutSpy).toHaveBeenCalledTimes(0);
222264
});
223265

224266
it('should match snapshot for errors into the "cacheKeys" option', async () => {

0 commit comments

Comments
 (0)
Failed to load comments.