Fair trade! About 14 of knowledge webpack4

Recent work uses nuxt, only to find, to learn if webpack 6, nuxt basically no learning, no learning costs, and therefore, this re-record some basic knowledge under webpack4, the next will be configured to optimize a ultimate react scaffolding , but also hope that we can continue to focus, and then configure webpack is to optimize optimization optimization, ha ha ~

Gall strong wine counseling people, I learn this way basically three steps:

  1. First, the necessary configuration, as well as some of the loader, some plug-ins, like language texts as silent reading, and recite (this step is the most important)
  2. Hands to practice, to trial and error
  3. Understand its principles

Well, the official start

Foreword

Webpack packer module can be seen: it to do is analyze your project structure, expand find language (Scss, TypeScript, etc.) JavaScript modules as well as some other browsers can not run directly, and package it as appropriate format for browsers.

WebPack and Grunt and Gulp compared to what features

In fact Webpack and the other two did not have much comparable, Gulp / Grunt is a tool to optimize the front end of the development process, and WebPack is a modular solution, but the advantages of Webpack makes Webpack in many scenes Alternatively the tool can Gulp / Grunt class.

  1. Entry: entry, the first step in execution of the build Webpack Entry will start, can be abstracted as an input.
  2. Module: module, where everything is in Webpack module, a module corresponds to a file. Webpack will be configured to start from the Entry recursively find all dependent modules.
  3. Chunk: code block, a combination of a plurality of modules made Chunk, code for merging and segmentation.
  4. Loader: converter module, the module for the original content into the new content on demand.
  5. Plugin: extension, in particular the timing of the build process Webpack injection expansion logic to change the results of the build or do what you want.
  6. Output: output, in Webpack after a series of treatment and obtain the desired output code will eventually result.

1. From zero arrangement

  • Initialize the project structure
12462640-9e03bc765d21b3af.png

2. Configure webpack.config.js

  • In the project root directory create webpack.config.js
12462640-07d6027da7d2aac6.png

3. Configure development server

12462640-76cee113b53d8f05.png

4. Packing js

12462640-a5401d7ef67c1921.png

5. Support ES6, react, vue

12462640-d4921886460abf1b.png

6. The process css, sass, and attribute prefix css3

Processing css

12462640-cba69c8f6624b60b.png

Dynamic loading and unloadingCSS

style-loader provides the use () and UNUSE () of the object css two ways to load and unload css

For example, to achieve a demand-based switching colors click, modify index.js

12462640-b92a9ced9795ad29.png

Processing sass

12462640-8f7463f437736237.png

Css file is extracted as a separate file

12462640-f006f06eed2e8593.png

7. output html

12462640-55dc7657c6031cf3.png

8. The process referenced third-party libraries, exposure to global variables

webpack.ProvidePlugin parameters are key-value pairs in the form, the key is the variable name we used in the project, the value is the key point to library

12462640-33dada22c9fa12dc.png

9. code splitting, lazy loading (demand loading)

It means carrying loaded when needed, such as a scene, just click the button to load a js.

12462640-c642da3f33140744.png

10. JS Tree Shaking

12462640-43758df953021d26.png

11. Image processing

12462640-becb4ccf12eabdcf.png

12. Clean Plugin and Watch Mode

Empty directories, files, there are changes to repackage

12462640-3ee2a4cf648de8ec.png

13. The distinction between environment variables

12462640-e16e9be879db9140.png

14. The development modes webpack-dev-server, proxy

12462640-5753003656d952c7.png

Here basically is over, I feel helpful, might point a praise , deficiencies, but also look treatise ~

Author: Zhang not counseled
link: https://juejin.im/post/5cea1e1ae51d4510664d1652

Points by praise, seeking attention ~


14064769-2e3882154ac05004.gif

Reproduced in: https: //www.jianshu.com/p/522b1c2d40b6

Guess you like

Origin blog.csdn.net/weixin_34304013/article/details/91069181