Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ea33463

Browse files
feat: enable cache by default for webpack@4 (#164)
BREAKING CHANGE: the `cache` is `true` by default for webpack@4
1 parent d07d854 commit ea33463

16 files changed

+73
-2
lines changed

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CompressionPlugin {
2626
test,
2727
include,
2828
exclude,
29-
cache = false,
29+
cache = true,
3030
algorithm = 'gzip',
3131
compressionOptions = {},
3232
filename = '[path].gz[query]',

test/CompressionPlugin.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ import {
66
getCompiler,
77
getErrors,
88
getWarnings,
9+
removeCache,
910
} from './helpers/index';
1011

1112
describe('CompressionPlugin', () => {
13+
beforeEach(() => {
14+
return removeCache();
15+
});
16+
1217
it('should work', async () => {
1318
const compiler = getCompiler(
1419
'./entry.js',

test/algorithm.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import {
66
getCompiler,
77
getErrors,
88
getWarnings,
9+
removeCache,
910
} from './helpers/index';
1011

1112
describe('"algorithm" option', () => {
1213
let compiler;
1314

1415
beforeEach(() => {
1516
compiler = getCompiler('./entry.js');
17+
18+
return removeCache();
1619
});
1720

1821
it('matches snapshot for `unknown` value ({String})', () => {

test/cache-option.test.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import findCacheDir from 'find-cache-dir';
66
import del from 'del';
77

88
import CompressionPlugin from '../src/index';
9+
import Webpack4Cache from '../src/Webpack4Cache';
910

1011
import {
1112
compile,
@@ -17,13 +18,15 @@ import {
1718

1819
const cacheDir = findCacheDir({ name: 'compression-webpack-plugin' });
1920
const otherCacheDir = findCacheDir({ name: 'other-cache-directory' });
21+
const uniqueDirectory = findCacheDir({ name: 'unique-cache-directory' });
2022

2123
if (getCompiler.isWebpack4()) {
2224
describe('"cache" option', () => {
2325
beforeEach(() => {
2426
return Promise.all([
2527
cacache.rm.all(cacheDir),
2628
cacache.rm.all(otherCacheDir),
29+
cacache.rm.all(uniqueDirectory),
2730
]);
2831
});
2932

@@ -65,6 +68,12 @@ if (getCompiler.isWebpack4()) {
6568
cacache.get = jest.fn(cacache.get);
6669
cacache.put = jest.fn(cacache.put);
6770

71+
const getCacheDirectorySpy = jest
72+
.spyOn(Webpack4Cache, 'getCacheDirectory')
73+
.mockImplementation(() => {
74+
return uniqueDirectory;
75+
});
76+
6877
const stats = await compile(beforeCacheCompiler);
6978

7079
expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
@@ -78,7 +87,7 @@ if (getCompiler.isWebpack4()) {
7887
// Put files in cache
7988
expect(cacache.put.mock.calls.length).toBe(countAssets / 2);
8089

81-
const cacheEntriesList = await cacache.ls(cacheDir);
90+
const cacheEntriesList = await cacache.ls(uniqueDirectory);
8291

8392
const cacheKeys = Object.keys(cacheEntriesList);
8493

@@ -117,6 +126,8 @@ if (getCompiler.isWebpack4()) {
117126
// Now we have cached files so we get their and don't put
118127
expect(cacache.get.mock.calls.length).toBe(newCountAssets / 2);
119128
expect(cacache.put.mock.calls.length).toBe(0);
129+
130+
getCacheDirectorySpy.mockRestore();
120131
});
121132

122133
it('matches snapshot for `other-cache-directory` value ({String})', async () => {

test/compressionOptions-option.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import {
66
getCompiler,
77
getErrors,
88
getWarnings,
9+
removeCache,
910
} from './helpers/index';
1011

1112
describe('"compressionOptions" option', () => {
1213
let compiler;
1314

1415
beforeEach(() => {
1516
compiler = getCompiler('./entry.js');
17+
18+
return removeCache();
1619
});
1720

1821
it('matches snapshot without values', async () => {

test/deleteOriginalAssets.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import {
66
getCompiler,
77
getErrors,
88
getWarnings,
9+
removeCache,
910
} from './helpers/index';
1011

1112
describe('"deleteOriginalAssets" option', () => {
1213
let compiler;
1314

1415
beforeEach(() => {
1516
compiler = getCompiler('./entry.js');
17+
18+
return removeCache();
1619
});
1720

1821
it('matches snapshot for `true` value ({Boolean})', async () => {

test/exclude-option.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getCompiler,
77
getErrors,
88
getWarnings,
9+
removeCache,
910
} from './helpers/index';
1011

1112
describe('"exclude" option', () => {
@@ -23,6 +24,8 @@ describe('"exclude" option', () => {
2324
},
2425
}
2526
);
27+
28+
return removeCache();
2629
});
2730

2831
it('matches snapshot for a single `exclude` value ({RegExp})', async () => {

test/filename-option.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getCompiler,
77
getErrors,
88
getWarnings,
9+
removeCache,
910
} from './helpers/index';
1011

1112
describe('"filename" option', () => {
@@ -23,6 +24,8 @@ describe('"filename" option', () => {
2324
},
2425
}
2526
);
27+
28+
return removeCache();
2629
});
2730

2831
it('matches snapshot for `[path].super-compressed.gz[query]` value ({String})', async () => {

test/helpers/getCacheDirectory.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import os from 'os';
2+
3+
import findCacheDir from 'find-cache-dir';
4+
5+
function getCacheDirectory() {
6+
return findCacheDir({ name: 'compression-webpack-plugin' }) || os.tmpdir();
7+
}
8+
9+
export default getCacheDirectory;

test/helpers/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import getWarnings from './getWarnings';
77
import normalizeErrors from './normalizeErrors';
88
import readAsset from './readAsset';
99
import readsAssets from './readAssets';
10+
import removeCache from './removeCache';
1011

1112
export {
1213
compile,
@@ -18,4 +19,5 @@ export {
1819
normalizeErrors,
1920
readAsset,
2021
readsAssets,
22+
removeCache,
2123
};

test/helpers/removeCache.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import cacache from 'cacache';
2+
3+
import getCacheDirectory from './getCacheDirectory';
4+
5+
async function removeCache(cacheDirectory) {
6+
const cacheDir = cacheDirectory || getCacheDirectory();
7+
8+
return cacache.rm.all(cacheDir);
9+
}
10+
11+
export default removeCache;

test/include-option.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getCompiler,
77
getErrors,
88
getWarnings,
9+
removeCache,
910
} from './helpers/index';
1011

1112
describe('"include" option', () => {
@@ -23,6 +24,8 @@ describe('"include" option', () => {
2324
},
2425
}
2526
);
27+
28+
return removeCache();
2629
});
2730

2831
it('matches snapshot for a single `include` value ({RegExp})', async () => {

test/minRatio-option.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import {
66
getCompiler,
77
getErrors,
88
getWarnings,
9+
removeCache,
910
} from './helpers/index';
1011

1112
describe('"minRatio" option', () => {
1213
let compiler;
1314

1415
beforeEach(() => {
1516
compiler = getCompiler('./entry.js');
17+
18+
return removeCache();
1619
});
1720

1821
it('matches snapshot for `0` value ({Number})', async () => {

test/test-option.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getCompiler,
77
getErrors,
88
getWarnings,
9+
removeCache,
910
} from './helpers/index';
1011

1112
describe('"test" option', () => {
@@ -23,6 +24,8 @@ describe('"test" option', () => {
2324
},
2425
}
2526
);
27+
28+
return removeCache();
2629
});
2730

2831
it('matches snapshot with empty `test` value', async () => {

test/threshold-option.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import {
66
getCompiler,
77
getErrors,
88
getWarnings,
9+
removeCache,
910
} from './helpers/index';
1011

1112
describe('"threshold" option', () => {
1213
let compiler;
1314

1415
beforeEach(() => {
1516
compiler = getCompiler('./entry.js');
17+
18+
return removeCache();
1619
});
1720

1821
it('matches snapshot for `0` value ({Number})', async () => {

test/validate-options.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import CompressionPlugin from '../src';
22

3+
import { removeCache } from './helpers';
4+
35
describe('validate options', () => {
6+
beforeEach(() => {
7+
return removeCache();
8+
});
9+
410
const tests = {
511
test: {
612
success: [

0 commit comments

Comments
 (0)