77const REGEXP_HASH = / \[ h a s h (?: : ( \d + ) ) ? \] / gi,
88 REGEXP_CHUNKHASH = / \[ c h u n k h a s h (?: : ( \d + ) ) ? \] / gi,
99 REGEXP_MODULEHASH = / \[ m o d u l e h a s h (?: : ( \d + ) ) ? \] / gi,
10+ REGEXP_CONTENTHASH = / \[ c o n t e n t h a s h (?: : ( \d + ) ) ? \] / gi,
1011 REGEXP_NAME = / \[ n a m e \] / gi,
1112 REGEXP_ID = / \[ i d \] / gi,
1213 REGEXP_MODULEID = / \[ m o d u l e i d \] / gi,
@@ -18,6 +19,7 @@ const REGEXP_HASH = /\[hash(?::(\d+))?\]/gi,
1819// We use a normal RegExp instead of .test
1920const REGEXP_HASH_FOR_TEST = new RegExp ( REGEXP_HASH . source , "i" ) ,
2021 REGEXP_CHUNKHASH_FOR_TEST = new RegExp ( REGEXP_CHUNKHASH . source , "i" ) ,
22+ REGEXP_CONTENTHASH_FOR_TEST = new RegExp ( REGEXP_CONTENTHASH . source , "i" ) ,
2123 REGEXP_NAME_FOR_TEST = new RegExp ( REGEXP_NAME . source , "i" ) ;
2224
2325const withHashLength = ( replacer , handlerFn ) => {
@@ -55,6 +57,15 @@ const replacePathVariables = (path, data) => {
5557 const chunkName = chunk && ( chunk . name || chunk . id ) ;
5658 const chunkHash = chunk && ( chunk . renderedHash || chunk . hash ) ;
5759 const chunkHashWithLength = chunk && chunk . hashWithLength ;
60+ const contentHashType = data . contentHashType ;
61+ const contentHash =
62+ ( chunk && chunk . contentHash && chunk . contentHash [ contentHashType ] ) ||
63+ data . contentHash ;
64+ const contentHashWithLength =
65+ ( chunk &&
66+ chunk . contentHashWithLength &&
67+ chunk . contentHashWithLength [ contentHashType ] ) ||
68+ data . contentHashWithLength ;
5869 const module = data . module ;
5970 const moduleId = module && module . id ;
6071 const moduleHash = module && ( module . renderedHash || module . hash ) ;
@@ -64,9 +75,15 @@ const replacePathVariables = (path, data) => {
6475 path = path ( data ) ;
6576 }
6677
67- if ( data . noChunkHash && REGEXP_CHUNKHASH_FOR_TEST . test ( path ) ) {
78+ if (
79+ data . noChunkHash &&
80+ ( REGEXP_CHUNKHASH_FOR_TEST . test ( path ) ||
81+ REGEXP_CONTENTHASH_FOR_TEST . test ( path ) )
82+ ) {
6883 throw new Error (
69- `Cannot use [chunkhash] for chunk in '${ path } ' (use [hash] instead)`
84+ `Cannot use [chunkhash] or [contenthash] for chunk in '${
85+ path
86+ } ' (use [hash] instead)`
7087 ) ;
7188 }
7289
@@ -80,6 +97,10 @@ const replacePathVariables = (path, data) => {
8097 REGEXP_CHUNKHASH ,
8198 withHashLength ( getReplacer ( chunkHash ) , chunkHashWithLength )
8299 )
100+ . replace (
101+ REGEXP_CONTENTHASH ,
102+ withHashLength ( getReplacer ( contentHash ) , contentHashWithLength )
103+ )
83104 . replace (
84105 REGEXP_MODULEHASH ,
85106 withHashLength ( getReplacer ( moduleHash ) , moduleHashWithLength )
@@ -115,6 +136,7 @@ class TemplatedPathPlugin {
115136 if (
116137 REGEXP_HASH_FOR_TEST . test ( publicPath ) ||
117138 REGEXP_CHUNKHASH_FOR_TEST . test ( publicPath ) ||
139+ REGEXP_CONTENTHASH_FOR_TEST . test ( publicPath ) ||
118140 REGEXP_NAME_FOR_TEST . test ( publicPath )
119141 )
120142 return true ;
@@ -132,6 +154,13 @@ class TemplatedPathPlugin {
132154 outputOptions . chunkFilename || outputOptions . filename ;
133155 if ( REGEXP_CHUNKHASH_FOR_TEST . test ( chunkFilename ) )
134156 hash . update ( JSON . stringify ( chunk . getChunkMaps ( true ) . hash ) ) ;
157+ if ( REGEXP_CONTENTHASH_FOR_TEST . test ( chunkFilename ) ) {
158+ hash . update (
159+ JSON . stringify (
160+ chunk . getChunkMaps ( true ) . contentHash . javascript || { }
161+ )
162+ ) ;
163+ }
135164 if ( REGEXP_NAME_FOR_TEST . test ( chunkFilename ) )
136165 hash . update ( JSON . stringify ( chunk . getChunkMaps ( true ) . name ) ) ;
137166 }
0 commit comments