• Vue3+vite打包配置及部分打包优化~


    这里介绍的是vite项目的打包配置,若想了解webpack打包配置可看我的其他博客。(下面来介绍下vite打包配置的步骤)

    1、步骤一:配置base。(为什么需要配置base?这里配置base主要是修改根路径,一般我们在开发环境中引用静态资源可能使用的是绝对路径,但是一旦打包部署到服务器上后可能会报404,无法正确的获取的资源。)

    1. //在vite.config.ts中
    2. import { defineConfig } from 'vite'
    3. export default defineConfig({
    4. //配置根路径,解决部署到服务器之后绝对路径会报404问题,所以需改为相对路径
    5. base:"./",
    6. })

    2、步骤二:可根据需要配置组件及静态资源路径别名(若配置了路径别名使用方法和typescript的类型别名一样,直接替换原路径即可)

    1. //在vite.config.ts中
    2. import { fileURLToPath, URL } from 'node:url'
    3. import { defineConfig } from 'vite'
    4. //配置组件路径别名需引入resolve
    5. import{resolve} from 'path'
    6. export default defineConfig({
    7. //配置根路径,解决部署到服务器之后绝对路径会报404问题,所以需改为相对路径
    8. base:"./",
    9. // 路径别名
    10. resolve: {
    11. alias: {
    12. '@': fileURLToPath(new URL('./src', import.meta.url)),
    13. // 组件路径别名,语法:别名:resolve(__dirname,'组件原路径')
    14. com:resolve(__dirname,'src/component'),
    15. //静态资源路径别名
    16. './image':'src/assets'
    17. }
    18. }
    19. })

    3、步骤三:配置生产环境移除console.log。

    1. //在vite.config.ts中
    2. import { fileURLToPath, URL } from 'node:url'
    3. import { defineConfig } from 'vite'
    4. import{resolve} from 'path'
    5. export default defineConfig({
    6. //配置根路径,解决部署到服务器之后绝对路径会报404问题,所以需改为相对路径
    7. base:"./",
    8. // 路径别名
    9. resolve: {
    10. alias: {
    11. '@': fileURLToPath(new URL('./src', import.meta.url)),
    12. // 组件路径别名,语法:别名:resolve(__dirname,'组件原路径')
    13. com:resolve(__dirname,'src/component'),
    14. //静态资源路径别名
    15. './image':'src/assets'
    16. }
    17. },
    18. // 生产环境移除console.log的配置
    19. build:{
    20. // 默认是esbuild,但这里需要改成terser,并且想使用terser的话,需提前安装,命令为npm add -D
    21. //terser
    22. minify:"terser",
    23. terserOptions: {
    24. compress: {
    25. //生产环境时移除console
    26. drop_console: true,
    27. drop_debugger: true,
    28. },
    29. },
    30. }
    31. })

    4、步骤四:设置代理解决跨域。(使用方法与webpack解决跨域一样)

    1. //在vite.config.ts中
    2. import { fileURLToPath, URL } from 'node:url'
    3. import { defineConfig } from 'vite'
    4. import{resolve} from 'path'
    5. export default defineConfig({
    6. //配置根路径,解决部署到服务器之后绝对路径会报404问题,所以需改为相对路径
    7. base:"./",
    8. server: {
    9. proxy: {
    10. // 字符串简写写法,其他方法可看vite官网,使用方法与webpack解决跨域一样
    11. '/foo': 'baseURL地址',
    12. }
    13. },
    14. // 路径别名
    15. resolve: {
    16. alias: {
    17. '@': fileURLToPath(new URL('./src', import.meta.url)),
    18. // 组件路径别名,语法:别名:resolve(__dirname,'组件原路径')
    19. com:resolve(__dirname,'src/component'),
    20. //静态资源路径别名
    21. './image':'src/assets'
    22. }
    23. },
    24. // 生产环境移除console.log的配置
    25. build:{
    26. // 默认是esbuild,但这里需要改成terser,并且想使用terser的话,需提前安装,命令为npm add -D terser
    27. minify:'terser',
    28. terserOptions: {
    29. compress: {
    30. //生产环境时移除console
    31. drop_console: true,
    32. drop_debugger: true,
    33. },
    34. },
    35. }
    36. })

    5、打包优化,使用CDN分发。(忽略,后面更新)

    1. //安装cdn插件
    2. npm install vite-plugin-cdn-import --save-dev
    1. //引入插件,在vite.config.ts中
    2. //import importToCDN from "vite-plugin-cdn-import"
    3. //或
    4. import { Plugin as importToCDN } from "vite-plugin-cdn-import"

    6、代码压缩。

    npm i vite-plugin-compression -D
    1. //在vite.config.ts中
    2. import viteCompression from 'vite-plugin-compression'
    3. //在plugins中配置即可
    4. plugins: [
    5. viteCompression(),
    6. ]

    7、图片压缩。

    npm i vite-plugin-imagemin -D
    1. //在vite.config.ts中
    2. import viteImagemin from 'vite-plugin-imagemin'
    3. //在plugins下写
    4. viteImagemin({
    5. gifsicle: {
    6. optimizationLevel: 7,
    7. interlaced: false
    8. },
    9. optipng: {
    10. optimizationLevel: 7
    11. },
    12. mozjpeg: {
    13. quality: 20
    14. },
    15. pngquant: {
    16. quality: [0.8, 0.9],
    17. speed: 4
    18. },
    19. svgo: {
    20. plugins: [
    21. {
    22. name: 'removeViewBox'
    23. },
    24. {
    25. name: 'removeEmptyAttrs',
    26. active: false
    27. }
    28. ]
    29. }
    30. })

    8、打包命令配置。

    1. //在package.json中
    2. "build": "vite build",

  • 相关阅读:
    docker构建FreeSWITCH编译环境及打包
    冥想第九百五十一天
    ubuntu/linux系统知识(30)ubuntu系统配置项dconf/gsettings
    从可逆计算看DSL的设计要点
    Mysql——查询sql语句练习
    关于MySQL8.0移除PASSWORD()函数
    【uniapp】六格验证码输入框实现
    【吞噬星空】罗峰成功抵达虬龙星,宇宙超级富二代登场,不容错过
    【Java八股文总结】之Java设计模式
    机器学习深度解析:原理、应用与前景
  • 原文地址:https://blog.csdn.net/m0_62626838/article/details/141070691