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 all commits
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 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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use import { utils: { createHash } } from 'webpack', no need hardcore path to exported utils

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't destructure in an import statement.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use webpack.utils.createHash

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange, I will look at this in near future, anyway thanks for the PR

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);
});
});