webpack series directory
- The core configuration of webpack5
- The modular principle of webpack5
- Babel/ESlint/browser compatibility of webpack5
- Performance optimization of webpack5
- Implementation of Loader and Plugin of webpack5
- Core source code analysis of webpack5
The principle of starting the webpack command
We start webpack usually by configuring webpack commands in npm scripts, such as build packaging commands
"scripts" {
"build": "webpack --config ./config/webpack.common.js --env production"
}
复制代码
1. webpack/bin/webpack.js
We use the webpack command to start with the help of webpack-cli
tools. When we install webpack, there is a line of bin
attributes in webpack's package.json, then npm will use bin
the attribute name inside webpack
as the file name, and the new content in this file in the bin
directory as the content. webpack.js
file, copy and node_modules/.bin
install below
So when we execute the webpack command, we actually execute webpack/bin/webpack.js
this file.
Let's take a look at webpack/bin/webpack.js
this file, the main process is as follows
- First define an
cli
object - When judging whether
cli
the innerinstalled
attribute is true - implement
runCli
That is to say, it will first determine whether webpack-cli
the package is installed. If it is not, it will enter the conditional installation, otherwise it will be executed runCli
. In general, we also use the command package to install webpack
and install webpack-cli
the two packages at the same time, so the main thing is to seerunCli
1. Execute runCli
The main thing in runcli is to splicing the attribute values in the cli into a path, pkgPath
that is webpack-cli/package.json
, pkg
the webpack-cli/package.json
object, and webpack-cli/package.json
the bin
corresponding webpack-cli
attribute value of the attribute in the cli isbin/cli.js
所以最后拼接的是webpack-cli/bin/cli.js
这个路径,将调用这个文件
2. webpack-cli/bin/cli.js
1. 执行runCLI
这个文件其实主要执行的是runCLI
,并且将命令后的参数一起传进去
这里调用了runCLI
的引用来自bootstrap这个文件,我们来看下这个文件
3. webpack-cli/lib/bootstrap.js
1. 创建了WebpackCLI对象
2. 执行cli.run
而这个WebpackCLI
来自webpack-cli.js
4.webpack-cli/lib/webpack-cli.js
run
方法这里主要是执行了以下流程
1. 执行makeCommand
为了检查一些依赖包是否存在
2. 执行makeOption
makeCommand
方法里面执行makeOption
方法,对我们传入的参数做了进一步处理
3. 执行runWebpack
4. 执行createCompiler
而在runWebpack
里面主要执行了createCompiler
5. 执行webpack
而在createCompiler
里面主要调用webpack
这个方法,而这个webpack
方法就是来自webpack包
到这一步其实webpack已经打包完成了
webpack Compiler创建原理
在上述执行webpack函数创建了compiler
,那这个是compiler
是如何创建的呢,我们来看一下这个webpack方法。webpack来自webpack/lib/webpack.js
webpack/lib/webpack.js
在wepack方法里面可以看到,不管是否有回调都会调用create
返回compiler
1. 执行create
在create
方法中主要执行了createCompiler
创建了compiler
2. 执行createCompiler
而在createCompiler
主要做了
new
了一个Compiler
对象plugin.apply
注册了所有的插件- 调用了
environment
和afterEnvironment
环境hook
- 调用
new WebpackOptionsApply().process
将配置属性转为plugin
注册 - 返回
compiler
3. 执行compiler.run
在webpack
方法内首先会判断是否有callback
回调,如果存在回调会执行compiler.run
,如果不存在直接返回compiler
,所以我们在外面在执行webpack
方法获取compiler
后,我们即可以传入一个回调方法,也可以调用run
方法。
webpack/lib/WebpackOptionsApply.js
1. 插件注入plugin.apply()
在webpack
中的createCompiler
里我们调用了new WebpackOptionsApply().process
,我们来看看这里到底怎么实现将配置属性转为plugin
注册
其实在process
方法中,我们将传入的属性转成webpack
的plugin
注入到webpack生命周期内,如上图展示的部分属性做判断,存在就将内置的Plugin进行导入(所以plugin事实上贯穿webpack的整个构建流程),其实这个方法都是在做plugin.apply
的调用注册,并将compiler
对象传入进去,这些Plugin后续会通过tapable
来实现钩子的监听, 并进行自己的处理逻辑
Compiler中run方法执行原理
webpack/lib/Compiler.js
在上述createCompiler
中我们new
了一个Compiler
对象,这个构造方法主要做了什么呢,我们可以看下webpack/lib/Compiler.js
这个文件
当new Compiler
这个构造函数是会初始化各种各样的hooks
,而之前说process
里面的plugin
里会注册这些hooks
,这些hooks
来自一个叫tapable
的库来管理的,这是由webpack
官方自己来维护的一个库,对于tapable
这个库的介绍使用可以看我另一篇webpack文章webpack5之Loader和Plugin的实现。
现在我们来看看Compiler
内的run
方法,其实主要是执行之前plugin
注册的hooks
。
而在Compiler
里面的run
方法里,又定义了一个run
方法,那我们看下这里做了什么
1. 执行run
- 首先执行了
hooks.beforeRun
,执行一些需要运行前操作的plugin
- 再执行了
hooks.run
,执行一些需要运行开始需要操作的plugin
- 执行
compile
方法,并传入了onCompiled
编译完成的回调
2. 执行compile
当执行到this.compile
就是开始准备编译了,我们来看看compile
里面做了什么
- 执行
hooks.beforeCompile
- 执行
hooks.compile
- 执行
hooks.make
- 执行
hooks.finishMake
- 执行
hooks.afterCompile
其实hooks.make
是最终的编译过程,而在hooks.compile
和hooks.make
之间执行了const compilation = this.newCompilation(params);
,并将compilation
传入了hooks.make
。
这里的Compilation与Compiler有什么区别呢
Compiler
- 在webpack构建的之初就会创建的一个对象, 并且在webpack的整个生命周期都会存在
(before - run - beforeCompiler - compile - make - finishMake - afterCompiler - done)
- 只要是做webpack的编译, 都会先创建一个
Compiler
- 如果修改webpack配置需要重新
npm run build
Compilation
- 存在于
compile - make
阶段 watch
源代码,每次发生改变就需要重新编译模块,创建一个新的Compilation
对象
Compilation对Module的处理
上述的hooks.make
只是一个hook的调用,我们要去找注册在这个钩子上的回调,我们可以前往process
内的new EntryOptionPlugin().apply(compiler)
这个entry
插件
1. webpack/lib/EntryPlugin.js
这个插件在apply
里调用applyEntryOption
,而里面又调用EntryPlugin
插件
EntryPlugin
插件内可以看到注册了hooks.make
而在注册回调中主要执行了compilation.addEntry
,那我们来看看在compilation
这个对象中主要做了什么
2. webpack/lib/Compilation.js
在执行compilation.addEntry
这里主要做了
- 执行
_addEntryItem
,用于添加入口的Item - 执行
addModuleTree
- 在
addModuleTree
中执行handleModuleCreation
- 在
handleModuleCreation
中执行factorizeModule
,添加hooks
到factorizeQueue
队列中 - 在
handleModuleCreation
中执行addModule
,添加module
模块到addModuleQueue
队列中 - 在
addModule
中执行buildModule
,将需要构建的module
模块添加到buildQueue
队列中 buildQueue
队列中有一个processor
属性,执行_buildModule
_buildModule
中执行module.needBuild
判断模块是否需要构建- 执行
module.build
, - 最后会在
wepack/lib/NormalModule.js
中执行build
方法,开始构建模块
module的build阶段
上面在处理module
的最后在wepack/lib/NormalModule.js
中执行build
方法,开始构建模块,那现在我们来看看build
做了哪些内容
wepack/lib/NormalModule.js
1. 执行doBuild
2. 执行_doBuild
执行doBuild
内的_doBuild
方法
3. 执行runLoaders
执行_doBuild
内runLoaders
,这个runLoaders
来自独立的loader-runner
库,我们之前配置的各种Loaders就是在这里处理的
4. 执行processResult
runLoaders
执行结束后回执行processResult
这个回调
5. 执行parse
之后会调用parse
解析AST
树
而这个parse
来自webpack/lib/javascript/JavascriptParser.js
内的parse
这个parse
其实是用到了acorn
这个库来解析javascript
6. 执行handleBuildDone
解析完后会调用handleParseResult
回调,里面执行handleBuildDone
handleBuildDone
里又执行了build
里面传进来的回调
最终执行的webpack/lib/Compilation.js
下的module.build
传进来的回调
7. _buildModule执行完成
当_buildModule
执行完成后,最终hooks.make
执行完成,于是接下来会执行webpack/lib/Compiler.js
的compilation.finish
和compilation.seal
方法
到seal
这一步,就是开始将静态资源输出到构建目录了
输出静态资源到构建目录
1. 执行hooks.optimizeChunkModules
Execute first hooks.optimizeChunkModules
, optimize the module code before
2. Execute codeGeneration
execute codeGeneration
, generate code
3. Execute createChunkAssets
execute createChunkAssets
, create chunkAssets
resource
4. Execute getRenderManifest
In the execution createChunkAssets
, getRenderManifest
put all the data into a manifest object
5. Execute emitAsset
Execute emitAsset
, output resources, at this time resources have been stored in memory
6. Execute onCompiled
webpack/lib/Compiler
Execute callback after final compile
completiononCompiled
7. Execute emitAssets
onCompiled
Execute in the callback emitAssets
,
8. Execute hooks.emit
emitAssets
Finally execute hooks.emit
the export of the resource to the build directory inside
end
The introduction of the source code may still be incomplete. When viewing the source code, we can use the debugger tool of vscode to view the code direction through breakpoints. The above introduction should be able to roughly clarify the process of webpack execution, but more For the details, I still hope that you can explore more with the debugger.