/* eslint-disable no-console */
import type { ConfigEnv } 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'; // 自动导入组件
// Constants
const SOURCE_DIR = path.resolve(__dirname, './dist/static/js/');
const TARGET_DIR = path.resolve(__dirname, './dist/static/map/');
const PROJECT_ROOT = process.cwd();
// Target environments
const TARGETS = {
rd: 'https://kaoshi-rd.weizhipin.com',
qa: 'https://kaoshi-qa.weizhipin.com',
pre: 'https://pre-kaoshi.zhipin.com',
};
// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv) => {
console.log('DEPLOY_ENV:', mode);
console.log('DEPLOY_VERSION:', process.env.VITE_DEPLOY_VERSION);
const BASE_URL = TARGETS[mode as keyof typeof TARGETS];
return defineConfig({
// Project root
root: PROJECT_ROOT,
// Base URL for deployment
base: BASE_URL,
// Static directory
publicDir: 'public',
// Resolve aliases and extensions
resolve: {
alias: {
'@': path.resolve(PROJECT_ROOT, './src'),
'~@': path.resolve(PROJECT_ROOT, './src'),
},
extensions: ['.mjs', '.js', 'jsx', '.ts', 'tsx', '.vue', '.json'],
},
// Define global constants
define: {
DEPLOY_VERSION: process.env.VITE_DEPLOY_VERSION,
},
// CSS preprocessor and PostCSS configuration
css: {
preprocessorOptions: {
less: {
math: 'always',
additionalData: `@import "@/styles/base/index.less";@import "@/styles/base/biz.less";`,
globalVars: {
'primary-color': '#009999',
},
},
},
postcss: {
plugins: [
autoprefixer(),
],
},
},
// Server configuration for development
server: {
host: true,
open: 'http://localhost:8899?pid=f46c19a3e1e1b0af1nB62w~~',
hmr: true,
port: 8899,
proxy: {
'/mapi': {
target: BASE_URL,
changeOrigin: true,
secure: true,
},
'/wapi': {
target: BASE_URL,
changeOrigin: true,
secure: true,
},
},
},
// Build configuration
build: {
assetsDir: 'static',
outDir: 'dist',
sourcemap: 'hidden',
rollupOptions: {
output: {
format: 'esm',
chunkFileNames: 'static/js/[name]-[hash].js',
entryFileNames: 'static/js/[name]-[hash].js',
assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
},
},
minify: 'terser',
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
},
},
},
// Plugins
plugins: [
vue(),
Components({
resolvers: [VantResolver()],
}),
vueJsx(),
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: [path.resolve(PROJECT_ROOT, './src/assets/svg-sprite')],
symbolId: 'icon-[dir]-[name]',
}),
autoImport({
imports: ['vue', 'vue-router', 'pinia'],
dirs: ['src/components', 'src/utils', 'src/hooks'],
eslintrc: { enabled: true, filepath: './.eslintrc-auto-import.json', globalsPropValue: 'readonly' },
}),
{
...SetSourceMapFiletToPathPlugin({ sourceDir: SOURCE_DIR, targetDir: TARGET_DIR }),
enforce: 'post',
apply: 'build',
},
],
});
};