@@ -4,7 +4,7 @@ import webpack from 'webpack';
4
4
import sources from 'webpack-sources' ;
5
5
6
6
const { ConcatSource, SourceMapSource, OriginalSource } = sources ;
7
- const { Template } = webpack ;
7
+ const { Template, util : { createHash } } = webpack ;
8
8
9
9
const NS = path . dirname ( fs . realpathSync ( __filename ) ) ;
10
10
@@ -57,7 +57,7 @@ class CssModule extends webpack.Module {
57
57
nameForCondition ( ) {
58
58
const resource = this . _identifier . split ( '!' ) . pop ( ) ;
59
59
const idx = resource . indexOf ( '?' ) ;
60
- if ( idx >= 0 ) return resource . substr ( 0 , idx ) ;
60
+ if ( idx >= 0 ) return resource . substring ( 0 , idx ) ;
61
61
return resource ;
62
62
}
63
63
@@ -66,6 +66,13 @@ class CssModule extends webpack.Module {
66
66
this . buildMeta = { } ;
67
67
callback ( ) ;
68
68
}
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
+ }
69
76
}
70
77
71
78
class CssModuleFactory {
@@ -121,6 +128,7 @@ class MiniCssExtractPlugin {
121
128
filenameTemplate : this . options . filename ,
122
129
pathOptions : {
123
130
chunk,
131
+ contentHashType : NS ,
124
132
} ,
125
133
identifier : `mini-css-extract-plugin.${ chunk . id } ` ,
126
134
} ) ;
@@ -134,11 +142,26 @@ class MiniCssExtractPlugin {
134
142
filenameTemplate : this . options . chunkFilename ,
135
143
pathOptions : {
136
144
chunk,
145
+ contentHashType : NS ,
137
146
} ,
138
147
identifier : `mini-css-extract-plugin.${ chunk . id } ` ,
139
148
} ) ;
140
149
}
141
150
} ) ;
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
+ . substring ( 0 , hashDigestLength ) ;
164
+ } ) ;
142
165
const { mainTemplate } = compilation ;
143
166
mainTemplate . hooks . localVars . tap (
144
167
pluginName ,
@@ -178,13 +201,35 @@ class MiniCssExtractPlugin {
178
201
const shortChunkHashMap = Object . create ( null ) ;
179
202
for ( const chunkId of Object . keys ( chunkMaps . hash ) ) {
180
203
if ( typeof chunkMaps . hash [ chunkId ] === 'string' ) {
181
- shortChunkHashMap [ chunkId ] = chunkMaps . hash [ chunkId ] . substr ( 0 , length ) ;
204
+ shortChunkHashMap [ chunkId ] = chunkMaps . hash [ chunkId ] . substring ( 0 , length ) ;
182
205
}
183
206
}
184
207
return `" + ${ JSON . stringify ( shortChunkHashMap ) } [chunkId] + "` ;
185
208
} ,
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
+ ] . substring ( 0 , length ) ;
223
+ }
224
+ }
225
+ return `" + ${ JSON . stringify (
226
+ shortContentHashMap ,
227
+ ) } [chunkId] + "`;
228
+ } ,
229
+ } ,
186
230
name : `" + (${ JSON . stringify ( chunkMaps . name ) } [chunkId]||chunkId) + "` ,
187
231
} ,
232
+ contentHashType : NS ,
188
233
} ,
189
234
) ;
190
235
return Template . asString ( [
0 commit comments