There are two common usages of webpack: First, using configuration files, and not using configuration files (webpack <entry> [<entry>] <output>). The parameters of these two methods are not initialized in much the same way:
Method One, read and merge parameters from the configuration file and Shell statements, and then read the entry and exit parameters directly from the shell statement. This article will be combined with the source code, analysis of the implementation of these two methods. (The Webpack version used in the article is 3.10.0)
Webpack execution of the entry file is the bin directory under the webpack.js, responsible for parameter conversion processing of the file is the bin directory under the Convert-argv.js. Here's a quick introduction to the two files.
Entry file Webpack.js
The approximate function of the import file is: Through the convert-argv.js parameter initialization processing, the Webpack.js file obtains the processing parameter, passes to the Lib directory the Webpack.js file obtains the compilation object complier, then Complier calls the Run method succeeds, after compiles The information is printed to the screen. The webpack.js of the ingress file is as follows (pseudo-code is only for illustration and cannot actually run):
parameter Conversion convert-argv.js
Webpack the conversion processing of parameters in this file, the process is a little more cumbersome, we use a flowchart to introduce the way.
1, Webpack First will specify a default configuration file array defaultconfigfiles, which is stored in the file name and extension of the object, for example:
{path:e:/demo/webpack.config.js, Ext:js} or {path:e:/demo/webpack.config.js, Ext:java}
2, the program to determine the user has no other name through the command line configuration file. Webpack enables users to use the following commands:
Webpack--config Example.config.js
3. The files specified by the user through the Config command, or the information read from the default configuration array, are saved in the Configfiles array, and the format of the objects saved in the array is also
{path:e:/demo/webpack.config.js, Ext:js}
4, by configfiles the length of the array, the program determines whether the user uses configuration files to configure Webpack. The array length is zero, indicating that the user is not using the configuration file, the entry and output information needs to be read from the command line, and if the length is nonzero, the information needs to be read and merged from the configuration file and the shell statement, when the program loads the configuration file in require mode. Reads the configured object or array.
5, using the Processconfiguredoptions method to deal with options, the options parameters of the introduction of roughly three forms: one, empty objects; second, ordinary objects; three, array objects.
If the object is empty, the description is to use Webpack <entry> [<entry>] <output>, which requires entry and output information to be obtained from argv._;
If it is a normal object, merge the parameters passed in from the normal object and the shell statement
If it is a numeric object, it is necessary to traverse every ordinary object and go into the process of ordinary objects.
When finished, returns an Options object or an options array.
Webpack Source code Analysis--parameter initialization