This is the 15th day of my participation in the August Text Challenge.More challenges in August

We already know that Webpack automatically uses a configuration file named webpack.config.js, but what if our configuration file is not called webpack.config.js, but something else?

  • For example, we’re going towebpack.config.jsTo modify awk.config.js;
  • At this time we can pass--configTo specify the corresponding configuration file;

We can test this by changing webpack.config.js to wk.config.js, and then executing the NPM run build command from the command line terminal (note, The script “build” in the package.json file is “webpack”), resulting in the following:

Run error: failed to pack successfully. Why should that be? The reason is very simple, because by default when webpack is packaged, when we run the NPM run build command, You’ll find the command “build” in the package.json file “scripts” (” webpack “in this case), which is the difference between executing webpack on the command line. The script command webpack configured in package.json will preferentially use the locally installed (current project) Webpack when executed. If there is no locally installed webpack, the globally installed webpack will be used and webpack will be executed directly from the command line. If there is a file named webpack.config.js in the current folder, the compiler will start the compilation process according to the configuration in this file. If there is no configuration in this file, the compiler will start the compilation process. It will still look for the index.js file in the SRC folder under the current folder. SRC directory has been renamed to main.js, so we can’t find the index.js file.

It’s called wk.config.js instead of webpack.config.js, and we’ll even put a lot of configuration files in one folder later (like config). How do we specify a file as a configuration file for WebPack?

Come on, let’s go to the documentation:

As you can see, we can specify the webpack configuration file with –config, so we can do this: Change the contents of “build” in “scripts” of package.json file to “webpack –config./wk.config.js” (you can also omit the./), as shown in the figure below:

Then execute NPM run build, and the package will compile normally.