Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use webpack hashFunction rather than hard-code MD4 #213

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/Webpack4Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import findCacheDir from 'find-cache-dir';
import serialize from 'serialize-javascript';

export default class Webpack4Cache {
constructor(compilation, options) {
constructor(compiler, compilation, options) {
this.options = options;
this.cacheDir =
options.cache === true
Expand Down
12 changes: 6 additions & 6 deletions src/Webpack5Cache.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import crypto from 'crypto';

// eslint-disable-next-line import/extensions,import/no-unresolved
import getLazyHashedEtag from 'webpack/lib/cache/getLazyHashedEtag';
import serialize from 'serialize-javascript';

import TerserPlugin from './index';

export default class Cache {
constructor(compilation, options) {
this.options = options;
constructor(compiler, compilation, options) {
this.compiler = compiler;
this.compilation = compilation;
this.options = options;
}

isEnabled() {
return !!this.compilation.cache;
}

createCacheIdent(task) {
const cacheKeys = crypto
.createHash('md4')
const cacheKeys = TerserPlugin.getHasher(this.compiler)
.update(serialize(task.cacheKeys))
.digest('hex');

Expand Down
18 changes: 15 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,7 @@ class TerserPlugin {
'terser-webpack-plugin-options': this.options,
nodeVersion: process.version,
filename: file,
contentHash: crypto
.createHash('md4')
contentHash: TerserPlugin.getHasher(compiler)
.update(input)
.digest('hex'),
};
Expand Down Expand Up @@ -499,7 +498,7 @@ class TerserPlugin {
const taskRunner = new TaskRunner({
taskGenerator,
files,
cache: new CacheEngine(compilation, this.options),
cache: new CacheEngine(compiler, compilation, this.options),
parallel: this.options.parallel,
});

Expand Down Expand Up @@ -567,6 +566,19 @@ class TerserPlugin {
);
});
}

static getHasher(compiler = null) {
const hashFunction =
compiler && compiler.output && compiler.output.hashFunction;

if (typeof hashFunction === 'string') {
return crypto.createHash(hashFunction);
} else if (typeof hashFunction === 'function') {
return hashFunction();
}

return crypto.createHash('md4');
}
}

export default TerserPlugin;
41 changes: 41 additions & 0 deletions test/TerserPlugin.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import crypto from 'crypto';

import RequestShortener from 'webpack/lib/RequestShortener';
import { javascript } from 'webpack';
import MainTemplate from 'webpack/lib/MainTemplate';
Expand Down Expand Up @@ -385,3 +387,42 @@ describe('TerserPlugin', () => {
).toMatchSnapshot();
});
});

const UNHASHED = 'this is some text';
const HASHED_MD4 = '565a21837631bdec2da173a5de2a2f87';
const HASHED_SHA1 = '0393694d16b84deb612e47ce6252bd35f0d86c06';

describe('getHasher', () => {
it('should return MD4 hasher with no compiler parameter', () => {
const hasher = TerserPlugin.getHasher();

expect(hasher).not.toBeNull();
expect(hasher.update(UNHASHED).digest('hex')).toEqual(HASHED_MD4);
});

it('should return MD4 hasher with incomplete compiler parameter', () => {
const compiler = { incomplete: { bad: {} } };
const hasher = TerserPlugin.getHasher(compiler);

expect(hasher).not.toBeNull();
expect(hasher.update(UNHASHED).digest('hex')).toEqual(HASHED_MD4);
});

it('should return hasher with string as hashFunction', () => {
const compiler = { output: { hashFunction: 'SHA1' } };
const hasher = TerserPlugin.getHasher(compiler);

expect(hasher).not.toBeNull();
expect(hasher.update(UNHASHED).digest('hex')).toEqual(HASHED_SHA1);
});

it('should return hasher with function as hashFunction', () => {
const compiler = {
output: { hashFunction: () => crypto.createHash('SHA1') },
};
const hasher = TerserPlugin.getHasher(compiler);

expect(hasher).not.toBeNull();
expect(hasher.update(UNHASHED).digest('hex')).toEqual(HASHED_SHA1);
});
});