123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import fs from 'node:fs'
- import path from 'node:path'
- import process from 'node:process'
- import { defineConfig, loadEnv } from 'vite'
- import createVitePlugins from './vite/plugins'
- // https://vitejs.dev/config/
- export default ({ mode, command }) => {
- const env = loadEnv(mode, process.cwd())
- // 全局 scss 资源
- const scssResources = []
- fs.readdirSync('src/assets/styles/resources').forEach((dirname) => {
- if (fs.statSync(`src/assets/styles/resources/${dirname}`).isFile()) {
- scssResources.push(`@use "src/assets/styles/resources/${dirname}" as *;`)
- }
- })
- return defineConfig({
- base: './',
- // 开发服务器选项 https://cn.vitejs.dev/config/server-options
- server: {
- open: true,
- port: 9000,
- proxy: {
- '/proxy': {
- target: env.VITE_APP_API_BASE_URL,
- changeOrigin: command === 'serve' && env.VITE_OPEN_PROXY === 'true',
- rewrite: path => path.replace(/\/proxy/, ''),
- },
- },
- },
- // 构建选项 https://cn.vitejs.dev/config/build-options
- build: {
- outDir: mode === 'production' ? 'dist' : `dist-${mode}`,
- sourcemap: env.VITE_BUILD_SOURCEMAP === 'true',
- },
- plugins: createVitePlugins(env, command === 'build'),
- resolve: {
- alias: {
- '@': path.resolve(__dirname, 'src'),
- '#': path.resolve(__dirname, 'src/types'),
- },
- },
- css: {
- preprocessorOptions: {
- scss: {
- additionalData: scssResources.join(''),
- },
- },
- },
- })
- }
|