import type { ConfigEnv, UserConfig } from 'vite';
import path from 'node:path';
import inject from '@rollup/plugin-inject';
import legacy from '@vitejs/plugin-legacy';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import SetSourceMapFiletToPathPlugin from 'sourcemap-set-path-plugin';
import { VantResolver } from 'unplugin-vue-components/resolvers';
import Components from 'unplugin-vue-components/vite';
import { defineConfig } from 'vite';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import autoprefixer from 'autoprefixer';
import autoImport from 'unplugin-auto-import/vite';
// 常量定义
const ENV_TARGETS = {
rd: 'https://kaoshi-rd.weizhipin.com',
qa: 'https://kaoshi-qa.weizhipin.com',
pre: 'https://pre-kaoshi.zhipin.com',
} as const;
const SOURCE_DIR = path.resolve(__dirname, './dist/static/js/');
const TARGET_DIR = path.resolve(__dirname, './dist/static/map/');
const DEPLOY_VERSION = process.env.VITE_DEPLOY_VERSION;
const SVG_ICON_DIR = path.resolve(process.cwd(), './src/assets/svg-sprite');
const PROJECT_ROOT = process.cwd();
// CSS 预处理配置
const CSS_PREPROCESSOR_OPTIONS = {
less: {
math: 'always',
additionalData: `
@import "@/styles/base/index.less";
@import "@/styles/base/biz.less";
`,
globalVars: {
'primary-color': '#009999',
},
},
};
// Rollup 输出配置
const ROLLUP_OUTPUT_OPTIONS = {
format: 'esm',
chunkFileNames: 'static/js/[name]-[hash].js',
entryFileNames: 'static/js/[name]-[hash].js',
assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
};
// 插件配置
const getPlugins = () => [
vue(),
vueJsx(),
Components({
resolvers: [VantResolver()],
}),
autoImport({
imports: ['vue', 'vue-router', 'pinia'],
dirs: ['src/components', 'src/utils', 'src/hooks'],
eslintrc: {
enabled: true,
filepath: './.eslintrc-auto-import.json',
globalsPropValue: 'readonly'
},
}),
inject({
Invoke: path.resolve(PROJECT_ROOT, './src/services/invoke.ts'),
postLog: path.resolve(PROJECT_ROOT, 'src/utils/post-log.ts'),
}),
legacy({
targets: ['ie >= 11'],
additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
}),
createSvgIconsPlugin({
iconDirs: [SVG_ICON_DIR],
symbolId: 'icon-[dir]-[name]',
}),
{
...SetSourceMapFiletToPathPlugin({
sourceDir: SOURCE_DIR,
targetDir: TARGET_DIR
}),
enforce: 'post',
apply: 'build',
},
];
// 开发服务器配置
const getServerConfig = (baseUrl: string): UserConfig['server'] => ({
host: true,
open: 'http://localhost:8899?pid=f46c19a3e1e1b0af1nB62w~~',
hmr: true,
port: 8899,
proxy: {
'/mapi': {
target: baseUrl,
changeOrigin: true,
secure: true,
},
'/wapi': {
target: baseUrl,
changeOrigin: true,
secure: true,
},
},
});
// 构建配置
const getBuildConfig = (): UserConfig['build'] => ({
assetsDir: 'static',
outDir: 'dist',
sourcemap: 'hidden',
rollupOptions: {
output: ROLLUP_OUTPUT_OPTIONS,
},
minify: 'terser',
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
},
},
});
export default ({ command, mode }: ConfigEnv) => {
console.log('DEPLOY_ENV:', mode);
console.log('DEPLOY_VERSION:', DEPLOY_VERSION);
const baseUrl = ENV_TARGETS[mode as keyof typeof ENV_TARGETS] || '';
return defineConfig({
root: PROJECT_ROOT,
base: baseUrl,
publicDir: 'public',
define: {
DEPLOY_VERSION: JSON.stringify(DEPLOY_VERSION),
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'~@': path.resolve(__dirname, './src'),
},
extensions: ['.mjs', '.js', 'jsx', '.ts', '.tsx', '.vue', '.json'],
},
css: {
preprocessorOptions: CSS_PREPROCESSOR_OPTIONS,
postcss: {
plugins: [autoprefixer()],
},
},
server: getServerConfig(baseUrl),
build: getBuildConfig(),
plugins: getPlugins(),
});
};