• vite vue3 config配置篇


    关于vite初始化项目参考自 https://vitejs.cn/
    配置基于自生产上线项目所使用配置

    vite.config.js

    导入模块内容

    import { defineConfig } from 'vite' #vite配置
    import vue from '@vitejs/plugin-vue' #vue
    import vueJsx from '@vitejs/plugin-vue-jsx' #支持jsx
    import viteSvgIcons from 'vite-plugin-svg-icons' # svg
    import { resolve } from "path"; # 引用项目地址
    import OptimizationPersist from 'vite-plugin-optimize-persist' 
    import PkgConfig from 'vite-plugin-package-config' #解决加载缓慢自动填充package.json
    import legacy from '@vitejs/plugin-legacy' #浏览器兼容
    import importToCDN from 'vite-plugin-cdn-import' # node_modules内容走CDN配置 缓解打包过大加载慢(服务器带宽不够)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    配置alias

    // search path
    const pathResolve = (dir) => {
      return resolve(__dirname, ".", dir);
    };
    
    //set alias
    const alias = [
      {
        find: '@',
        replacement: pathResolve("src")
      },
      {
        find: '~',
        replacement: pathResolve("./")
      },
      {
        find: "@build",
        replacement: pathResolve("build")}
    ];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    defineConfig server

    server:{
        https: false, //本地环境不用https
        hrm: true,
        port: 8080, //本地端口
        host: '0.0.0.0', 
        proxy: { //反向代理配置
          '/api': {
            target: 'http://localhost:8090/',
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/api/, '')
          }
        },
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    defineConfig plugins

    添加项目中引用添加svg项 浏览器兼容问题 以及所需框架使用cdn加速(因服务器带宽有限不想打包时过大加载过慢问题)

    plugins: [
        vue(),
        vueJsx(),
    // 配置svg项 监听项目路径地址 并做icon替换
        viteSvgIcons({
          iconDirs: [resolve(process.cwd(), 'src/icons/svg')],
          symbol: 'icon-[dir]-[name]'
        }),
        PkgConfig(),
        OptimizationPersist(),
        legacy({
          targets: ['ie >= 11'],
          additionalLegacyPolyfills: ['regenerator-runtime/runtime']
        }),
        importToCDN({
          modules: [
            {
              name:'vue',
              var:'Vue',
              path:'https://npm.elemecdn.com/vue@3.2.25/dist/vue.global.prod.js'
            },
            {
              name:'vuex',
              var:'Vuex',
              path:'https://npm.elemecdn.com/vuex@4.0.2/dist/vuex.global.prod.js'
            },
            {
              name:'vue-router',
              var:'VueRouter',
              path:'https://npm.elemecdn.com/vue-router@4.0.12/dist/vue-router.global.prod.js'
            },
            {
              name: 'element-plus',
              var: 'ElementPlus',
              path: `https://npm.elemecdn.com/element-plus@2.1.8/dist/index.full.min.js`,
              css: 'https://npm.elemecdn.com/element-plus@2.1.8/dist/index.css',
            },
            {
              name: 'nprogress',
              var: 'NProgress',
              path: `https://npm.elemecdn.com/nprogress@0.2.0/nprogress.js`,
              css: 'https://npm.elemecdn.com/nprogress@0.2.0/nprogress.css',
            },
            {
              name: 'echarts',
              var: 'echarts',
              path: `https://npm.elemecdn.com/echarts@5.3.3/dist/echarts.min.js`
            }       
          ]
        })
      ],
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    defineConfig resolve

    该内容配置@ 及~ 因为习惯于vue-cli 2.x时候import习惯@找文件并且不添加后缀 直接在配置项内添加后缀为.vue,.js以及.json文件

    resolve: {
        alias,
        extensions: ['.vue', '.js', '.json']
      },
    
    • 1
    • 2
    • 3
    • 4

    defineConfig build

    build: {
        target: 'es2020',
        cssCodeSplit: false,
        minify: 'terser', // 混淆器,terser构建后文件体积更小
        sourcemap: false,
        terserOptions: {
          compress: {
            drop_console: true, // 生产环境移除console
            drop_debugger: true // 生产环境移除debugger
          }
        },
        rollupOptions: {
          treeshake: false,
          output: {
            manualChunks (id) {
              if (id.includes('node_modules')) {
                return id.toString().split('node_modules/')[1].split('/')[0].toString()
              }
            }
          }
        }
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    完整配置内容

    import { defineConfig } from 'vite'
    import vue from '@vitejs/plugin-vue'
    import vueJsx from '@vitejs/plugin-vue-jsx'
    import viteSvgIcons from 'vite-plugin-svg-icons'
    import { resolve } from "path";
    import OptimizationPersist from 'vite-plugin-optimize-persist'
    import PkgConfig from 'vite-plugin-package-config'
    import legacy from '@vitejs/plugin-legacy'
    import importToCDN from 'vite-plugin-cdn-import'
    // search path
    const pathResolve = (dir) => {
      return resolve(__dirname, ".", dir);
    };
    
    //set alias
    const alias = [
      {
        find: '@',
        replacement: pathResolve("src")
      },
      {
        find: '~',
        replacement: pathResolve("./")
      },
      {
        find: "@build",
        replacement: pathResolve("build")}
    ];
    
    
    // https://vitejs.dev/config/
    export default defineConfig({
      base: '/',
      server:{
        https: false,
        hrm: true,
        port: 8080,
        host: '0.0.0.0',
        proxy: {
          '/api': {
            target: 'http://localhost:8090/',
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/api/, '')
          }
        },
      },
      plugins: [
        vue(),
        vueJsx(),
        viteSvgIcons({
          iconDirs: [resolve(process.cwd(), 'src/icons/svg')],
          symbol: 'icon-[dir]-[name]'
        }),
        PkgConfig(),
        OptimizationPersist(),
        legacy({
          targets: ['ie >= 11'],
          additionalLegacyPolyfills: ['regenerator-runtime/runtime']
        }),
        importToCDN({
          modules: [
            {
              name:'vue',
              var:'Vue',
              path:'https://npm.elemecdn.com/vue@3.2.25/dist/vue.global.prod.js'
            },
            {
              name:'vuex',
              var:'Vuex',
              path:'https://npm.elemecdn.com/vuex@4.0.2/dist/vuex.global.prod.js'
            },
            {
              name:'vue-router',
              var:'VueRouter',
              path:'https://npm.elemecdn.com/vue-router@4.0.12/dist/vue-router.global.prod.js'
            },
            {
              name: 'element-plus',
              var: 'ElementPlus',
              path: `https://npm.elemecdn.com/element-plus@2.1.8/dist/index.full.min.js`,
              css: 'https://npm.elemecdn.com/element-plus@2.1.8/dist/index.css',
            },
            {
              name: 'nprogress',
              var: 'NProgress',
              path: `https://npm.elemecdn.com/nprogress@0.2.0/nprogress.js`,
              css: 'https://npm.elemecdn.com/nprogress@0.2.0/nprogress.css',
            },
            {
              name: 'echarts',
              var: 'echarts',
              path: `https://npm.elemecdn.com/echarts@5.3.3/dist/echarts.min.js`
            }       
          ]
        })
      ],
      resolve: {
        alias,
        extensions: ['.vue', '.js', '.json']
      },
      css: {
        postcss: {
          plugins: [
            {
              postcssPlugin: 'internal:charset-removal',
              AtRule: {
                charset: (atRule) => {
                  if (atRule.name === 'charset') {
                    atRule.remove();
                  }
                }
              }
            }
          ],
        },
      },
      build: {
        target: 'es2020',
        cssCodeSplit: false,
        minify: 'terser', // 混淆器,terser构建后文件体积更小
        sourcemap: false,
        terserOptions: {
          compress: {
            drop_console: true, // 生产环境移除console
            drop_debugger: true // 生产环境移除debugger
          }
        },
        rollupOptions: {
          treeshake: false,
          output: {
            manualChunks (id) {
              if (id.includes('node_modules')) {
                return id.toString().split('node_modules/')[1].split('/')[0].toString()
              }
            }
          }
        }
      },
    })
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
  • 相关阅读:
    MySQL 高级语句 Part1(进阶查询语句+MySQL数据库函数+连接查询)
    【力扣2656】K个元素的最大和
    基于水循环优化的BP神经网络(分类应用) - 附代码
    淘宝API商品详情接口,通过商品ID获取商品名称,淘宝主图,价格,颜色规格尺寸,库存,SKU等调用演示案例
    C++复合类型总结,看这一篇就够了
    数组转树形结构
    中国优质稻种 国稻种芯-丰收节贸促会:老挝拟设立推广中心
    C/C++ 泛型模板约束
    通过深度可分离卷积神经网络对七种表情进行区分
    信号量与自旋锁的简单介绍
  • 原文地址:https://blog.csdn.net/Wx_H_/article/details/126177904