Using vite to build Vue project

Wang Ji 2021-02-23 14:39:29
using vite build vue project


We knew last year that Youda had developed a new web Develop build tools Vite, the other day Vite2.0 It's officially released ! It's time to learn !

Build the first Vite project :

Node.js need >12.0.0

npm init @vitejs/app

After installation, you can specify the project name :
image
Select the initialization template :
image
When the selection is complete :

cd vite-project
npm install
npm run dev

first Vite+Vue3.0 The project is set up !Vite The default application template is 3.x, If the project starts , In the browser. F12 I can't see Vue Options ,Vue.js not detected, Need to update devtools, Update address

Quick build method :

# npm 6.x
npm init @vitejs/app my-project --template vue
# npm 7+ Need extra double horizontal lines :
npm init @vitejs/app my-project -- --template vue

pack :

npm run build

After packing, open the project blank , Need configuration vite.config.js Medium base The basic path is './'

environment variable :

Vite In a special import.meta.env Expose environment variables on objects

  • import.meta.env.MODE: String The environment mode of application running
  • import.meta.env.BASE_URL: String Applications are being deployed on base URL, from base Configuration items determine
  • import.meta.env.PROD: Boolean Whether the application is running in the production environment
  • import.meta.env.DEV: Boolean Whether the application is running in the development environment

.env file

.env # It will load in all cases
.env.local # It will load in all cases , But it will be git Ignore ( Need to be in .gitignore Medium plus .local)
.env.[mode] # Load only in the specified mode
.env.[mode].local # Load only in the specified mode , Will be git Ignore 

vite.config.js Common configuration

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
// Array of plug-ins to use
plugins: [vue()],
// A common infrastructure path for developing or producing environmental services , It can be /foo/、https://foo.com/、 Empty string or ./( For development environment ) Several types , This option can also be specified through command line arguments ( example :vite build --base=/my/public/path/)
base: './',
// Folder for static resource services , Default "public"
publicDir: 'public',
css: {
postcss: {
plugins: [
require('autoprefixer')
]
}
},
server: {
// Server hostname , If external access is allowed , Can be set to "0.0.0.0"
host: '0.0.0.0',
// Server port number
port: 3000,
// boolean | string Automatically open the application in the browser when you start the project ; If string, such as "/index.html", Will open http://localhost:3000/index.html
open: false,
// Custom proxy rules
proxy: {
'/api': {
target: 'http://jsonplaceholder.typicode.com',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
}
}
},
build: {
// Specify the output path , Default 'dist'
outDir: 'dist',
// Specify the storage path of the generated static resources ( be relative to build.outDir)
assetsDir: 'assets',
// An import or reference resource less than this threshold will be inlined as base64 code , Set to 0 This can be disabled . Default 4096(4kb)
assetsInlineLimit: '4096',
// Enable / Ban CSS Code splitting , If you disable , All of the whole project CSS Will be extracted into a CSS In file , Default true
cssCodeSplit: true,
// Whether to generate after construction source map file , Default false
sourcemap: false,
// by true when , Will generate manifest.json file , For back end Integration
manifest: false
}
})
版权声明
本文为[Wang Ji]所创,转载请带上原文链接,感谢
https://qdmana.com/2021/02/20210223143906205V.html

  1. 对前端异常window error捕获的全面总结
  2. A comprehensive summary of front end exception window error capture
  3. 成功解决Problem while trying to mount target]\“. HTTP response code is 400
  4. Problem while trying to mount target] \ ". HTTP response code is 400
  5. 放弃okhttp、httpClient,选择了这个牛逼的神仙工具!贼爽
  6. 前端面试每日 3+1 —— 第679天
  7. How to add elements at the beginning of an array in JS?
  8. Give up okhttp and httpclient and choose this awesome immortal tool! Thief Shuang
  9. Front end interview daily 3 + 1 - day 679
  10. 【2021 第一期】日常开发 26 个常见的 JavaScript 代码优化方案
  11. Daily development of 26 common JavaScript code optimization schemes
  12. 前端的字符串时间如何自动转换为后端Java的Date属性,介绍springMVC中如何解决时间转换问题
  13. How to automatically convert the front-end string time to the back-end Java date attribute, and how to solve the time conversion problem in spring MVC are introduced
  14. 前端面试常考题:JS垃圾回收机制
  15. ReactDOM.render串联渲染链路(一)
  16. 更简洁、更快速!腾讯云 Serverless 云函数创建流程再次升级!
  17. 粗涉Webpack
  18. Frequently asked questions in front end interview: JS garbage collection mechanism
  19. ReactDOM.render Serial rendering link (1)
  20. More concise and faster! Tencent cloud serverless cloud function creation process upgrade again!
  21. 更简洁、更快速!腾讯云 Serverless 云函数创建流程再次升级!
  22. About webpack
  23. More concise and faster! Tencent cloud serverless cloud function creation process upgrade again!
  24. 详解vue静态资源打包中的坑与解决方案
  25. 一篇搞懂TCP、HTTP、Socket、Socket连接池
  26. 字节跳动2021前端技术岗发布+最新内部面试题
  27. Detailed explanation of Vue static resource packaging and Solutions
  28. Understanding TCP, HTTP, socket, socket connection pool
  29. 2008-2021 front end technical post release + latest internal interview questions
  30. 4. Vue基本指令
  31. 4. Vue basic instruction
  32. Java 发起 http 请求
  33. Java initiates HTTP request
  34. 网站由http升级为https图文教程
  35. Upgrade the website from HTTP to HTTPS
  36. 更简洁、更快速!腾讯云 Serverless 云函数创建流程再次升级!
  37. More concise and faster! Tencent cloud serverless cloud function creation process upgrade again!
  38. 混合开发入门 Vue结合Android/iOS开发仿京东项目App
  39. Hybrid development entry Vue combined with Android / IOS to develop app imitating Jingdong project
  40. 无缝对接 Tableau,这家月活跃用户 5000+ 的大型银行如何实现自助式分析?
  41. Are airpods still the strongest? Horizontal evaluation of 4 true wireless headphones
  42. Seamless connection with tableau, how can this large bank with 5000 + active users realize self-service analysis?
  43. react-native版文字跑马灯
  44. React native text running lantern
  45. Java、JavaScript、C、C++、PHP、Python都是用来开发什么?
  46. What are Java, JavaScript, C, C + +, PHP and python used to develop?
  47. this.byId(SupplierForm).bindElement in SAP UI5
  48. SAP UI5 JavaScript文件的lazy load - 懒加载
  49. this.byId (SupplierForm).bindElement in SAP UI5
  50. Lazy load lazy load of SAP ui5 JavaScript files
  51. "Gnome 3" - interface elements, desktop components, part names (learning notes) @ 20210223
  52. How to connect the ground gas to the micro front end?
  53. How to transform single / micro service application into serverless application
  54. 在 2021 年你需要掌握的 7 种关于 JavaScript 的数组方法
  55. Seven array methods for JavaScript you need to master in 2021
  56. 在 2021 年你需要掌握的 7 种关于 JavaScript 的数组方法
  57. Seven array methods for JavaScript you need to master in 2021
  58. 在 2021 年你需要掌握的 7 种关于 JavaScript 的数组方法
  59. Seven array methods for JavaScript you need to master in 2021
  60. RxHttp - 轻量级、可扩展、易使用、完美兼容MVVM、MVC架构的网络封装类库