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

Commit

Permalink
fix: more consistent cache (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Dec 19, 2018
1 parent 15e38e8 commit f61439d
Show file tree
Hide file tree
Showing 10 changed files with 1,412 additions and 3,440 deletions.
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
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
@@ -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
@@ -0,0 +1 @@
module.exports = 'string';
1 change: 1 addition & 0 deletions test/fixtures/cache-2.js
@@ -0,0 +1 @@
module.exports = function getRegExp() { return /test/ };
1 change: 1 addition & 0 deletions test/fixtures/cache-3.js
@@ -0,0 +1 @@
module.exports = function foo() {};
9 changes: 9 additions & 0 deletions test/fixtures/cache-4.js
@@ -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
@@ -0,0 +1,9 @@
// foo
/* @preserve*/
// bar
var a = 2 + 2;

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

0 comments on commit f61439d

Please sign in to comment.