@@ -4,7 +4,7 @@ import webpack from 'webpack';
44import sources from 'webpack-sources' ;
55
66const { ConcatSource, SourceMapSource, OriginalSource } = sources ;
7- const { Template } = webpack ;
7+ const { Template, util : { createHash } } = webpack ;
88
99const NS = path . dirname ( fs . realpathSync ( __filename ) ) ;
1010
@@ -66,6 +66,13 @@ class CssModule extends webpack.Module {
6666 this . buildMeta = { } ;
6767 callback ( ) ;
6868 }
69+
70+ updateHash ( hash ) {
71+ super . updateHash ( hash ) ;
72+ hash . update ( this . content ) ;
73+ hash . update ( this . media || '' ) ;
74+ hash . update ( JSON . stringify ( this . sourceMap || '' ) ) ;
75+ }
6976}
7077
7178class CssModuleFactory {
@@ -121,6 +128,7 @@ class MiniCssExtractPlugin {
121128 filenameTemplate : this . options . filename ,
122129 pathOptions : {
123130 chunk,
131+ contentHashType : NS ,
124132 } ,
125133 identifier : `mini-css-extract-plugin.${ chunk . id } ` ,
126134 } ) ;
@@ -134,11 +142,26 @@ class MiniCssExtractPlugin {
134142 filenameTemplate : this . options . chunkFilename ,
135143 pathOptions : {
136144 chunk,
145+ contentHashType : NS ,
137146 } ,
138147 identifier : `mini-css-extract-plugin.${ chunk . id } ` ,
139148 } ) ;
140149 }
141150 } ) ;
151+ compilation . hooks . contentHash . tap ( pluginName , ( chunk ) => {
152+ const { outputOptions } = compilation ;
153+ const { hashFunction, hashDigest, hashDigestLength } = outputOptions ;
154+ const hash = createHash ( hashFunction ) ;
155+ for ( const m of chunk . modulesIterable ) {
156+ if ( m . type === NS ) {
157+ m . updateHash ( hash ) ;
158+ }
159+ }
160+ const { contentHash } = chunk ;
161+ contentHash [ NS ] = hash
162+ . digest ( hashDigest )
163+ . substr ( 0 , hashDigestLength ) ;
164+ } ) ;
142165 const { mainTemplate } = compilation ;
143166 mainTemplate . hooks . localVars . tap (
144167 pluginName ,
@@ -183,8 +206,30 @@ class MiniCssExtractPlugin {
183206 }
184207 return `" + ${ JSON . stringify ( shortChunkHashMap ) } [chunkId] + "` ;
185208 } ,
209+ contentHash : {
210+ [ NS ] : `" + ${ JSON . stringify (
211+ chunkMaps . contentHash [ NS ] ,
212+ ) } [chunkId] + "`,
213+ } ,
214+ contentHashWithLength : {
215+ [ NS ] : ( length ) => {
216+ const shortContentHashMap = { } ;
217+ const contentHash = chunkMaps . contentHash [ NS ] ;
218+ for ( const chunkId of Object . keys ( contentHash ) ) {
219+ if ( typeof contentHash [ chunkId ] === 'string' ) {
220+ shortContentHashMap [ chunkId ] = contentHash [
221+ chunkId
222+ ] . substr ( 0 , length ) ;
223+ }
224+ }
225+ return `" + ${ JSON . stringify (
226+ shortContentHashMap ,
227+ ) } [chunkId] + "`;
228+ } ,
229+ } ,
186230 name : `" + (${ JSON . stringify ( chunkMaps . name ) } [chunkId]||chunkId) + "` ,
187231 } ,
232+ contentHashType : NS ,
188233 } ,
189234 ) ;
190235 return Template . asString ( [
0 commit comments