Skip to content

Commit ac01513

Browse files
author
sunfutao
committed
docs: 增加注释
1 parent 8b52fee commit ac01513

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

note/data_source.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// mini-css-extract-plugin 处理assets(compilation.hooks.processAssets)生成的source
2+
// type: string
3+
4+
'/*
5+
* ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
6+
* This devtool is neither made for production nor for readable output files.
7+
* It uses "eval()" calls to create a separate source file in the browser devtools.
8+
* If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
9+
* or disable the default devtool with "devtool: false".
10+
* If you are looking for production-ready output files, see mode:…esModule', { value: true });
11+
/******/ };
12+
/******/ })();
13+
/******/
14+
/************************************************************************/
15+
/******/
16+
/******/
17+
// startup/******/
18+
// Load entry module and return exports/******/
19+
// This entry module can't be inlined because the eval devtool is used.
20+
/******/ var __webpack_exports__ = __webpack_require__("./node_modules/css-loader/dist/cjs.js!./test/style.css");
21+
/******/ module.exports = __webpack_exports__;
22+
/******/ /******/ })();'
23+

src/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,9 @@ class MiniCssExtractPlugin {
391391
/**
392392
* Prevent creation of multiple CssDependency classes to allow other integrations to get the current CssDependency.
393393
*/
394+
// 判断有没有将CssDependency这个class存入缓存,这里的webpack来自于compiler.webpack,它是个方法
395+
// 当前插件apply时compiler.webpack会被speed-measure-webpack-plugin 设为proxy
396+
// 但是第二次normal-loader-hook触发时,传入的不是proxy,引发bug
394397
if (cssDependencyCache.has(webpack)) {
395398
return /** @type {CssDependencyConstructor} */ (
396399
cssDependencyCache.get(webpack)
@@ -636,6 +639,7 @@ class MiniCssExtractPlugin {
636639
const { loader: normalModuleHook } =
637640
NormalModule.getCompilationHooks(compilation);
638641

642+
// 添加插件标记,loader转化时会检测
639643
normalModuleHook.tap(
640644
pluginName,
641645
/**
@@ -652,6 +656,7 @@ class MiniCssExtractPlugin {
652656
);
653657
});
654658

659+
// 初始化 compilation 时调用,在触发 compilation 事件之前调用。这个钩子 不会 被复制到子编译器
655660
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
656661
class CssModuleFactory {
657662
/**
@@ -668,6 +673,7 @@ class MiniCssExtractPlugin {
668673
}
669674
}
670675

676+
// webpack中添加CssDependency类型,用于生成CssModule
671677
compilation.dependencyFactories.set(
672678
CssDependency,
673679
new CssModuleFactory()
@@ -678,11 +684,13 @@ class MiniCssExtractPlugin {
678684
apply() {}
679685
}
680686

687+
// 设置CssDependency模版
681688
compilation.dependencyTemplates.set(
682689
CssDependency,
683690
new CssDependencyTemplate()
684691
);
685692

693+
// 这个hook会根据chunk生成对应的manifest,其中manifest.render()可以根据template生成对应的source
686694
compilation.hooks.renderManifest.tap(
687695
pluginName,
688696
/**
@@ -745,6 +753,7 @@ class MiniCssExtractPlugin {
745753
}
746754
);
747755

756+
// 生成css chunk的contentHash
748757
compilation.hooks.contentHash.tap(pluginName, (chunk) => {
749758
const { outputOptions, chunkGraph } = compilation;
750759
const modules = this.sortModules(
@@ -1305,6 +1314,7 @@ class MiniCssExtractPlugin {
13051314
}
13061315

13071316
/**
1317+
* 根据模版生成assets
13081318
* @private
13091319
* @param {Compiler} compiler
13101320
* @param {Compilation} compilation

src/loader.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ function hotLoader(content, context) {
6969
* @param {string} request
7070
*/
7171
function pitch(request) {
72+
// 如果使用了 webpack 5 的 experiments.css 特性,并且当前模块类型为 css,则发出警告
7273
if (
7374
this._compiler &&
7475
this._compiler.options &&
@@ -94,6 +95,7 @@ function pitch(request) {
9495
MiniCssExtractPlugin.pluginSymbol
9596
];
9697

98+
// 如果没有添加 mini-css-extract-plugin 插件,则抛出错误 (使用speed-measure-webpack-plugin会报错)
9799
if (!optionsFromPlugin) {
98100
callback(
99101
new Error(
@@ -122,6 +124,7 @@ function pitch(request) {
122124
typeof options.esModule !== "undefined" ? options.esModule : true;
123125

124126
/**
127+
* 将依赖添加到 this._module的依赖中
125128
* @param {Dependency[] | [null, object][]} dependencies
126129
*/
127130
const addDependencies = (dependencies) => {
@@ -136,6 +139,7 @@ function pitch(request) {
136139
const identifierCountMap = new Map();
137140
let lastDep;
138141

142+
// 遍历模块依赖,将依赖添加到当前 this._module 的依赖中
139143
for (const dependency of dependencies) {
140144
if (!(/** @type {Dependency} */ (dependency).identifier) || !emit) {
141145
// eslint-disable-next-line no-continue
@@ -146,6 +150,7 @@ function pitch(request) {
146150
identifierCountMap.get(
147151
/** @type {Dependency} */ (dependency).identifier
148152
) || 0;
153+
// 获取CssDependency类
149154
const CssDependency = MiniCssExtractPlugin.getCssDependency(webpack);
150155

151156
/** @type {NormalModule} */
@@ -199,6 +204,7 @@ function pitch(request) {
199204
/** @type {Dependency[] | [null, object][]} */
200205
let dependencies;
201206

207+
// 将exports转为dependencies依赖
202208
if (!Array.isArray(exports)) {
203209
dependencies = [[null, exports]];
204210
} else {
@@ -242,6 +248,7 @@ function pitch(request) {
242248
return;
243249
}
244250

251+
// 根据导出类型,生成最终导出的代码
245252
const result = locals
246253
? namedExport
247254
? Object.keys(locals)
@@ -261,15 +268,18 @@ function pitch(request) {
261268

262269
let resultSource = `// extracted by ${MiniCssExtractPlugin.pluginName}`;
263270

271+
// 只有在支持热更新并且 emit 为 true 时,才会添加热更新代码
264272
// only attempt hotreloading if the css is actually used for something other than hash values
265273
resultSource +=
266274
this.hot && emit
267275
? hotLoader(result, { loaderContext: this, options, locals })
268276
: result;
269277

278+
// 将处理后的结果传递给下一个 Loader
270279
callback(null, resultSource);
271280
};
272281

282+
// 设置publicPath
273283
let { publicPath } =
274284
/** @type {Compilation} */
275285
(this._compilation).outputOptions;
@@ -285,6 +295,7 @@ function pitch(request) {
285295
publicPath = AUTO_PUBLIC_PATH;
286296
}
287297

298+
// 使用实验特性 experimentalUseImportModule 或者支持 this.importModule 函数时,通过this.importModule处理 CSS
288299
if (
289300
(typeof optionsFromPlugin.experimentalUseImportModule === "undefined" &&
290301
typeof this.importModule === "function") ||
@@ -316,6 +327,7 @@ function pitch(request) {
316327
publicPathForExtract = publicPath;
317328
}
318329

330+
// 处理 CSS
319331
this.importModule(
320332
`${this.resourcePath}.webpack[javascript/auto]!=!!!${request}`,
321333
{
@@ -333,15 +345,18 @@ function pitch(request) {
333345

334346
return;
335347
}
336-
348+
// 导出处理结果
337349
handleExports(exports);
338350
}
339351
);
340352
return;
341353
}
354+
// 不使用实验特性时,使用子compile来处理css
342355

356+
// 删除当前mini-css-extract-plugin loader
343357
const loaders = this.loaders.slice(this.loaderIndex + 1);
344358

359+
// 将当前 CSS 文件添加为依赖
345360
this.addDependency(this.resourcePath);
346361

347362
const childFilename = "*";
@@ -351,6 +366,7 @@ function pitch(request) {
351366
publicPath,
352367
};
353368

369+
// 创建子compile来处理css
354370
const childCompiler =
355371
/** @type {Compilation} */
356372
(this._compilation).createChildCompiler(
@@ -384,6 +400,7 @@ function pitch(request) {
384400

385401
new EnableLibraryPlugin("commonjs2").apply(childCompiler);
386402

403+
// 设置入口
387404
EntryOptionPlugin.applyEntryOption(childCompiler, this.context, {
388405
child: {
389406
library: {
@@ -393,7 +410,7 @@ function pitch(request) {
393410
},
394411
});
395412
const { LimitChunkCountPlugin } = webpack.optimize;
396-
413+
// 限制只输出一个 chunk
397414
new LimitChunkCountPlugin({ maxChunks: 1 }).apply(childCompiler);
398415

399416
const { NormalModule } = webpack;
@@ -410,8 +427,11 @@ function pitch(request) {
410427
normalModuleHook.tap(
411428
`${MiniCssExtractPlugin.pluginName} loader`,
412429
(loaderContext, module) => {
430+
// 下次loader转化时,如果遇到当前css模块
413431
if (module.request === request) {
414432
// eslint-disable-next-line no-param-reassign
433+
434+
// 更新为删除mini-css-extract-plugin loader 后的loader列表,避免当前Loader再次处理
415435
module.loaders = loaders.map((loader) => {
416436
return {
417437
type: null,
@@ -438,11 +458,13 @@ function pitch(request) {
438458
compilation.hooks.processAssets.tap(
439459
MiniCssExtractPlugin.pluginName,
440460
() => {
461+
// 保存source
441462
source =
442463
compilation.assets[childFilename] &&
443464
compilation.assets[childFilename].source();
444465

445466
// Remove all chunk assets
467+
// 为了避免将样式内联到 JavaScript 文件中,清除assets
446468
compilation.chunks.forEach((chunk) => {
447469
chunk.files.forEach((file) => {
448470
compilation.deleteAsset(file);
@@ -453,6 +475,7 @@ function pitch(request) {
453475
}
454476
);
455477

478+
// 运行子compiler
456479
childCompiler.runAsChild((error, entries, compilation) => {
457480
if (error) {
458481
callback(error);
@@ -494,13 +517,15 @@ function pitch(request) {
494517

495518
let originalExports;
496519
try {
520+
// 在loader上下文,执行刚刚导出的 source代码,导出webpack的立即执行函数
497521
originalExports = evalModuleCode(this, source, request);
498522
} catch (e) {
499523
callback(/** @type {Error} */ (e));
500524

501525
return;
502526
}
503527

528+
// 将导出的立即执行函数,利用module.addDependicies设为当前模块的依赖
504529
handleExports(originalExports, compilation, assets, assetsInfo);
505530
});
506531
}

src/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function findModuleById(compilation, id) {
3535
}
3636

3737
/**
38+
* 新建node.js模块,loaderContext中执行 字符串代码
3839
* @param {LoaderContext} loaderContext
3940
* @param {string | Buffer} code
4041
* @param {string} filename

0 commit comments

Comments
 (0)