vue-cli v4.5.17 scaffolding project creation and operation principle

1. What is Vue Scaffolding?

I have an article that introduces the relevant knowledge of webpack configuration, including the development environment of Vue, but in real development, it is impossible for us to complete all webpack configurations from scratch for each project, which shows that the efficiency of development will be greatly reduced
  1. So in real development, we usually use scaffolding to create a project. For Vue projects, we use Vue's scaffolding.
  2. Scaffolding is actually a concept in construction engineering. In our software engineering, some tools that help us build projects are also called scaffolding.
  3. Vue's scaffolding isVue CLI
  4. CLI is Command-Line Interface, translated into command line interface
  5. We can select the configuration of the project and create our project through the CLI
  6. Vue CLI has built-in webpack related configuration, we don't need to configure it from scratch

Second, Vue CLI installation and use

  1. Install Vue CLI (currently the latest version is v4.5.17)
  2. We are doing a global installation, so that the project can be created at any time through the vue command
  3. npm install @vue/cli -g
  4. Upgrade Vue CLI
  5. If it is an older version, you can upgrade it with the following command
  6. npm update @vue/cli -g
  7. Create a project with Vue commands
  8. vue create 项目的名称

3. The process of vue create project

insert image description here
insert image description here
insert image description here
insert image description here

Fourth, the project directory structure

insert image description here
insert image description here

Five, the operation principle of Vue CLI

insert image description here
Follow the above flow chart to explain the operation principle of vue-cli in detail

  1. When we run in the terminal npm run serve , we are actually runningvue-cli-service serve

  2. When running , it will automatically find the files in the directory under the vue-cli-service servenode_modules folder.binvue-cli-service
    insert image description here

  3. As you can see in the vue-cli-service file as shown above, it will actually look for it again@vue下的cli-service下的bin下的vue-cli-service.js

  4. After arriving at this file, it can be found that it is essentially carried out new Service()的操作,并且调用了run()方法, and the service instance comes fromlib目录下的Service.js文件
    insert image description here

  5. So let's take a look lib目录下的Service.js文件. As shown in the figure below, the file exports a class. One step of the constructor of this class is to execute resolvePluginsthis function, and this function is to make a mapping to map each configuration file.
    insert image description here
    insert image description here

  6. lib目录下的Service.js文件, and what needs to be called as shown in the run方法figure below, so let's take a look at what this run method does
    insert image description here

  7. According to the above figure, we find that this method will be called first init, so let's take a look at the init method.
    insert image description here

  8. According to the above figure, let's first look at how to load a user-defined configuration fileloadUserOptions()
    insert image description here

  9. Let's take a look again this.plugins.forEach中的apply. We need to pass in two parameters through the above analysis guide. First, let's take a look at the first parameter new PluginAPI(id, this). This parameter creates a new PluginAPI, so take a lookPluginAPI.js这个文件
    insert image description here

  10. Next, let's take a look at the essence of the apply function. Here we take the serve.js file as an example, and the same is true for other files such as build.js.
    insert image description here

  11. So after so many operations above, we now come to the place where webpack is executed. The above operations correspond to the steps of the flowchart. Let's take a look at how the specific webpack is executed.
    insert image description here

  12. The above resolveWebpackConfigmethod is the method provided in the Service.js file
    insert image description here

  13. So the operating principle of vue-cli is probably the above understanding.

Part of the content of this article comes from the courseware of teacher coderwhy

Guess you like

Origin blog.csdn.net/qq_41880073/article/details/123927812