vite基础使用

vite高级使用

SSR

HMR

vite原理

rollup+vite

https://juejin.cn/post/6869915676501835783#heading-4

vite源码解析

实践

vite快速创建vue3+vite项目

  1. npm init @vitejs/app stu --template vue // npm 6.x

项目配置信息

  1. // package.json
  2. "scripts": {
  3. "dev": "vite",
  4. "build": "vite build",
  5. "serve": "vite preview"
  6. },
  7. "dependencies": {
  8. "vue": "^3.0.5"
  9. },
  10. "devDependencies": {
  11. "@vitejs/plugin-vue": "^1.1.5",
  12. "@vue/compiler-sfc": "^3.0.5",
  13. "vite": "^2.0.5"
  14. }

vite.config.js
image.png
打包
image.png
引入方式ESM
image.png
开发环境的引入方式ESM
image.png

对比vue-cli快速创建vue3项目(依旧webpack打包)

  1. vue create stu // 选择手动, 选择vue3

虽然vue3已经更新值3.0.5, 但是vue-cli中vue3(preview)创建项目中的vue版本依旧是3.0.0
项目配置信息

  1. // package.json
  2. "scripts": {
  3. "serve": "vue-cli-service serve",
  4. "build": "vue-cli-service build",
  5. "lint": "vue-cli-service lint"
  6. },
  7. "dependencies": {
  8. "core-js": "^3.6.5",
  9. "vue": "^3.0.0",
  10. "vue-router": "^4.0.0-0",
  11. "vuex": "^4.0.0-0"
  12. },
  13. "devDependencies": {
  14. "@vue/cli-plugin-babel": "~4.5.0",
  15. "@vue/cli-plugin-eslint": "~4.5.0",
  16. "@vue/cli-plugin-router": "~4.5.0",
  17. "@vue/cli-plugin-vuex": "~4.5.0",
  18. "@vue/cli-service": "~4.5.0",
  19. "@vue/compiler-sfc": "^3.0.0",
  20. "babel-eslint": "^10.1.0",
  21. "eslint": "^6.7.2",
  22. "eslint-plugin-vue": "^7.0.0-0"
  23. }

打包
image.png
资源引入方式还是bundle方式
image.png

简单例子

起步

  1. yarn init -y
  2. yarn add -D vite
  3. // scripts/package.json
  4. "scripts": {
  5. "dev": "vite"
  6. },

index.html

  1. <h1>hello</h1>
  2. <script type="module">
  3. import '/main.js'
  4. </script>

main.js

  1. document.body.style.color="red";

tailwindcss, postcss插件

起步

  1. yarn add -D autoprefixer postcss tailwindcss
  2. npx tailwind init -P // Created tailwind.config.js + postcss.config.js

配置style.css

  1. @tailwind base;
  2. @tailwind components;
  3. @tailwind utilities;

index.html

  1. <h1 class="3xl">hello</h1>
  2. <link rel="stylesheet" href="/style.css" />

引入scss

  1. yarn add -D sass
  2. import '/style.scss'

style.scss

  1. $color: blue;
  2. h1 {
  3. color: $color;
  4. }

参考:
https://www.bilibili.com/video/BV1kh411Q7WN
https://mp.weixin.qq.com/s/lC6rvEZtjJoBvHR2ZYa6cw