@@ -69,6 +69,7 @@ function hotLoader(content, context) {
69
69
* @param {string } request
70
70
*/
71
71
function pitch ( request ) {
72
+ // 如果使用了 webpack 5 的 experiments.css 特性,并且当前模块类型为 css,则发出警告
72
73
if (
73
74
this . _compiler &&
74
75
this . _compiler . options &&
@@ -94,6 +95,7 @@ function pitch(request) {
94
95
MiniCssExtractPlugin . pluginSymbol
95
96
] ;
96
97
98
+ // 如果没有添加 mini-css-extract-plugin 插件,则抛出错误 (使用speed-measure-webpack-plugin会报错)
97
99
if ( ! optionsFromPlugin ) {
98
100
callback (
99
101
new Error (
@@ -122,6 +124,7 @@ function pitch(request) {
122
124
typeof options . esModule !== "undefined" ? options . esModule : true ;
123
125
124
126
/**
127
+ * 将依赖添加到 this._module的依赖中
125
128
* @param {Dependency[] | [null, object][] } dependencies
126
129
*/
127
130
const addDependencies = ( dependencies ) => {
@@ -136,6 +139,7 @@ function pitch(request) {
136
139
const identifierCountMap = new Map ( ) ;
137
140
let lastDep ;
138
141
142
+ // 遍历模块依赖,将依赖添加到当前 this._module 的依赖中
139
143
for ( const dependency of dependencies ) {
140
144
if ( ! ( /** @type {Dependency } */ ( dependency ) . identifier ) || ! emit ) {
141
145
// eslint-disable-next-line no-continue
@@ -146,6 +150,7 @@ function pitch(request) {
146
150
identifierCountMap . get (
147
151
/** @type {Dependency } */ ( dependency ) . identifier
148
152
) || 0 ;
153
+ // 获取CssDependency类
149
154
const CssDependency = MiniCssExtractPlugin . getCssDependency ( webpack ) ;
150
155
151
156
/** @type {NormalModule } */
@@ -199,6 +204,7 @@ function pitch(request) {
199
204
/** @type {Dependency[] | [null, object][] } */
200
205
let dependencies ;
201
206
207
+ // 将exports转为dependencies依赖
202
208
if ( ! Array . isArray ( exports ) ) {
203
209
dependencies = [ [ null , exports ] ] ;
204
210
} else {
@@ -242,6 +248,7 @@ function pitch(request) {
242
248
return ;
243
249
}
244
250
251
+ // 根据导出类型,生成最终导出的代码
245
252
const result = locals
246
253
? namedExport
247
254
? Object . keys ( locals )
@@ -261,15 +268,18 @@ function pitch(request) {
261
268
262
269
let resultSource = `// extracted by ${ MiniCssExtractPlugin . pluginName } ` ;
263
270
271
+ // 只有在支持热更新并且 emit 为 true 时,才会添加热更新代码
264
272
// only attempt hotreloading if the css is actually used for something other than hash values
265
273
resultSource +=
266
274
this . hot && emit
267
275
? hotLoader ( result , { loaderContext : this , options, locals } )
268
276
: result ;
269
277
278
+ // 将处理后的结果传递给下一个 Loader
270
279
callback ( null , resultSource ) ;
271
280
} ;
272
281
282
+ // 设置publicPath
273
283
let { publicPath } =
274
284
/** @type {Compilation } */
275
285
( this . _compilation ) . outputOptions ;
@@ -285,6 +295,7 @@ function pitch(request) {
285
295
publicPath = AUTO_PUBLIC_PATH ;
286
296
}
287
297
298
+ // 使用实验特性 experimentalUseImportModule 或者支持 this.importModule 函数时,通过this.importModule处理 CSS
288
299
if (
289
300
( typeof optionsFromPlugin . experimentalUseImportModule === "undefined" &&
290
301
typeof this . importModule === "function" ) ||
@@ -316,6 +327,7 @@ function pitch(request) {
316
327
publicPathForExtract = publicPath ;
317
328
}
318
329
330
+ // 处理 CSS
319
331
this . importModule (
320
332
`${ this . resourcePath } .webpack[javascript/auto]!=!!!${ request } ` ,
321
333
{
@@ -333,15 +345,18 @@ function pitch(request) {
333
345
334
346
return ;
335
347
}
336
-
348
+ // 导出处理结果
337
349
handleExports ( exports ) ;
338
350
}
339
351
) ;
340
352
return ;
341
353
}
354
+ // 不使用实验特性时,使用子compile来处理css
342
355
356
+ // 删除当前mini-css-extract-plugin loader
343
357
const loaders = this . loaders . slice ( this . loaderIndex + 1 ) ;
344
358
359
+ // 将当前 CSS 文件添加为依赖
345
360
this . addDependency ( this . resourcePath ) ;
346
361
347
362
const childFilename = "*" ;
@@ -351,6 +366,7 @@ function pitch(request) {
351
366
publicPath,
352
367
} ;
353
368
369
+ // 创建子compile来处理css
354
370
const childCompiler =
355
371
/** @type {Compilation } */
356
372
( this . _compilation ) . createChildCompiler (
@@ -384,6 +400,7 @@ function pitch(request) {
384
400
385
401
new EnableLibraryPlugin ( "commonjs2" ) . apply ( childCompiler ) ;
386
402
403
+ // 设置入口
387
404
EntryOptionPlugin . applyEntryOption ( childCompiler , this . context , {
388
405
child : {
389
406
library : {
@@ -393,7 +410,7 @@ function pitch(request) {
393
410
} ,
394
411
} ) ;
395
412
const { LimitChunkCountPlugin } = webpack . optimize ;
396
-
413
+ // 限制只输出一个 chunk
397
414
new LimitChunkCountPlugin ( { maxChunks : 1 } ) . apply ( childCompiler ) ;
398
415
399
416
const { NormalModule } = webpack ;
@@ -410,8 +427,11 @@ function pitch(request) {
410
427
normalModuleHook . tap (
411
428
`${ MiniCssExtractPlugin . pluginName } loader` ,
412
429
( loaderContext , module ) => {
430
+ // 下次loader转化时,如果遇到当前css模块
413
431
if ( module . request === request ) {
414
432
// eslint-disable-next-line no-param-reassign
433
+
434
+ // 更新为删除mini-css-extract-plugin loader 后的loader列表,避免当前Loader再次处理
415
435
module . loaders = loaders . map ( ( loader ) => {
416
436
return {
417
437
type : null ,
@@ -438,11 +458,13 @@ function pitch(request) {
438
458
compilation . hooks . processAssets . tap (
439
459
MiniCssExtractPlugin . pluginName ,
440
460
( ) => {
461
+ // 保存source
441
462
source =
442
463
compilation . assets [ childFilename ] &&
443
464
compilation . assets [ childFilename ] . source ( ) ;
444
465
445
466
// Remove all chunk assets
467
+ // 为了避免将样式内联到 JavaScript 文件中,清除assets
446
468
compilation . chunks . forEach ( ( chunk ) => {
447
469
chunk . files . forEach ( ( file ) => {
448
470
compilation . deleteAsset ( file ) ;
@@ -453,6 +475,7 @@ function pitch(request) {
453
475
}
454
476
) ;
455
477
478
+ // 运行子compiler
456
479
childCompiler . runAsChild ( ( error , entries , compilation ) => {
457
480
if ( error ) {
458
481
callback ( error ) ;
@@ -494,13 +517,15 @@ function pitch(request) {
494
517
495
518
let originalExports ;
496
519
try {
520
+ // 在loader上下文,执行刚刚导出的 source代码,导出webpack的立即执行函数
497
521
originalExports = evalModuleCode ( this , source , request ) ;
498
522
} catch ( e ) {
499
523
callback ( /** @type {Error } */ ( e ) ) ;
500
524
501
525
return ;
502
526
}
503
527
528
+ // 将导出的立即执行函数,利用module.addDependicies设为当前模块的依赖
504
529
handleExports ( originalExports , compilation , assets , assetsInfo ) ;
505
530
} ) ;
506
531
}
0 commit comments