webpack源码笔记

webpack 主入口

  1. 读取 options、判断是否为 multiCompiler
  2. 判断是否为 watch 模式
  3. createCompiler 创建出compiler对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* @param {WebpackOptions} rawOptions options object
* @returns {Compiler} a compiler
*/
const createCompiler = (rawOptions) => {
// 1. 处理 options
const options = getNormalizedWebpackOptions(rawOptions)
// 2. 添加默认配置项
applyWebpackOptionsBaseDefaults(options)
const compiler = new Compiler(options.context)
compiler.options = options
new NodeEnvironmentPlugin({
infrastructureLogging: options.infrastructureLogging,
}).apply(compiler)
if (Array.isArray(options.plugins)) {
for (const plugin of options.plugins) {
if (typeof plugin === 'function') {
plugin.call(compiler, compiler)
} else {
plugin.apply(compiler)
}
}
}
applyWebpackOptionsDefaults(options)
compiler.hooks.environment.call()
compiler.hooks.afterEnvironment.call()
new WebpackOptionsApply().process(options, compiler)
compiler.hooks.initialize.call()
return compiler
}

compiler

Author: liuarui
Link: https://liuarui.github.io/2021/06/03/框架/webpack/源码笔记/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.