2024-08-19 17:52:52 +08:00

90 lines
2.6 KiB
TypeScript

/**
* vite 配置
* @see https://cn.vitejs.dev/config/
* @type {import('vite').UserConfig}
*/
import { resolve } from 'node:path';
import process from 'node:process';
import type { UserConfig } from 'vite';
import { defineConfig, loadEnv } from 'vite';
import TransformPages from 'uni-read-pages-vite';
import postcssPlugins from './postcss.config';
import { createVitePlugins } from './build/vitePlugins';
import { currentPlatform } from './build/platform';
export default defineConfig(async ({ mode }) => {
const root = process.cwd();
const env = loadEnv(mode, resolve(root, 'env'));
const isProd = mode === 'production';
const { VITE_PROXY_PREFIX, VITE_UPLOAD_PROXY_PREFIX, VITE_BASE_URL, VITE_UPLOAD_URL, VITE_PORT } = env;
return {
base: './',
envDir: './env', // 自定义env目录
// 设置路径别名
resolve: {
alias: {
'@': resolve('./src'),
},
extensions: ['.js', '.ts'], // 使用路径别名时想要省略的后缀名,可以自己 增减
},
// 自定义全局变量
define: {
'process.env': {},
'PLATFORM': JSON.stringify(currentPlatform),
'ROUTES': new TransformPages().routes,
},
css: {
preprocessorOptions: {
scss: {
additionalData: '@import "./src/uni.scss";',
},
},
postcss: {
plugins: postcssPlugins,
},
},
plugins: createVitePlugins({ isProd }),
// 开发服务器配置
server: {
host: true,
// open: true,
port: Number.parseInt(VITE_PORT!, 10),
proxy: {
[VITE_PROXY_PREFIX!]: {
target: VITE_BASE_URL,
changeOrigin: true,
rewrite: (path: string) => path.replace(new RegExp(`^${VITE_PROXY_PREFIX}`), ''),
},
[VITE_UPLOAD_PROXY_PREFIX!]: {
target: VITE_UPLOAD_URL,
changeOrigin: true,
rewrite: (path: string) => path.replace(new RegExp(`^${VITE_UPLOAD_PROXY_PREFIX}`), ''),
},
},
},
// 构建配置
build: {
outDir: 'dist',
chunkSizeWarningLimit: 1500,
sourcemap: !isProd,
target: 'es6',
minify: isProd ? 'terser' : false,
terserOptions: {
compress: {
drop_console: isProd,
drop_debugger: true,
},
},
rollupOptions: {
output: {
entryFileNames: `assets/[name].${new Date().getTime()}.js`,
chunkFileNames: `assets/[name].${new Date().getTime()}.js`,
assetFileNames: `assets/[name].${new Date().getTime()}.[ext]`,
compact: true,
},
},
},
} as UserConfig;
});