Through this article you will learn the following:
- 1, npm create specific execution process
- 2, three libraries of minimist, prompts and kolorist
- 3. Source code analysis of create-vite
Vite source code download:
//复制一份vite源码到自己的本地
git clone https://github.com/vitejs/vite.git
//安装依赖 npm i
First, how does the npm create vite command trigger?
From the vite
official documentation, we can know that we can npm create vite
initialize a vite
project through , so npm create vite
how does it work? With this question in mind, I checked npm
relevant information.
First of all, we must first understand npm
what exactly is it? Excerpted from the official explanation: NPM
The full name is Node Package Manager
a NodeJS
package management and distribution tool, which has become the standard for unofficial release Node
modules (packages). We can compare it to a package management tool. Therefore, the packages that users want to use and the packages that they want to provide to others can be "traded" through this tool. Since it is a tool, to use it, you need to abide by the rules it makes. But to understand npm create
the principle of , you must first know npm init
. As you can see from the picture below, they are one thing.
On the official website of npm, we learned that npm init + initializer
, npm
will execute the npm exec
command. npm create vite
->npm exec create vite
npm exec
: run command from local or remote npm package
Here, npm
the create-vite
package , and then package.json bin
the create-vite
command will be run. Next, let's open the source code for analysis to see what ispackage.json bin
actually executed in ?create-vite
2. Source code directory analysis and code debugging
After we github
downloaded vite
the source code, open the directory, first analyze its directory structure, you can see the following figure:
We find the command create-vite
package corresponding topackage.json bin
create-vite
find the finalindex.ts
Next, debug the code. After working for a long time, I ts
can't directly node
execute the command. I found two methods to debug on the Internet.
One is to use vscode
compile it into js
execute the code.
One is to use npx esno src/index.ts
or npx tsx src/index.ts
to debug.
npx esno src/index.ts
Opening the debug terminal with will report an error .
I don’t know why, but the break point of the second method cannot take effect, so when debugging the code, an error will be reported, or compile it first js
and then execute it. During the execution process, I encountered the error shown in the figure below. After troubleshooting, I found that it was a nodeJs
version problem. I used10.15.3
Need to pay attention to the nodeJs
version:
Three, init main process source code analysis
Before the analysis, you first need to be familiar with several libraries introduced in the code.
Then look at the code, it will be much easier
1. Obtain the user's input and options, as well as the specific directory of the project download.
2. Determine whether there are custom attributes
3. Obtain the template directory and write it to the user-specified folder, and modify the package.json file
4. Prompt the user how to run the initialization project
Fourth, the extension function
1. File copy
2. Replace the backslash /
with an empty string
3. Information processing corresponding to the package manager
At last
Recently, I also sorted out a JavaScript and ES note, a total of 25 important knowledge points, and explained and analyzed each knowledge point. It can help you quickly master the relevant knowledge of JavaScript and ES, and improve work efficiency.
Friends in need, you can click the card below to receive and share for free