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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Webpack4Cache.js
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
@@ -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 { getHasher } from './hash-helper';

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 = getHasher(this.compiler)
.update(serialize(task.cacheKeys))
.digest('hex');

Expand Down
16 changes: 16 additions & 0 deletions src/hash-helper.js
@@ -0,0 +1,16 @@
import crypto from 'crypto';

export function 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 getHasher;
JoshMcCullough marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 3 additions & 4 deletions src/index.js
@@ -1,4 +1,3 @@
import crypto from 'crypto';
import path from 'path';

import { SourceMapConsumer } from 'source-map';
Expand All @@ -16,6 +15,7 @@ import terserPackageJson from 'terser/package.json';

import schema from './options.json';
import TaskRunner from './TaskRunner';
import { getHasher } from './hash-helper';

const warningRegex = /\[.+:([0-9]+),([0-9]+)\]/;

Expand Down Expand Up @@ -394,8 +394,7 @@ class TerserPlugin {
'terser-webpack-plugin-options': this.options,
nodeVersion: process.version,
filename: file,
contentHash: crypto
.createHash('md4')
contentHash: 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
42 changes: 42 additions & 0 deletions test/hash-helper.test.js
@@ -0,0 +1,42 @@
import crypto from 'crypto';

import { getHasher } from '../src/hash-helper';

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 = 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 = 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 = 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 = getHasher(compiler);

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