Preface

The compiler object is a global singleton, which is responsible for controlling the entire webpack packaging construction process.
This article will introduce what webpack did before the new compiler

Start webpack

Under normal circumstances, we use the following method to start webpack

// package.json
script: {
   "start": "webpack"
}

webpack/bin

After running npm run start , it will first enter webpack/bin , webpack uses

isInstalled("webpack-cli")

To determine whether cli is installed, if it is not installed, it will use yarn or npm help you install it, and finally it will come to runCli , the core code is just one sentence

require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]));

Go here to read webpack-cli/bin/cli.js .

webpack-cli/bin

webpack-cli/bin/cli.js actually has to judge whether webpack is installed (not judged above), if it is not installed, install it for you, then instantiate a webpack-cli object and execute the run method of the instance.
I won’t elaborate on the code inside, but actually did two things:

  • Get the parameters of process.args and verify
  • Combine the parameters and add the corresponding plugin to the webpack config according to the value of args

Finally got the parameters and called webpack .

Two packages are used here to improve operating efficiency, one is import-local , which is used to give priority to local files, and the other is v8-compile-cache , which is used to optimize the compilation and cache of v8. We will talk about these two later

Back to webpack

After returning to webpack, we will start to create compiler instance of 06160404f10c44. Before that, there will actually be a bit of branching logic that needs to be processed.

  • If the parameter is an array, create an instance of MultiCompiler, otherwise create an instance of Compiler.
  • The checksum of the parameter copies the default value

After everything is done, it is time to create the compiler object.

Concluding remarks

Webpack didn't do much before creating the compiler To put it simply in one sentence, ready for the parameter . At the same time, there are some performance optimization methods, which are not in the scope of this series of discussion, and I will share with you when I have time in the future.


csywweb
232 声望8 粉丝

最担心的事不是写出ipcode,而是从不开始