Skip to content

Commit

Permalink
feat: enable cache by default for webpack@4 (#164)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the `cache` is `true` by default for webpack@4
  • Loading branch information
evilebottnawi committed May 12, 2020
1 parent d07d854 commit ea33463
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CompressionPlugin {
test,
include,
exclude,
cache = false,
cache = true,
algorithm = 'gzip',
compressionOptions = {},
filename = '[path].gz[query]',
Expand Down
5 changes: 5 additions & 0 deletions test/CompressionPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import {
getCompiler,
getErrors,
getWarnings,
removeCache,
} from './helpers/index';

describe('CompressionPlugin', () => {
beforeEach(() => {
return removeCache();
});

it('should work', async () => {
const compiler = getCompiler(
'./entry.js',
Expand Down
3 changes: 3 additions & 0 deletions test/algorithm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import {
getCompiler,
getErrors,
getWarnings,
removeCache,
} from './helpers/index';

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

beforeEach(() => {
compiler = getCompiler('./entry.js');

return removeCache();
});

it('matches snapshot for `unknown` value ({String})', () => {
Expand Down
13 changes: 12 additions & 1 deletion test/cache-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import findCacheDir from 'find-cache-dir';
import del from 'del';

import CompressionPlugin from '../src/index';
import Webpack4Cache from '../src/Webpack4Cache';

import {
compile,
Expand All @@ -17,13 +18,15 @@ import {

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

if (getCompiler.isWebpack4()) {
describe('"cache" option', () => {
beforeEach(() => {
return Promise.all([
cacache.rm.all(cacheDir),
cacache.rm.all(otherCacheDir),
cacache.rm.all(uniqueDirectory),
]);
});

Expand Down Expand Up @@ -65,6 +68,12 @@ if (getCompiler.isWebpack4()) {
cacache.get = jest.fn(cacache.get);
cacache.put = jest.fn(cacache.put);

const getCacheDirectorySpy = jest
.spyOn(Webpack4Cache, 'getCacheDirectory')
.mockImplementation(() => {
return uniqueDirectory;
});

const stats = await compile(beforeCacheCompiler);

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

const cacheEntriesList = await cacache.ls(cacheDir);
const cacheEntriesList = await cacache.ls(uniqueDirectory);

const cacheKeys = Object.keys(cacheEntriesList);

Expand Down Expand Up @@ -117,6 +126,8 @@ if (getCompiler.isWebpack4()) {
// Now we have cached files so we get their and don't put
expect(cacache.get.mock.calls.length).toBe(newCountAssets / 2);
expect(cacache.put.mock.calls.length).toBe(0);

getCacheDirectorySpy.mockRestore();
});

it('matches snapshot for `other-cache-directory` value ({String})', async () => {
Expand Down
3 changes: 3 additions & 0 deletions test/compressionOptions-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import {
getCompiler,
getErrors,
getWarnings,
removeCache,
} from './helpers/index';

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

beforeEach(() => {
compiler = getCompiler('./entry.js');

return removeCache();
});

it('matches snapshot without values', async () => {
Expand Down
3 changes: 3 additions & 0 deletions test/deleteOriginalAssets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import {
getCompiler,
getErrors,
getWarnings,
removeCache,
} from './helpers/index';

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

beforeEach(() => {
compiler = getCompiler('./entry.js');

return removeCache();
});

it('matches snapshot for `true` value ({Boolean})', async () => {
Expand Down
3 changes: 3 additions & 0 deletions test/exclude-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getCompiler,
getErrors,
getWarnings,
removeCache,
} from './helpers/index';

describe('"exclude" option', () => {
Expand All @@ -23,6 +24,8 @@ describe('"exclude" option', () => {
},
}
);

return removeCache();
});

it('matches snapshot for a single `exclude` value ({RegExp})', async () => {
Expand Down
3 changes: 3 additions & 0 deletions test/filename-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getCompiler,
getErrors,
getWarnings,
removeCache,
} from './helpers/index';

describe('"filename" option', () => {
Expand All @@ -23,6 +24,8 @@ describe('"filename" option', () => {
},
}
);

return removeCache();
});

it('matches snapshot for `[path].super-compressed.gz[query]` value ({String})', async () => {
Expand Down
9 changes: 9 additions & 0 deletions test/helpers/getCacheDirectory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os from 'os';

import findCacheDir from 'find-cache-dir';

function getCacheDirectory() {
return findCacheDir({ name: 'compression-webpack-plugin' }) || os.tmpdir();
}

export default getCacheDirectory;
2 changes: 2 additions & 0 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import getWarnings from './getWarnings';
import normalizeErrors from './normalizeErrors';
import readAsset from './readAsset';
import readsAssets from './readAssets';
import removeCache from './removeCache';

export {
compile,
Expand All @@ -18,4 +19,5 @@ export {
normalizeErrors,
readAsset,
readsAssets,
removeCache,
};
11 changes: 11 additions & 0 deletions test/helpers/removeCache.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions test/include-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getCompiler,
getErrors,
getWarnings,
removeCache,
} from './helpers/index';

describe('"include" option', () => {
Expand All @@ -23,6 +24,8 @@ describe('"include" option', () => {
},
}
);

return removeCache();
});

it('matches snapshot for a single `include` value ({RegExp})', async () => {
Expand Down
3 changes: 3 additions & 0 deletions test/minRatio-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import {
getCompiler,
getErrors,
getWarnings,
removeCache,
} from './helpers/index';

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

beforeEach(() => {
compiler = getCompiler('./entry.js');

return removeCache();
});

it('matches snapshot for `0` value ({Number})', async () => {
Expand Down
3 changes: 3 additions & 0 deletions test/test-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getCompiler,
getErrors,
getWarnings,
removeCache,
} from './helpers/index';

describe('"test" option', () => {
Expand All @@ -23,6 +24,8 @@ describe('"test" option', () => {
},
}
);

return removeCache();
});

it('matches snapshot with empty `test` value', async () => {
Expand Down
3 changes: 3 additions & 0 deletions test/threshold-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import {
getCompiler,
getErrors,
getWarnings,
removeCache,
} from './helpers/index';

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

beforeEach(() => {
compiler = getCompiler('./entry.js');

return removeCache();
});

it('matches snapshot for `0` value ({Number})', async () => {
Expand Down
6 changes: 6 additions & 0 deletions test/validate-options.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import CompressionPlugin from '../src';

import { removeCache } from './helpers';

describe('validate options', () => {
beforeEach(() => {
return removeCache();
});

const tests = {
test: {
success: [
Expand Down

0 comments on commit ea33463

Please sign in to comment.