vite.config.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import fs from 'node:fs'
  2. import path from 'node:path'
  3. import process from 'node:process'
  4. import { defineConfig, loadEnv } from 'vite'
  5. import createVitePlugins from './vite/plugins'
  6. // https://vitejs.dev/config/
  7. export default ({ mode, command }) => {
  8. const env = loadEnv(mode, process.cwd())
  9. // 全局 scss 资源
  10. const scssResources = []
  11. fs.readdirSync('src/assets/styles/resources').forEach((dirname) => {
  12. if (fs.statSync(`src/assets/styles/resources/${dirname}`).isFile()) {
  13. scssResources.push(`@use "src/assets/styles/resources/${dirname}" as *;`)
  14. }
  15. })
  16. return defineConfig({
  17. base: './',
  18. // 开发服务器选项 https://cn.vitejs.dev/config/server-options
  19. server: {
  20. open: true,
  21. port: 9000,
  22. proxy: {
  23. '/proxy': {
  24. target: env.VITE_APP_API_BASE_URL,
  25. changeOrigin: command === 'serve' && env.VITE_OPEN_PROXY === 'true',
  26. rewrite: path => path.replace(/\/proxy/, ''),
  27. },
  28. },
  29. },
  30. // 构建选项 https://cn.vitejs.dev/config/build-options
  31. build: {
  32. outDir: mode === 'production' ? 'dist' : `dist-${mode}`,
  33. sourcemap: env.VITE_BUILD_SOURCEMAP === 'true',
  34. },
  35. plugins: createVitePlugins(env, command === 'build'),
  36. resolve: {
  37. alias: {
  38. '@': path.resolve(__dirname, 'src'),
  39. '#': path.resolve(__dirname, 'src/types'),
  40. },
  41. },
  42. css: {
  43. preprocessorOptions: {
  44. scss: {
  45. additionalData: scssResources.join(''),
  46. },
  47. },
  48. },
  49. })
  50. }