80 lines
2.2 KiB
TypeScript
80 lines
2.2 KiB
TypeScript
import { defineConfig, loadEnv } from 'vite';
|
|
import vue from '@vitejs/plugin-vue';
|
|
import vueJsx from '@vitejs/plugin-vue-jsx';
|
|
import removeConsole from 'vite-plugin-remove-console';
|
|
import strip from '@rollup/plugin-strip';
|
|
import { resolve } from 'path';
|
|
|
|
// https://vitejs.dev/config/
|
|
export default defineConfig(({ mode }) => {
|
|
const env = loadEnv(mode, process.cwd(), '');
|
|
const isProd = mode === 'production';
|
|
|
|
return {
|
|
base: env.VITE_BASE_URL || '/',
|
|
plugins: [
|
|
vue(),
|
|
vueJsx(),
|
|
// 生产构建:移除 console.log / console.warn / console.error
|
|
isProd && removeConsole(),
|
|
// 生产构建:移除代码中的注释
|
|
isProd &&
|
|
strip({
|
|
include: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx'],
|
|
}),
|
|
].filter(Boolean),
|
|
resolve: {
|
|
alias: {
|
|
'@': resolve(__dirname, 'src'),
|
|
},
|
|
},
|
|
css: {
|
|
modules: {
|
|
localsConvention: 'camelCaseOnly',
|
|
},
|
|
preprocessorOptions: {
|
|
less: {
|
|
javascriptEnabled: true,
|
|
},
|
|
},
|
|
},
|
|
server: {
|
|
host: '0.0.0.0',
|
|
port: 3000,
|
|
strictPort: false,
|
|
open: true,
|
|
proxy: {
|
|
'/api': {
|
|
target: env.VITE_API_BASE_URL || 'http://localhost:3000',
|
|
changeOrigin: true,
|
|
rewrite: (path) => path.replace(/^\/api/, ''),
|
|
},
|
|
},
|
|
},
|
|
build: {
|
|
outDir: 'dist',
|
|
sourcemap: mode !== 'production',
|
|
// 生产构建移除 console 和注释(esbuild minify 阶段)
|
|
minify: isProd ? 'esbuild' : false,
|
|
chunkSizeWarningLimit: 1500,
|
|
rollupOptions: {
|
|
output: {
|
|
// 更细粒度的分包,确保按需加载
|
|
manualChunks(id) {
|
|
if (id.includes('node_modules')) {
|
|
if (id.includes('ant-design-vue') || id.includes('@ant-design')) {
|
|
return 'antd';
|
|
}
|
|
if (id.includes('vue') || id.includes('@vue') || id.includes('vue-router')) {
|
|
return 'vue-vendor';
|
|
}
|
|
// 其他第三方依赖单独打包,未使用的不会进入
|
|
return 'vendor';
|
|
}
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
});
|