Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.

fix: more consistent cache #385

Merged
merged 1 commit into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4,425 changes: 1,194 additions & 3,231 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,12 @@ class UglifyJsPlugin {
};

if (this.options.cache) {
const { outputPath } = compiler;
const defaultCacheKeys = {
// eslint-disable-next-line global-require
'uglify-js': require('uglify-js/package.json').version,
// eslint-disable-next-line global-require
'uglifyjs-webpack-plugin': require('../package.json').version,
'uglifyjs-webpack-plugin-options': this.options,
path: `${outputPath ? `${outputPath}/` : ''}${file}`,
hash: crypto
.createHash('md4')
.update(input)
Expand Down
305 changes: 163 additions & 142 deletions test/__snapshots__/cache-option.test.js.snap

Large diffs are not rendered by default.

60 changes: 29 additions & 31 deletions test/__snapshots__/test-option.test.js.snap

Large diffs are not rendered by default.

39 changes: 5 additions & 34 deletions test/cache-option.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import path from 'path';

import cacache from 'cacache';
import findCacheDir from 'find-cache-dir';

Expand All @@ -16,10 +14,11 @@ describe('when applied with `cache` option', () => {
beforeEach(() => {
compiler = createCompiler({
entry: {
one: `${__dirname}/fixtures/entry.js`,
two: `${__dirname}/fixtures/entry.js`,
three: `${__dirname}/fixtures/entry.js`,
four: `${__dirname}/fixtures/entry.js`,
one: `${__dirname}/fixtures/cache.js`,
two: `${__dirname}/fixtures/cache-1.js`,
three: `${__dirname}/fixtures/cache-2.js`,
four: `${__dirname}/fixtures/cache-3.js`,
five: `${__dirname}/fixtures/cache-4.js`,
},
});

Expand Down Expand Up @@ -105,18 +104,6 @@ describe('when applied with `cache` option', () => {
// 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}`
)();
const basename = path.basename(cacheEntryOptions.path);

expect([basename, cacheEntryOptions.hash]).toMatchSnapshot(
basename
);
});

cacache.get.mockClear();
cacache.put.mockClear();
})
Expand Down Expand Up @@ -192,18 +179,6 @@ describe('when applied with `cache` option', () => {
// 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}`
)();
const basename = path.basename(cacheEntryOptions.path);

expect([basename, cacheEntryOptions.hash]).toMatchSnapshot(
basename
);
});

cacache.get.mockClear();
cacache.put.mockClear();
})
Expand Down Expand Up @@ -294,15 +269,11 @@ describe('when applied with `cache` option', () => {
const cacheEntryOptions = new Function(
`'use strict'\nreturn ${cacheEntry}`
)();
const basename = path.basename(cacheEntryOptions.path);

expect(cacheEntryOptions.myCacheKey).toBe(1);
expect(cacheEntryOptions.myCacheKeyBasedOnFile).toMatch(
/file-(.+)?\.js/
);
expect([basename, cacheEntryOptions.hash]).toMatchSnapshot(
basename
);
});

cacache.get.mockClear();
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/cache-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'string';
1 change: 1 addition & 0 deletions test/fixtures/cache-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = function getRegExp() { return /test/ };
1 change: 1 addition & 0 deletions test/fixtures/cache-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = function foo() {};
9 changes: 9 additions & 0 deletions test/fixtures/cache-4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// foo
/* @preserve*/
// bar
var a = 2 + 2;

module.exports = function Foo() {
var b = 2 + 2;
console.log(b + 1 + 2);
};
9 changes: 9 additions & 0 deletions test/fixtures/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// foo
/* @preserve*/
// bar
var a = 2 + 2;

module.exports = function Foo() {
var b = 2 + 2;
console.log(b + 1 + 2);
};