1
1
import fs from 'fs' ;
2
2
import path from 'path' ;
3
+
3
4
import webpack from 'webpack' ;
4
5
import sources from 'webpack-sources' ;
5
6
@@ -15,7 +16,11 @@ const REGEXP_CONTENTHASH = /\[contenthash(?::(\d+))?\]/i;
15
16
const REGEXP_NAME = / \[ n a m e \] / i;
16
17
17
18
class CssDependency extends webpack . Dependency {
18
- constructor ( { identifier, content, media, sourceMap } , context , identifierIndex ) {
19
+ constructor (
20
+ { identifier, content, media, sourceMap } ,
21
+ context ,
22
+ identifierIndex
23
+ ) {
19
24
super ( ) ;
20
25
this . identifier = identifier ;
21
26
this . identifierIndex = identifierIndex ;
@@ -55,7 +60,9 @@ class CssModule extends webpack.Module {
55
60
}
56
61
57
62
readableIdentifier ( requestShortener ) {
58
- return `css ${ requestShortener . shorten ( this . _identifier ) } ${ this . _identifierIndex ? ` (${ this . _identifierIndex } )` : '' } ` ;
63
+ return `css ${ requestShortener . shorten ( this . _identifier ) } ${
64
+ this . _identifierIndex ? ` (${ this . _identifierIndex } )` : ''
65
+ } `;
59
66
}
60
67
61
68
nameForCondition ( ) {
@@ -97,9 +104,12 @@ class CssModuleFactory {
97
104
98
105
class MiniCssExtractPlugin {
99
106
constructor ( options ) {
100
- this . options = Object . assign ( {
101
- filename : '[name].css' ,
102
- } , options ) ;
107
+ this . options = Object . assign (
108
+ {
109
+ filename : '[name].css' ,
110
+ } ,
111
+ options
112
+ ) ;
103
113
if ( ! this . options . chunkFilename ) {
104
114
const { filename } = this . options ;
105
115
const hasName = filename . includes ( '[name]' ) ;
@@ -110,7 +120,10 @@ class MiniCssExtractPlugin {
110
120
this . options . chunkFilename = filename ;
111
121
} else {
112
122
// Elsewise prefix '[id].' in front of the basename to make it changing
113
- this . options . chunkFilename = filename . replace ( / ( ^ | \/ ) ( [ ^ / ] * (?: \? | $ ) ) / , '$1[id].$2' ) ;
123
+ this . options . chunkFilename = filename . replace (
124
+ / ( ^ | \/ ) ( [ ^ / ] * (?: \? | $ ) ) / ,
125
+ '$1[id].$2'
126
+ ) ;
114
127
}
115
128
}
116
129
}
@@ -122,7 +135,11 @@ class MiniCssExtractPlugin {
122
135
const module = m ;
123
136
loaderContext [ NS ] = ( content ) => {
124
137
if ( ! Array . isArray ( content ) && content != null ) {
125
- throw new Error ( `Exported value was not extracted as an array: ${ JSON . stringify ( content ) } ` ) ;
138
+ throw new Error (
139
+ `Exported value was not extracted as an array: ${ JSON . stringify (
140
+ content
141
+ ) } `
142
+ ) ;
126
143
}
127
144
const identifierCountMap = new Map ( ) ;
128
145
for ( const line of content ) {
@@ -132,38 +149,62 @@ class MiniCssExtractPlugin {
132
149
}
133
150
} ;
134
151
} ) ;
135
- compilation . dependencyFactories . set ( CssDependency , new CssModuleFactory ( ) ) ;
136
- compilation . dependencyTemplates . set ( CssDependency , new CssDependencyTemplate ( ) ) ;
137
- compilation . mainTemplate . hooks . renderManifest . tap ( pluginName , ( result , { chunk } ) => {
138
- const renderedModules = Array . from ( chunk . modulesIterable ) . filter ( module => module . type === NS ) ;
139
- if ( renderedModules . length > 0 ) {
140
- result . push ( {
141
- render : ( ) => this . renderContentAsset ( renderedModules , compilation . runtimeTemplate . requestShortener ) ,
142
- filenameTemplate : this . options . filename ,
143
- pathOptions : {
144
- chunk,
145
- contentHashType : NS ,
146
- } ,
147
- identifier : `mini-css-extract-plugin.${ chunk . id } ` ,
148
- hash : chunk . contentHash [ NS ] ,
149
- } ) ;
152
+ compilation . dependencyFactories . set (
153
+ CssDependency ,
154
+ new CssModuleFactory ( )
155
+ ) ;
156
+ compilation . dependencyTemplates . set (
157
+ CssDependency ,
158
+ new CssDependencyTemplate ( )
159
+ ) ;
160
+ compilation . mainTemplate . hooks . renderManifest . tap (
161
+ pluginName ,
162
+ ( result , { chunk } ) => {
163
+ const renderedModules = Array . from ( chunk . modulesIterable ) . filter (
164
+ ( module ) => module . type === NS
165
+ ) ;
166
+ if ( renderedModules . length > 0 ) {
167
+ result . push ( {
168
+ render : ( ) =>
169
+ this . renderContentAsset (
170
+ renderedModules ,
171
+ compilation . runtimeTemplate . requestShortener
172
+ ) ,
173
+ filenameTemplate : this . options . filename ,
174
+ pathOptions : {
175
+ chunk,
176
+ contentHashType : NS ,
177
+ } ,
178
+ identifier : `mini-css-extract-plugin.${ chunk . id } ` ,
179
+ hash : chunk . contentHash [ NS ] ,
180
+ } ) ;
181
+ }
150
182
}
151
- } ) ;
152
- compilation . chunkTemplate . hooks . renderManifest . tap ( pluginName , ( result , { chunk } ) => {
153
- const renderedModules = Array . from ( chunk . modulesIterable ) . filter ( module => module . type === NS ) ;
154
- if ( renderedModules . length > 0 ) {
155
- result . push ( {
156
- render : ( ) => this . renderContentAsset ( renderedModules , compilation . runtimeTemplate . requestShortener ) ,
157
- filenameTemplate : this . options . chunkFilename ,
158
- pathOptions : {
159
- chunk,
160
- contentHashType : NS ,
161
- } ,
162
- identifier : `mini-css-extract-plugin.${ chunk . id } ` ,
163
- hash : chunk . contentHash [ NS ] ,
164
- } ) ;
183
+ ) ;
184
+ compilation . chunkTemplate . hooks . renderManifest . tap (
185
+ pluginName ,
186
+ ( result , { chunk } ) => {
187
+ const renderedModules = Array . from ( chunk . modulesIterable ) . filter (
188
+ ( module ) => module . type === NS
189
+ ) ;
190
+ if ( renderedModules . length > 0 ) {
191
+ result . push ( {
192
+ render : ( ) =>
193
+ this . renderContentAsset (
194
+ renderedModules ,
195
+ compilation . runtimeTemplate . requestShortener
196
+ ) ,
197
+ filenameTemplate : this . options . chunkFilename ,
198
+ pathOptions : {
199
+ chunk,
200
+ contentHashType : NS ,
201
+ } ,
202
+ identifier : `mini-css-extract-plugin.${ chunk . id } ` ,
203
+ hash : chunk . contentHash [ NS ] ,
204
+ } ) ;
205
+ }
165
206
}
166
- } ) ;
207
+ ) ;
167
208
compilation . mainTemplate . hooks . hashForChunk . tap (
168
209
pluginName ,
169
210
( hash , chunk ) => {
@@ -173,15 +214,13 @@ class MiniCssExtractPlugin {
173
214
}
174
215
if ( REGEXP_CONTENTHASH . test ( chunkFilename ) ) {
175
216
hash . update (
176
- JSON . stringify (
177
- chunk . getChunkMaps ( true ) . contentHash [ NS ] || { } ,
178
- ) ,
217
+ JSON . stringify ( chunk . getChunkMaps ( true ) . contentHash [ NS ] || { } )
179
218
) ;
180
219
}
181
220
if ( REGEXP_NAME . test ( chunkFilename ) ) {
182
221
hash . update ( JSON . stringify ( chunk . getChunkMaps ( true ) . name ) ) ;
183
222
}
184
- } ,
223
+ }
185
224
) ;
186
225
compilation . hooks . contentHash . tap ( pluginName , ( chunk ) => {
187
226
const { outputOptions } = compilation ;
@@ -198,25 +237,22 @@ class MiniCssExtractPlugin {
198
237
. substring ( 0 , hashDigestLength ) ;
199
238
} ) ;
200
239
const { mainTemplate } = compilation ;
201
- mainTemplate . hooks . localVars . tap (
202
- pluginName ,
203
- ( source , chunk ) => {
204
- const chunkMap = this . getCssChunkObject ( chunk ) ;
205
- if ( Object . keys ( chunkMap ) . length > 0 ) {
206
- return Template . asString ( [
207
- source ,
208
- '' ,
209
- '// object to store loaded CSS chunks' ,
210
- 'var installedCssChunks = {' ,
211
- Template . indent (
212
- chunk . ids . map ( id => `${ JSON . stringify ( id ) } : 0` ) . join ( ',\n' ) ,
213
- ) ,
214
- '}' ,
215
- ] ) ;
216
- }
217
- return source ;
218
- } ,
219
- ) ;
240
+ mainTemplate . hooks . localVars . tap ( pluginName , ( source , chunk ) => {
241
+ const chunkMap = this . getCssChunkObject ( chunk ) ;
242
+ if ( Object . keys ( chunkMap ) . length > 0 ) {
243
+ return Template . asString ( [
244
+ source ,
245
+ '' ,
246
+ '// object to store loaded CSS chunks' ,
247
+ 'var installedCssChunks = {' ,
248
+ Template . indent (
249
+ chunk . ids . map ( ( id ) => `${ JSON . stringify ( id ) } : 0` ) . join ( ',\n' )
250
+ ) ,
251
+ '}' ,
252
+ ] ) ;
253
+ }
254
+ return source ;
255
+ } ) ;
220
256
mainTemplate . hooks . requireEnsure . tap (
221
257
pluginName ,
222
258
( source , chunk , hash ) => {
@@ -227,7 +263,7 @@ class MiniCssExtractPlugin {
227
263
JSON . stringify ( this . options . chunkFilename ) ,
228
264
{
229
265
hash : `" + ${ mainTemplate . renderCurrentHashCode ( hash ) } + "` ,
230
- hashWithLength : length =>
266
+ hashWithLength : ( length ) =>
231
267
`" + ${ mainTemplate . renderCurrentHashCode ( hash , length ) } + "` ,
232
268
chunk : {
233
269
id : '" + chunkId + "' ,
@@ -236,14 +272,18 @@ class MiniCssExtractPlugin {
236
272
const shortChunkHashMap = Object . create ( null ) ;
237
273
for ( const chunkId of Object . keys ( chunkMaps . hash ) ) {
238
274
if ( typeof chunkMaps . hash [ chunkId ] === 'string' ) {
239
- shortChunkHashMap [ chunkId ] = chunkMaps . hash [ chunkId ] . substring ( 0 , length ) ;
275
+ shortChunkHashMap [ chunkId ] = chunkMaps . hash [
276
+ chunkId
277
+ ] . substring ( 0 , length ) ;
240
278
}
241
279
}
242
- return `" + ${ JSON . stringify ( shortChunkHashMap ) } [chunkId] + "` ;
280
+ return `" + ${ JSON . stringify (
281
+ shortChunkHashMap
282
+ ) } [chunkId] + "`;
243
283
} ,
244
284
contentHash : {
245
285
[ NS ] : `" + ${ JSON . stringify (
246
- chunkMaps . contentHash [ NS ] ,
286
+ chunkMaps . contentHash [ NS ]
247
287
) } [chunkId] + "`,
248
288
} ,
249
289
contentHashWithLength : {
@@ -258,14 +298,16 @@ class MiniCssExtractPlugin {
258
298
}
259
299
}
260
300
return `" + ${ JSON . stringify (
261
- shortContentHashMap ,
301
+ shortContentHashMap
262
302
) } [chunkId] + "`;
263
303
} ,
264
304
} ,
265
- name : `" + (${ JSON . stringify ( chunkMaps . name ) } [chunkId]||chunkId) + "` ,
305
+ name : `" + (${ JSON . stringify (
306
+ chunkMaps . name
307
+ ) } [chunkId]||chunkId) + "`,
266
308
} ,
267
309
contentHashType : NS ,
268
- } ,
310
+ }
269
311
) ;
270
312
return Template . asString ( [
271
313
source ,
@@ -277,7 +319,6 @@ class MiniCssExtractPlugin {
277
319
Template . indent ( [
278
320
'promises.push(installedCssChunks[chunkId] = new Promise(function(resolve, reject) {' ,
279
321
Template . indent ( [
280
-
281
322
`var href = ${ linkHrefPath } ;` ,
282
323
`var fullhref = ${ mainTemplate . requireFn } .p + href;` ,
283
324
'var existingLinkTags = document.getElementsByTagName("link");' ,
@@ -313,16 +354,15 @@ class MiniCssExtractPlugin {
313
354
'head.appendChild(linkTag);' ,
314
355
] ) ,
315
356
'}).then(function() {' ,
316
- Template . indent ( [
317
- 'installedCssChunks[chunkId] = 0;' ,
318
- ] ) ,
357
+ Template . indent ( [ 'installedCssChunks[chunkId] = 0;' ] ) ,
319
358
'}));' ,
320
359
] ) ,
321
360
'}' ,
322
361
] ) ;
323
362
}
324
363
return source ;
325
- } ) ;
364
+ }
365
+ ) ;
326
366
} ) ;
327
367
}
328
368
@@ -361,9 +401,20 @@ class MiniCssExtractPlugin {
361
401
source . add ( `@media ${ m . media } {\n` ) ;
362
402
}
363
403
if ( m . sourceMap ) {
364
- source . add ( new SourceMapSource ( m . content , m . readableIdentifier ( requestShortener ) , m . sourceMap ) ) ;
404
+ source . add (
405
+ new SourceMapSource (
406
+ m . content ,
407
+ m . readableIdentifier ( requestShortener ) ,
408
+ m . sourceMap
409
+ )
410
+ ) ;
365
411
} else {
366
- source . add ( new OriginalSource ( m . content , m . readableIdentifier ( requestShortener ) ) ) ;
412
+ source . add (
413
+ new OriginalSource (
414
+ m . content ,
415
+ m . readableIdentifier ( requestShortener )
416
+ )
417
+ ) ;
367
418
}
368
419
source . add ( '\n' ) ;
369
420
if ( m . media ) {
0 commit comments