Skip to content

Commit e6c9e47

Browse files
wadahiroSimenB
authored andcommitted
Add hash option (#8)
1 parent 1eed44c commit e6c9e47

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ Type: `string`, mandatory
7373

7474
The absolute path of the file you want to add to the compilation, and resulting HTML file.
7575

76+
#### `hash`
77+
Type: `boolean`, default: `false`
78+
79+
If `true`, will append a unique hash of the file to the filename. This is useful for cache busting.
80+
7681
#### `includeSourcemap`
7782
Type: `boolean`, default: `false`
7883

addAssetHtmlPlugin.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'path';
2+
import crypto from 'crypto';
23
import Promise from 'bluebird';
34

45
// Copied from html-webpack-plugin
@@ -13,11 +14,19 @@ function resolvePublicPath(compilation, filename) {
1314
return publicPath;
1415
}
1516

16-
function addFileToAssets(htmlPluginData, compilation, { filename, typeOfAsset = 'js', includeSourcemap = true } = {}) {
17+
function addFileToAssets(htmlPluginData, compilation, { filename, typeOfAsset = 'js', includeSourcemap = true, hash = false } = {}) {
1718
if (!filename) return compilation.errors.push(new Error('No filename defined'));
1819

1920
return htmlPluginData.plugin.addFileToAssets(filename, compilation)
20-
.then(addedFilename => htmlPluginData.assets[typeOfAsset].unshift(`${resolvePublicPath(compilation, addedFilename)}${addedFilename}`))
21+
.then(addedFilename => {
22+
let suffix = '';
23+
if (hash) {
24+
const md5 = crypto.createHash('md5');
25+
md5.update(compilation.assets[addedFilename].source());
26+
suffix = `?${md5.digest('hex').substr(0, 20)}`;
27+
}
28+
return htmlPluginData.assets[typeOfAsset].unshift(`${resolvePublicPath(compilation, addedFilename)}${addedFilename}${suffix}`);
29+
})
2130
.then(() => {
2231
if (includeSourcemap) {
2332
return htmlPluginData.plugin.addFileToAssets(`${filename}.map`, compilation);

0 commit comments

Comments
 (0)