Skip to content

Commit

Permalink
fix: use webpack hashFunction rather than hard-code MD4 (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshMcCullough committed Jan 29, 2020
1 parent 6b45dbe commit 330c1f6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
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 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
14 changes: 10 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 @@ -10,6 +9,7 @@ import {
javascript,
version as webpackVersion,
} from 'webpack';
import createHash from 'webpack/lib/util/createHash';
import validateOptions from 'schema-utils';
import serialize from 'serialize-javascript';
import terserPackageJson from 'terser/package.json';
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: 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,13 @@ class TerserPlugin {
);
});
}

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

return createHash(hashFunction || 'md4');
}
}

export default TerserPlugin;
45 changes: 45 additions & 0 deletions test/TerserPlugin.test.js
@@ -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,46 @@ 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', () => {
function sha1() {
return crypto.createHash('SHA1');
}

const compiler = {
output: { hashFunction: sha1 },
};
const hasher = TerserPlugin.getHasher(compiler);

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

0 comments on commit 330c1f6

Please sign in to comment.