import { UserConfig, ConfigEnv, loadEnv } from 'vite'; import vue from '@vitejs/plugin-vue'; import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'; import path from 'path'; export default ({ mode }: ConfigEnv): UserConfig => { // 获取 .env 环境配置文件 const env = loadEnv(mode, process.cwd()); return { base: "./", plugins: [ vue(), createSvgIconsPlugin({ // 指定需要缓存的图标文件夹 iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')], // 指定symbolId格式 symbolId: 'icon-[dir]-[name]' }), ], // 本地反向代理解决浏览器跨域限制 server: { host: '0.0.0.0', port: Number(env.VITE_APP_PORT), open: true, // 运行自动打开浏览器 proxy: { [env.VITE_APP_BASE_API]: { // 线上API地址 target: 'http://192.168.1.20:8070/', // 本地API地址 // target: 'http://192.168.1.152:8080/riis-system', changeOrigin: true, rewrite: path => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '') } } }, resolve: { // Vite路径别名配置 alias: { '@': path.resolve('./src'), 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js' } }, build: { sourcemap: false, // 不生成 source map chunkSizeWarningLimit: 1500, terserOptions: { compress: { // 打包时清除 console 和 debug 相关代码 drop_console: true, drop_debugger: true, }, }, rollupOptions: { output: { assetFileNames: (assetInfo:any) => { var info = assetInfo.name.split('.') var extType = info[info.length - 1] if ( /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/i.test(assetInfo.name) ) { extType = 'media' } else if (/\.(png|jpe?g|gif|svg)(\?.*)?$/.test(assetInfo.name)) { extType = 'img' } else if (/\.(woff2?|eot|ttf|otf)(\?.*)?$/i.test(assetInfo.name)) { extType = 'fonts' } return `${extType}/[name]-[hash][extname]` }, chunkFileNames: 'js/[name]-[hash].js', entryFileNames: 'js/[name]-[hash].js' // // 最小化拆分包 // manualChunks(id) { // if (id.includes("node_modules")) { // return id.toString().split("node_modules/")[1].split("/")[0].toString() // } // }, // // 用于从入口点创建的块的打包输出格式[name]表示文件名,[hash]表示该文件内容hash值 // entryFileNames: 'js/[name].[hash].js', // // 用于命名代码拆分时创建的共享块的输出命名 // //   chunkFileNames: 'js/[name].[hash].js', // // 用于输出静态资源的命名,[ext]表示文件扩展名 // assetFileNames: '[ext]/[name].[hash].[ext]', // // 拆分js到模块文件夹 // chunkFileNames: (chunkInfo) => { // const facadeModuleId = chunkInfo.facadeModuleId ? chunkInfo.facadeModuleId.split('/') : []; // const fileName = facadeModuleId[facadeModuleId.length - 2] || '[name]'; // return `js/${fileName}/[name].[hash].js`; // }, } } } }; };