About pits created with Vite
-
There are still a lot of deprecated vite packages on the web, but there are a lot of misleading actions and deprecated packages from older articles and writers who have compiled articles based on videos.
-
Deprecated package 1@vitejs /create-app
- Package 2 has been deprecated
create-vite-app
- Deprecated package 2 is one of the most commonly encountered packages on the web for creating Vites.
- The Vite production phase does not skip compilation
- Nested imports cause additional network roundtrips, and it is still inefficient to publish unpackaged ESMs in a production environment
- To get the best loading performance in production
About vite
- Vite is a packaging tool, vite official explanation: is the next generation of packaging tool.
- about
webpack
.Rollup
andParcel
As projects get larger and larger, the amount of JS code increases exponentially - Vite takes advantage of the fact that browsers are starting to natively support the ES module
- Vite native support ts files, support TS file translation
- about
Build vUE projects using Vite
- Note: Vite itself relies on Node, so you need to have the Node environment installed as well
- Vite requires Node versions older than 12
- Installation method 1 :(official)
npm init vite@latest my-vue-app --template vue
- By default, vue projects with Vite are installed directly
- Note: to
npm install
Initialize the project
- Installation Method two:
- First of all:
npm init create-vite -g
Global installation project builder tool create-vite my-vue-app
: Select vUE. If ts is used, continue to select VUe-ts- vanilla
- vue
- react
- preact
- lit-element
- svelte
npm install
: Initializes the project
- First of all:
- Learn about Vite fast builds
- Browser native support for ES module features, build projects will be
script
Of the labeltype
Property set tomodule
- If we look at the request in the browser, we will see that the request is still the TS file:
- This is because the server Connect in Vite forwards our request;
- Get the ts compiled code and return it to the browser, which can parse it directly
- And most of the logic is done by plug-in hooks rather than middleware, which greatly reduces the need for middleware
- Browser native support for ES module features, build projects will be
- The official explanation:
- Cold start development server, where modules are distinguished asRely onandThe source code
- Dependencies :Vite will pre-build dependencies using esBuild
- Source code :Vite only needs to translate the source code when the browser requests it and provide it on demand(Load on demand)
- The source code usually contains files that are not directly JavaScript that need to be transformed (such as JSX,CSS, or Vue/Svelte components) and often edited. And not all source code needs to be loaded at the same time (routing)
- Hot update (HMR) :
- Note that this is very important:
- Vite also makes use of HTTP headers to speed up whole page reloads (again making the browser do more for us)
- Requests from source modules are negotiated cached according to 304 Not Modified, while dependent module requests are strongly cached via cache-control: max-age=31536000,immutable, so they do Not need to be requested again once cached
- Note that this is very important:
- Cold start development server, where modules are distinguished asRely onandThe source code
- Specific, official documentation cn. Vitejs. Dev/guide/according to h…
Using vite
- in
package.json
In the file
"scripts": {
"dev": "vite"."build": "vue-tsc --noEmit && vite build"."serve": "vite preview"
}
Copy the code
npm run dev
Start the development servernpm run build
Build artifacts for the production environmentvue-tsc --noEmit
Type checking tool
npm run serve
Local preview production build artifacts
Vite uses ESBuild packaging instead of Babel
-
The characteristics of ESBuild
- Super fast build times and no caching required
- Support ES6 and CommonJS modularity
- Tree Shaking with ES6 support
- Support Go,JavaScript API
- Support TypeScript,JSX, and other syntax compilation
- Support SourceMap
- Support code compression
- Support extension of other plug-ins
-
ESBuild quick packaging reasons:
- Written in the Go language, it can be directly converted to machine code without passing bytecode
- ESBuild takes full advantage of the CPU’s multiple cores, running them as full as possible
- Everything in ESBuild is written from scratch, rather than using a third party, so you can consider various performance issues from the start