@@ -99,6 +99,16 @@ class HtmlWebpackPlugin {
99
99
this . options . filename = path . relative ( compiler . options . output . path , filename ) ;
100
100
}
101
101
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 + ) : ) ? c o n t e n t h a s h (?: : ( [ 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
+
102
112
// Check if webpack is running in production mode
103
113
// @see https://github.com/webpack/webpack/blob/3366421f1784c449f415cda5930a8e445086f688/lib/WebpackOptionsDefaulter.js#L12-L14
104
114
const isProductionLikeMode = compiler . options . mode === 'production' || ! compiler . options . mode ;
@@ -279,10 +289,11 @@ class HtmlWebpackPlugin {
279
289
return self . options . showErrors ? prettyError ( err , compiler . context ) . toHtml ( ) : 'ERROR' ;
280
290
} )
281
291
. then ( html => {
292
+ const REGEXP_TEMPLATEHASH = / \[ (?: ( \w + ) : ) ? t e m p l a t e h a s h (?: : ( [ a - z ] + \d * ) ) ? (?: : ( \d + ) ) ? \] / ig;
282
293
// Allow to use [templatehash] as placeholder for the html-webpack-plugin name
283
294
// See also https://survivejs.com/webpack/optimizing/adding-hashes-to-filenames/
284
295
// From https://github.com/webpack-contrib/extract-text-webpack-plugin/blob/8de6558e33487e7606e7cd7cb2adc2cccafef272/src/index.js#L212-L214
285
- const finalOutputName = childCompilationOutputName . replace ( / \[ (?: ( \w + ) : ) ? t e m p l a t e h a s h (?: : ( [ a - z ] + \d * ) ) ? (?: : ( \d + ) ) ? \] / ig , ( _ , hashType , digestType , maxLength ) => {
296
+ const finalOutputName = childCompilationOutputName . replace ( REGEXP_TEMPLATEHASH , ( _ , hashType , digestType , maxLength ) => {
286
297
return loaderUtils . getHashDigest ( Buffer . from ( html , 'utf8' ) , hashType , digestType , parseInt ( maxLength , 10 ) ) ;
287
298
} ) ;
288
299
// Add the evaluated html code to the webpack assets
0 commit comments