diff --git a/src/Webpack4Cache.js b/src/Webpack4Cache.js index c4362de6..5dc5ae33 100644 --- a/src/Webpack4Cache.js +++ b/src/Webpack4Cache.js @@ -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 diff --git a/src/Webpack5Cache.js b/src/Webpack5Cache.js index ff0e6d83..e610703c 100644 --- a/src/Webpack5Cache.js +++ b/src/Webpack5Cache.js @@ -1,13 +1,14 @@ -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() { @@ -15,8 +16,7 @@ export default class Cache { } createCacheIdent(task) { - const cacheKeys = crypto - .createHash('md4') + const cacheKeys = TerserPlugin.getHasher(this.compiler) .update(serialize(task.cacheKeys)) .digest('hex'); diff --git a/src/index.js b/src/index.js index 85e7d403..af55891c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,3 @@ -import crypto from 'crypto'; import path from 'path'; import { SourceMapConsumer } from 'source-map'; @@ -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'; @@ -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'), }; @@ -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, }); @@ -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; diff --git a/test/TerserPlugin.test.js b/test/TerserPlugin.test.js index f73b614c..6fce4fc9 100644 --- a/test/TerserPlugin.test.js +++ b/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'; @@ -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); + }); +});