.22-浅析webpack源码之事件流compilation总览
呃,终于到了这地方……
newCompilation(params) {
// ...
this.applyPlugins("this-compilation", compilation, params);
//
console.log(this._plugins['compilation'].length);
this.applyPlugins("compilation", compilation, params);
return compilation;
}
MMP,有31个函数,估计可以写到明年了。
这里先梳理所有事件的注入来源,经检测,全部来源于WebpackOptionsApply中,回到那个可怕的模块,梳理后如下:
class WebpackOptionsApply extends OptionsApply {
constructor() {
super();
}
process(options, compiler) {
// ...
if (typeof options.target === "string") {
// ...
switch (options.target) {
case "web":
JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
NodeSourcePlugin = require("./node/NodeSourcePlugin");
compiler.apply(
new JsonpTemplatePlugin(options.output),
// plugin + 3
new FunctionModulePlugin(options.output),
new NodeSourcePlugin(options.node),
new LoaderTargetPlugin(options.target)
);
break;
// ...
}
}
// ...
// plugin + 1
compiler.apply(new EntryOptionPlugin());
compiler.applyPluginsBailResult("entry-option", options.context, options.entry);
// plugin + 24
compiler.apply( /**/ );
compiler.apply( /**/ );
// ...
// plugin + 1
compiler.apply(new TemplatedPathPlugin());
// plugin + 1
compiler.apply(new RecordIdsPlugin());
// plugin + 1
compiler.apply(new WarnCaseSensitiveModulesPlugin());
// ...
return options;
}
}
还好都集中在一个地方,这样又可以写流水账了。
这里先要过一个地方,之前似乎遗留了:
compiler.apply(new EntryOptionPlugin());
compiler.applyPluginsBailResult("entry-option", options.context, options.entry);
这里注入了entry-option事件流,并在下一行代码触发,所以直接进去看实现:
function itemToPlugin(context, item, name) {
if (Array.isArray(item)) {
return new MultiEntryPlugin(context, item, name);
}
return new SingleEntryPlugin(context, item, name);
}
module.exports = class EntryOptionPlugin {
apply(compiler) {
// context => options.context
// entry => options.entry
compiler.plugin("entry-option", (context, entry) => {
// 针对单字符串或数组情况
if (typeof entry === "string" || Array.isArray(entry)) {
// 输出文件为main
compiler.apply(itemToPlugin(context, entry, "main"));
}
// 对象 => 多入口
else if (typeof entry === "object") {
Object.keys(entry).forEach(name => compiler.apply(itemToPlugin(context, entry[name], name)));
}
// 函数
else if (typeof entry === "function") {
compiler.apply(new DynamicEntryPlugin(context, entry));
}
return true;
});
}
};
这里针对字符串、数组、对象、函数四种情况分别做了处理,先只看单字符串,其余情况后面单独讲解。
单字符串会进入SingleEntryPlugin插件:
"use strict";
const SingleEntryDependency = require("./dependencies/SingleEntryDependency");
class SingleEntryPlugin {
constructor(context, entry, name) {
this.context = context;
this.entry = entry;
this.name = name;
};
apply(compiler) {
compiler.plugin("compilation", (compilation, params) => { /**/ });
compiler.plugin("make", (compilation, callback) => { /**/ });
};
static createDependency(entry, name) {
// 该模块有一个isEqualResource方法判断entry是否一样
const dep = new SingleEntryDependency(entry);
dep.loc = name;
return dep;
}
}
这里引入的SingleEntryDependency原型链比较长,而且没有什么营养,出一个示意图,不贴源码了:

可以看到该模块注入了两个事件流,静态方法后面再讲。
第一小节先这样结束吧!
.22-浅析webpack源码之事件流compilation总览的更多相关文章
- .23-浅析webpack源码之事件流compilation(1)
正式开始跑编译,依次解析,首先是: compiler.apply( new JsonpTemplatePlugin(options.output), // start new FunctionModu ...
- .24-浅析webpack源码之事件流compilation(2)
下一个compilation来源于以下代码: compiler.apply(new EntryOptionPlugin()); compiler.applyPluginsBailResult(&quo ...
- .25-浅析webpack源码之事件流compilation(3)
这一节跑下一批plugin. compiler.apply( new EnsureChunkConditionsPlugin(), new RemoveParentModulesPlugin(), n ...
- .21-浅析webpack源码之事件流this-compilation
上一节生成Compilation实例后,添加了一些属性,随后触发this-compilation事件流,如下: Compiler.prototype.newCompilation = (params) ...
- .34-浅析webpack源码之事件流make(3)
新年好呀~过个年光打游戏,function都写不顺溜了. 上一节的代码到这里了: // NormalModuleFactory的resolver事件流 this.plugin("resolv ...
- .27-浅析webpack源码之事件流make(2)
上一节跑到了NormalModuleFactory模块,调用了原型方法create后,依次触发了before-rsolve.factory.resolver事件流,这节从resolver事件流开始讲. ...
- .26-浅析webpack源码之事件流make(1)
compilation事件流中,依然只是针对细节步骤做事件流注入,代码流程如图: // apply => this-compilation // apply => compilation ...
- .37-浅析webpack源码之事件流make(4)
赶紧完结这个系列咯,webpack4都已经出正式版了. 之前的代码搜索到js文件的对应loader,并添加到了对象中返回,流程如下: this.plugin("factory", ...
- 浅析libuv源码-node事件轮询解析(3)
好像博客有观众,那每一篇都画个图吧! 本节简图如下. 上一篇其实啥也没讲,不过node本身就是这么复杂,走流程就要走全套.就像曾经看webpack源码,读了300行代码最后就为了取package.js ...
随机推荐
- C#的发展已经15年了 。。。历史发展
C#是微软公司在2000年6月发布的一种新的编程语言,主要由安德斯·海尔斯伯格(Anders Hejlsberg)主持开发,它是第一个面向组件的编程语言,其源码会编译成msil再运行.它借鉴了Delp ...
- 在Windows上运行Linux
在Windows上运行Linux 之前了解过一些适用于linux的Windows子系统,最近又听人提起,于是在自己的Windows 10专业版上安装了一个Ubuntu.运行起来还真方便,以后在wind ...
- Python的locals()函数
Python的locals()函数会以dict类型返回当前位置的全部局部变量. 示例代码: def func(): arg_a, arg_b = 'a', 'b' def func_a(): pass ...
- (笔记):组合and继承之访问限制(二)
上篇简单介绍了public与private的基本使用.private的访问限制相对复杂.针对这种访问属性,我们会想到有没有一种方式可以无视这种属性.答案是:有.我们可以通过friend的方式(可以破解 ...
- Windows下搭建Redis服务器
Redis服务器是当下比较流行的缓存服务器,Redis通常被人拿来和Memcached进行对比.在我看来,应当是各具优势吧,虽然应用场景基本类似,但总会根据项目的不同来进行不通的选用. 我们今天主要讲 ...
- NanUI文档 - 如何实现C#与Javascript的相互通信
NanUI文档目录 NanUI简介 开始使用NanUI 打包并使用内嵌式的HTML/CSS/JS资源 使用网页来设计整个窗口 如何实现C#与Javascript的相互通信 如何处理NanUI中的下载过 ...
- 【分块】BZOJ2821 作诗(Poetize)
2821: 作诗(Poetize) Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 3265 Solved: 951[Submit][Status][ ...
- Java 与C++的各种优势与弱点--学习更新中
时隔两年没有怎么碰Java了,最近开始学习回顾下.在这里记录下学习的点滴,持续更新...希望对c++\java等不同语言有较为清晰的认识,至少不要学完以后,哪一个都不会了... Java 优势: 1. ...
- 【转载】mysql binlog日志自动清理及手动删除
说明:当开启mysql数据库主从时,会产生大量如mysql-bin.00000* log的文件,这会大量耗费您的硬盘空间.mysql-bin.000001mysql-bin.000002mysql-b ...
- 运用OpenMP提速图像处理速度
一.算法测试 // openmptest的测试程序#include "stdafx.h"void Test(int n){ for (int i=0;i<10000;i ...