import { defineConfig, loadEnv, UserConfigExport } from 'vite' import vueJsx from '@vitejs/plugin-vue-jsx' import vue from '@vitejs/plugin-vue' import * as path from "path"; import { resolve } from 'path'; import Components from 'unplugin-vue-components/vite'; import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'; import { visualizer } from 'rollup-plugin-visualizer' // @ts-ignore import { name, version, description, appName } from "./package.json"; import dayjs from "dayjs"; const __APP_INFO__ = { name: name, appName: appName, version: version, description: description, lastBuildTime: dayjs().format("YYYY-MM-DD HH:mm:ss"), } const pathSrc = path.resolve(__dirname, 'src'); // https://vitejs.dev/config/ export default ({ mode }): UserConfigExport => { const env: Record = loadEnv(mode, process.cwd(), '') return defineConfig({ define: { __APP_ENV: JSON.stringify(env), __APP_INFO: __APP_INFO__, // $: 'jquery', // jQuery: 'jquery' }, base: './', plugins: [ vue(), vueJsx(), Components({ resolvers: [AntDesignVueResolver({ importStyle: false })] }), visualizer({ emitFile: false, filename: 'analyze.html', open: false }) ], server: { host: '0.0.0.0', port: env['VITE_APP_PORT'] as unknown as number, open: false, proxy: { [env["VITE_APP_BASE_API"]]: { target: env["VITE_APP_PROXY_URL"], changeOrigin: true, secure: false, rewrite: path => path.replace(RegExp(`^${env['VITE_APP_BASE_API']}`), '') }, [env["VITE_APP_WS_API"]]: { target: env["VITE_APP_WS_PROXY_URL"], ws: true, changeOrigin: true, rewrite: path => path.replace(RegExp(`^${env['VITE_APP_WS_API']}`), ''), } }, }, build: { outDir: 'dist', target: 'modules', chunkSizeWarningLimit: 1500, minify: 'terser', terserOptions: { compress: { //生产环境时移除console // drop_console: env['VITE_DROP_CONSOLE'] as unknown as boolean, // drop_debugger: env['VITE_DROP_CONSOLE'] as unknown as boolean, }, format: { //删除注释 comments: false } }, rollupOptions: { output: { manualChunks(id) { if (id.includes('node_modules')) { return id .toString() .split('node_modules/')[1] .split('/')[0] .toString(); } }, chunkFileNames(chunkInfo) { const facadeModuleId = chunkInfo.facadeModuleId ? chunkInfo.facadeModuleId.split('/') : []; const fileName = facadeModuleId[facadeModuleId.length - 2] || '[name]'; return `js/${fileName}/[name].[hash].js`; } } } }, resolve: { alias: { '@': pathSrc, } }, css: { preprocessorOptions: { scss: { charset: false, // 全局css变量 additionalData: ` @import "@/assets/scss/variable.scss"; @import "@/assets/scss/mixin.scss"; ` } } } }) }