Skip to content

Commit 049d4d3

Browse files
sibiraj-sjantimon
authored andcommittedOct 3, 2018
fix: allow contenthash along with templatehash
Fixes: #1033
1 parent 119e817 commit 049d4d3

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed
 

‎index.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ class HtmlWebpackPlugin {
9999
this.options.filename = path.relative(compiler.options.output.path, filename);
100100
}
101101

102+
// `contenthash` is introduced in webpack v4.3
103+
// which conflicts with the plugin's existing `contenthash` method, hence
104+
// hence is renmaed to `templatehash` to avoid such conflicts
105+
const REGEXP_CONTENTHASH = /\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig;
106+
if (REGEXP_CONTENTHASH.test(this.options.filename)) {
107+
this.options.filename = this.options.filename.replace(REGEXP_CONTENTHASH, (match) => {
108+
return match.replace('contenthash', 'templatehash');
109+
});
110+
}
111+
102112
// Check if webpack is running in production mode
103113
// @see https://github.com/webpack/webpack/blob/3366421f1784c449f415cda5930a8e445086f688/lib/WebpackOptionsDefaulter.js#L12-L14
104114
const isProductionLikeMode = compiler.options.mode === 'production' || !compiler.options.mode;
@@ -279,10 +289,11 @@ class HtmlWebpackPlugin {
279289
return self.options.showErrors ? prettyError(err, compiler.context).toHtml() : 'ERROR';
280290
})
281291
.then(html => {
292+
const REGEXP_TEMPLATEHASH = /\[(?:(\w+):)?templatehash(?::([a-z]+\d*))?(?::(\d+))?\]/ig;
282293
// Allow to use [templatehash] as placeholder for the html-webpack-plugin name
283294
// See also https://survivejs.com/webpack/optimizing/adding-hashes-to-filenames/
284295
// From https://github.com/webpack-contrib/extract-text-webpack-plugin/blob/8de6558e33487e7606e7cd7cb2adc2cccafef272/src/index.js#L212-L214
285-
const finalOutputName = childCompilationOutputName.replace(/\[(?:(\w+):)?templatehash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, (_, hashType, digestType, maxLength) => {
296+
const finalOutputName = childCompilationOutputName.replace(REGEXP_TEMPLATEHASH, (_, hashType, digestType, maxLength) => {
286297
return loaderUtils.getHashDigest(Buffer.from(html, 'utf8'), hashType, digestType, parseInt(maxLength, 10));
287298
});
288299
// Add the evaluated html code to the webpack assets

‎spec/basic.spec.js

+32
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,38 @@ describe('HtmlWebpackPlugin', () => {
712712
}, ['<script src="index_bundle.js"'], /test-\S+\.html$/, done);
713713
});
714714

715+
it('should allow filename in the format of [<hashType>:contenthash:<digestType>:<length>]', done => {
716+
testHtmlPlugin({
717+
mode: 'production',
718+
entry: {
719+
index: path.join(__dirname, 'fixtures/index.js')
720+
},
721+
output: {
722+
path: OUTPUT_DIR,
723+
filename: '[name]_bundle.js'
724+
},
725+
plugins: [
726+
new HtmlWebpackPlugin({filename: 'index.[sha256:contenthash:base32:32].html'})
727+
]
728+
}, [], /index\.[a-z0-9]{32}\.html/, done);
729+
});
730+
731+
it('will replace [contenthash] in the filename with a content hash of 32 hex characters', done => {
732+
testHtmlPlugin({
733+
mode: 'production',
734+
entry: {
735+
index: path.join(__dirname, 'fixtures/index.js')
736+
},
737+
output: {
738+
path: OUTPUT_DIR,
739+
filename: '[name]_bundle.js'
740+
},
741+
plugins: [
742+
new HtmlWebpackPlugin({filename: 'index.[contenthash].html'})
743+
]
744+
}, [], /index\.[a-f0-9]{32}\.html/, done);
745+
});
746+
715747
it('will replace [templatehash] in the filename with a content hash of 32 hex characters', done => {
716748
testHtmlPlugin({
717749
mode: 'production',

0 commit comments

Comments
 (0)
Please sign in to comment.