呃,终于到了这地方……

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总览的更多相关文章

  1. .23-浅析webpack源码之事件流compilation(1)

    正式开始跑编译,依次解析,首先是: compiler.apply( new JsonpTemplatePlugin(options.output), // start new FunctionModu ...

  2. .24-浅析webpack源码之事件流compilation(2)

    下一个compilation来源于以下代码: compiler.apply(new EntryOptionPlugin()); compiler.applyPluginsBailResult(&quo ...

  3. .25-浅析webpack源码之事件流compilation(3)

    这一节跑下一批plugin. compiler.apply( new EnsureChunkConditionsPlugin(), new RemoveParentModulesPlugin(), n ...

  4. .21-浅析webpack源码之事件流this-compilation

    上一节生成Compilation实例后,添加了一些属性,随后触发this-compilation事件流,如下: Compiler.prototype.newCompilation = (params) ...

  5. .34-浅析webpack源码之事件流make(3)

    新年好呀~过个年光打游戏,function都写不顺溜了. 上一节的代码到这里了: // NormalModuleFactory的resolver事件流 this.plugin("resolv ...

  6. .27-浅析webpack源码之事件流make(2)

    上一节跑到了NormalModuleFactory模块,调用了原型方法create后,依次触发了before-rsolve.factory.resolver事件流,这节从resolver事件流开始讲. ...

  7. .26-浅析webpack源码之事件流make(1)

    compilation事件流中,依然只是针对细节步骤做事件流注入,代码流程如图: // apply => this-compilation // apply => compilation ...

  8. .37-浅析webpack源码之事件流make(4)

    赶紧完结这个系列咯,webpack4都已经出正式版了. 之前的代码搜索到js文件的对应loader,并添加到了对象中返回,流程如下: this.plugin("factory", ...

  9. 浅析libuv源码-node事件轮询解析(3)

    好像博客有观众,那每一篇都画个图吧! 本节简图如下. 上一篇其实啥也没讲,不过node本身就是这么复杂,走流程就要走全套.就像曾经看webpack源码,读了300行代码最后就为了取package.js ...

随机推荐

  1. ThreadLocal 线程本地变量 及 源码分析

    ■ ThreadLocal 定义 ThreadLocal通过为每个线程提供一个独立的变量副本解决了变量并发访问的冲突问题 当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量 ...

  2. Webpack 2 视频教程 019 - Webpack 2 中配置多页面编译

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  3. MySQL:表的操作 知识点难点总结:表完整性约束及其他常用知识点二次总结🙄

    表操作 一 : 修改表表表表表表表表表: ALTER TABLE 语法 1. 改表名rename alter table 表名 rename 新表名 2. 增加字段add alter table 表名 ...

  4. 在macOS上通过pyenv安装和切换多版本Python

    1. 安装homebrew 官网 http://brew.sh/index_zh-cn.html 打开终端,在终端中粘贴如下脚本 /usr/bin/ruby -e "$(curl -fsSL ...

  5. 【转】Python 爬虫的工具列表【预】

    这个列表包含与网页抓取和数据处理的Python库 网络 通用 urllib -网络库(stdlib). requests -网络库. grab – 网络库(基于pycurl). pycurl – 网络 ...

  6. golang其实也可以优先调度

    线上一个服务有个严重问题,处理消息数1k/s提升不上去,经过查看是阻塞在了一个新加的函数上,这个函数负责收集信息,送到一个channel上,再由某个函数处理,这个处理函数很简单,看不出任何问题,最大的 ...

  7. Windows程序设计学习笔记(五)——菜单资源和加速键的使用

    菜单可能是Windows提供的统一用户界面中最重要的一种方式,菜单通常在标题栏的下一行显示,这一栏叫做菜单栏,菜单栏中的每一项称之为菜单项,菜单栏中的每一个菜单项在激活时会显现一个下拉菜单(也可以说是 ...

  8. replace into 浅析之一

    一 介绍  在笔者支持业务过程中,经常遇到开发咨询replace into 的使用场景以及注意事项,这里做个总结.从功能原理,性能和注意事项上做个说明.二 原理2.1 当表中存在主键但是不存在唯一建的 ...

  9. C/C++知识点清单01

    第一章 C/C++程序基础 一.一般赋值语句: 考察一般赋值语句的概念和方法. 1.程序: #include<stdio.h> int main(void) { ,y,z; x*=(y=z ...

  10. hibernate使用注解配置索引

    添加普通索引 @Table(name="t_token", indexes={@Index(name="token_strIndex", columnList= ...