Webpack design idea
- Parse the dependency tree from the entry file
- Loader converts each file node
- Plugin listens for compilation events
webpack intermediate process from entry file to product output
- Read the webpack config file, merge with the built-in parameters, get the webpack startup initial parameters, and initialize the compiler
two。 Load all plugin plug-ins and register listening events
- Get the entry entry from the startup parameters and start the compilation
- Convert the file according to the loaders matched in rules
- Loaders first executes the pitch method from left to right, and then executes the normal method from right to left to complete the conversion processing of the file node
- After all files are processed, a file dependency tree is obtained, i.e., the content after file conversion and its dependency nodes, and then the merged processing is packaged and output into a file.
- When the above process is special, the built-in event will be triggered. Plugin can register to listen through the apply method.
Loader principle
Loader is a conversion function that inputs the original text and outputs the converted text, such as simply implementing ts-loader
const ts = require('typescript')
// normal方法
module.exports = (source) => {
return ts.transpile(source).outputText
}
// pitch方法
module.exports.pitch = (source) => {
// 这里如果返回了值,就会停止向右pitch,开始向左执行normal
// return source
}
Plugin Principle
Webpack itself is event-based and contains a number of built-in plug-ins that are executed based on event subscriptions. A simple plug-in is as follows:
class MyHtmlPlugin {
constructor(options) {
// 从参数拿到html模板
this.templateContent = options.templateContent
}
apply(compiler){
const self = this
// 监听emit事件
compiler.hooks.emit.tapAsync('MyHtmlPlugin', (compilation, callback) => {
// 往assets列表添加一个index.html
compilation.assets['index.html'] = {
source() {
return self.templateContent
},
size() {
// 返回文件字节大小
return this.source().length
}
}
callback()
})
}
}
module.exports = MyHtmlPlugin;
Common events:
- AfterPlugins: start a new compilation
- Compile: before creating the compilation object
- Compilation: the compilation object has been created
- Emit: resource generation is complete, before output
- AfterEmit: complete the output of resources to the directory
- Done: complete compilation