Skip to content

Commit

Permalink
fix: remove the hash calculation if hash exists (#159)
Browse files Browse the repository at this point in the history
Co-authored-by: Ivan Vasilov <ivan@hfour.com>
  • Loading branch information
SimenB and Ivan Vasilov committed Mar 8, 2022
1 parent e84e7d5 commit 8387886
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
9 changes: 9 additions & 0 deletions __snapshots__/test.js.snap
Expand Up @@ -100,6 +100,15 @@ Object {
}
`;

exports[`should use the hash from HtmlWebpackPlugin if option is set 1`] = `
Object {
"css": Array [],
"js": Array [
"my-file.js?testHash",
],
}
`;

exports[`should used passed in publicPath 1`] = `
Object {
"css": Array [],
Expand Down
11 changes: 8 additions & 3 deletions src/index.js
Expand Up @@ -143,9 +143,14 @@ export default class AddAssetHtmlPlugin {

let suffix = '';
if (hash) {
const md5 = crypto.createHash('md5');
md5.update(compilation.assets[addedFilename].source());
suffix = `?${md5.digest('hex').substr(0, 20)}`;
// if the hash is set by html-webpack-plugin use that hash, else generate a new one
if (compilation.hash) {
suffix = `?${compilation.hash}`;
} else {
const md5 = crypto.createHash('md5');
md5.update(compilation.assets[addedFilename].source());
suffix = `?${md5.digest('hex').substr(0, 20)}`;
}
}

const resolvedPublicPath =
Expand Down
16 changes: 16 additions & 0 deletions test.js
Expand Up @@ -138,6 +138,22 @@ test('should skip adding sourcemap and gzipped files to compilation if set to fa
expect(addFileToAssetsStub).toHaveBeenCalledWith('my-file.js', compilation);
});

test('should use the hash from HtmlWebpackPlugin if option is set', async () => {
const compilation = {
hash: 'testHash',
options: { output: {} },
assets: {
'my-file.js': { source: () => 'some source code is cool to have;' },
},
};
const pluginData = Object.assign({ assets: { js: [], css: [] } }, pluginMock);
const plugin = new AddAssetHtmlPlugin({ filepath: 'my-file.js', hash: true });

await plugin.addAllAssetsToCompilation(compilation, pluginData);

expect(pluginData.assets).toMatchSnapshot();
});

test('should include hash of file content if option is set', async () => {
const compilation = {
options: { output: {} },
Expand Down

0 comments on commit 8387886

Please sign in to comment.