Skip to content

Commit 8387886

Browse files
SimenBIvan Vasilov
and
Ivan Vasilov
authored
fix: remove the hash calculation if hash exists (#159)
Co-authored-by: Ivan Vasilov <[email protected]>
1 parent e84e7d5 commit 8387886

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

__snapshots__/test.js.snap

+9
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ Object {
100100
}
101101
`;
102102

103+
exports[`should use the hash from HtmlWebpackPlugin if option is set 1`] = `
104+
Object {
105+
"css": Array [],
106+
"js": Array [
107+
"my-file.js?testHash",
108+
],
109+
}
110+
`;
111+
103112
exports[`should used passed in publicPath 1`] = `
104113
Object {
105114
"css": Array [],

src/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,14 @@ export default class AddAssetHtmlPlugin {
143143

144144
let suffix = '';
145145
if (hash) {
146-
const md5 = crypto.createHash('md5');
147-
md5.update(compilation.assets[addedFilename].source());
148-
suffix = `?${md5.digest('hex').substr(0, 20)}`;
146+
// if the hash is set by html-webpack-plugin use that hash, else generate a new one
147+
if (compilation.hash) {
148+
suffix = `?${compilation.hash}`;
149+
} else {
150+
const md5 = crypto.createHash('md5');
151+
md5.update(compilation.assets[addedFilename].source());
152+
suffix = `?${md5.digest('hex').substr(0, 20)}`;
153+
}
149154
}
150155

151156
const resolvedPublicPath =

test.js

+16
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,22 @@ test('should skip adding sourcemap and gzipped files to compilation if set to fa
138138
expect(addFileToAssetsStub).toHaveBeenCalledWith('my-file.js', compilation);
139139
});
140140

141+
test('should use the hash from HtmlWebpackPlugin if option is set', async () => {
142+
const compilation = {
143+
hash: 'testHash',
144+
options: { output: {} },
145+
assets: {
146+
'my-file.js': { source: () => 'some source code is cool to have;' },
147+
},
148+
};
149+
const pluginData = Object.assign({ assets: { js: [], css: [] } }, pluginMock);
150+
const plugin = new AddAssetHtmlPlugin({ filepath: 'my-file.js', hash: true });
151+
152+
await plugin.addAllAssetsToCompilation(compilation, pluginData);
153+
154+
expect(pluginData.assets).toMatchSnapshot();
155+
});
156+
141157
test('should include hash of file content if option is set', async () => {
142158
const compilation = {
143159
options: { output: {} },

0 commit comments

Comments
 (0)