.21-浅析webpack源码之事件流this-compilation
上一节生成Compilation实例后,添加了一些属性,随后触发this-compilation事件流,如下:
Compiler.prototype.newCompilation = (params) => {
// new Compilation()
const compilation = this.createCompilation();
compilation.fileTimestamps = this.fileTimestamps;
compilation.contextTimestamps = this.contextTimestamps;
compilation.name = this.name;
compilation.records = this.records;
compilation.compilationDependencies = params.compilationDependencies;
// Go!
this.applyPlugins("this-compilation", compilation, params);
this.applyPlugins("compilation", compilation, params);
return compilation;
}
事件流的名字this-compilation我想了半天也不懂啥意思,从其内容来看其实也只算是一个预编译,叫pre-compilation似乎更好。
总之先不管那么多,继续跑流程,流程图如下:
this-compilation事件流的plugin来源有两个地方,分别是:
// JsonpTemplatePlugin
class JsonpTemplatePlugin {
apply(compiler) {
compiler.plugin("this-compilation", (compilation) => {
compilation.mainTemplate.apply(new JsonpMainTemplatePlugin());
compilation.chunkTemplate.apply(new JsonpChunkTemplatePlugin());
compilation.hotUpdateChunkTemplate.apply(new JsonpHotUpdateChunkTemplatePlugin());
});
}
}
// CachePlugin
compiler.plugin("this-compilation", compilation => {
// TODO remove notCacheable for webpack 4
if (!compilation.notCacheable) {
compilation.cache = cache;
compilation.plugin("child-compiler", (childCompiler, compilerName, compilerIndex) => { /**/ });
} else if (this.watching) {
compilation.warnings.push(
new Error(`CachePlugin - Cache cannot be used because of: ${compilation.notCacheable}`)
);
}
});
两者都出现在WebpackOptionsApply模块中,依次看具体内容。
JsonpTemplatePlugin
这里依次在上节中提到的Compilation几个属性上加载插件(Tapable),首先是:
compilation.mainTemplate.apply(new JsonpMainTemplatePlugin());
该插件源码整理如下:
"use strict";
const Template = require("./Template");
class JsonpMainTemplatePlugin {
apply(mainTemplate) {
// this.plugin("startup", (source, chunk, hash) => { /**/ });
// this.plugin("render", (bootstrapSource, chunk, hash, moduleTemplate, dependencyTemplates) => { /**/ });
// this.plugin("local-vars", (source, chunk, hash) => { /**/ });
// this.plugin("require", (source, chunk, hash) => { /**/ });
// this.plugin("module-obj", (source, chunk, hash, varModuleId) => { /**/ });
// this.plugin("require-extensions", (source, chunk, hash) => { /**/ });
mainTemplate.plugin("local-vars", function(source, chunk) { /**/ });
mainTemplate.plugin("jsonp-script", function(_, chunk, hash) { /**/ });
mainTemplate.plugin("require-ensure", function(_, chunk, hash) { /**/ });
mainTemplate.plugin("require-extensions", function(source, chunk) { /**/ });
mainTemplate.plugin("bootstrap", function(source, chunk, hash) { /**/ });
mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) { /**/ });
mainTemplate.plugin("hash", function(hash) { /**/ });
}
}
module.exports = JsonpMainTemplatePlugin;
可见,这里只是注入对应的事件流,这里我在注释同时给出了该属性初始化时的plugin,可以对比一下,只有local-vars是重复的。
既然没有任何的apply操作,就暂时先跳过。
然后是第二个:
compilation.chunkTemplate.apply(new JsonpChunkTemplatePlugin());
源码如下:
"use strict";
const ConcatSource = require("webpack-sources").ConcatSource;
class JsonpChunkTemplatePlugin {
apply(chunkTemplate) {
chunkTemplate.plugin("render", function(modules, chunk) { /**/ });
chunkTemplate.plugin("hash", function(hash) { /**/ });
}
}
module.exports = JsonpChunkTemplatePlugin;
同样只是注入事件流,该属性在初始化没有做操作,所有事件流只有这两。
第三个:
compilation.hotUpdateChunkTemplate.apply(new JsonpHotUpdateChunkTemplatePlugin());
"use strict";
const ConcatSource = require("webpack-sources").ConcatSource;
class JsonpHotUpdateChunkTemplatePlugin {
apply(hotUpdateChunkTemplate) {
hotUpdateChunkTemplate.plugin("render", function(modulesSource, modules, removedModules, hash, id) { /**/ });
hotUpdateChunkTemplate.plugin("hash", function(hash) { /**/ });
}
}
module.exports = JsonpHotUpdateChunkTemplatePlugin;
与上面那个类似。
该模块注入完结。
CachePlugin
该插件注入了多个事件流,直接上与this-compilation事件流相关的代码:
compiler.plugin("this-compilation", compilation => {
// TODO remove notCacheable for webpack 4
// 这个属性我从头到尾找不到哪出现的
// 反正注释说webpack4会将其移除
if (!compilation.notCacheable) {
// cache => {}
compilation.cache = cache;
// 注入事件流
compilation.plugin("child-compiler", (childCompiler, compilerName, compilerIndex) => { /**/ });
}
// 不可能到达的else
else if (this.watching) {
compilation.warnings.push(
new Error(`CachePlugin - Cache cannot be used because of: ${compilation.notCacheable}`)
);
}
});
果然是只是注入事件流,这里的notCacheable不知道在哪定义的,也不知道如何修改。
总之this-compilation也并不是编译,只是为一些辅助模块注入事件流。
.21-浅析webpack源码之事件流this-compilation的更多相关文章
- .34-浅析webpack源码之事件流make(3)
新年好呀~过个年光打游戏,function都写不顺溜了. 上一节的代码到这里了: // NormalModuleFactory的resolver事件流 this.plugin("resolv ...
- .23-浅析webpack源码之事件流compilation(1)
正式开始跑编译,依次解析,首先是: compiler.apply( new JsonpTemplatePlugin(options.output), // start new FunctionModu ...
- .27-浅析webpack源码之事件流make(2)
上一节跑到了NormalModuleFactory模块,调用了原型方法create后,依次触发了before-rsolve.factory.resolver事件流,这节从resolver事件流开始讲. ...
- .26-浅析webpack源码之事件流make(1)
compilation事件流中,依然只是针对细节步骤做事件流注入,代码流程如图: // apply => this-compilation // apply => compilation ...
- .24-浅析webpack源码之事件流compilation(2)
下一个compilation来源于以下代码: compiler.apply(new EntryOptionPlugin()); compiler.applyPluginsBailResult(&quo ...
- .22-浅析webpack源码之事件流compilation总览
呃,终于到了这地方-- newCompilation(params) { // ... this.applyPlugins("this-compilation", compilat ...
- .25-浅析webpack源码之事件流compilation(3)
这一节跑下一批plugin. compiler.apply( new EnsureChunkConditionsPlugin(), new RemoveParentModulesPlugin(), n ...
- .37-浅析webpack源码之事件流make(4)
赶紧完结这个系列咯,webpack4都已经出正式版了. 之前的代码搜索到js文件的对应loader,并添加到了对象中返回,流程如下: this.plugin("factory", ...
- 浅析libuv源码-node事件轮询解析(3)
好像博客有观众,那每一篇都画个图吧! 本节简图如下. 上一篇其实啥也没讲,不过node本身就是这么复杂,走流程就要走全套.就像曾经看webpack源码,读了300行代码最后就为了取package.js ...
随机推荐
- [javascript-snippet]使用javascript+html5实现图片的灰度处理
// 源码出自:潇湘夜雨<!DOCTYPE> <html> <head> <meta charset="utf-8"/> </ ...
- OpenStack Kilo版加CEPH部署手册
OpenStack Kilo版加CEPH部署手册 作者: yz联系方式: QQ: 949587200日期: 2015-7-13版本: Kilo 转载地址: http://mp.weixin.qq.co ...
- 调试 .NET Framework 源代码、.DotNetCore源码
调试 .NET Framework 源代码..DotNetCore源码 如何调试 .NET Framework 源代码 在 Visual Studio 调试器中指定符号 (.pdb) 和源文件 .NE ...
- C#为什么不能像C/C++一样的支持函数只读传参
C#为什么不能像C/C++一样的支持函数只读传参? 这个问题其实问的人挺多的,我自己也经常想实现这个功能,但是发现总是那么的不尽人意. 有些人倒是给出了一下答案,但是都不能很好的解决像C/C++一样的 ...
- 记录.NET Core在CentOS上基于Jenkins自动化发布
1.安装Jenkins,我这里采用的是非docker方式安装(两种都行,任选一种) 参考:https://www.cnblogs.com/xiaxiaolu/p/10357806.html https ...
- 浏览器环境下Javascript脚本加载与执行探析之DOMContentLoaded
在”浏览器环境下Javascript脚本加载与执行探析“系列文章的前几篇,分别针对浏览器环境下JavaScript加载与执行相关的知识点或者属性进行了探究,感兴趣的同学可以先行阅读前几篇文章,了解相关 ...
- 企业项目开发--cookie(1)
此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 注:本章代码基于<第五章 企业项目开发--mybatis注解与xml并用>的代码,链接如下: h ...
- Java正则表达式初学者使用法简介
在Java中使用正则表达式的方法非常多,最简单的就是和字符串一起使用.对于Java正则表达式初学者,在String中有四个方法可以使用正则表达式,本文正是介绍这四个方法来使用正则表达式来处理文本数据. ...
- MariaDB 存储过程与函数(10)
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQ ...
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...