vite.config.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import fs from 'node:fs'
  2. import path from 'node:path'
  3. import process from 'node:process'
  4. import dayjs from 'dayjs'
  5. import { defineConfig, loadEnv } from 'vite'
  6. import pkg from './package.json'
  7. import createVitePlugins from './vite/plugins'
  8. // https://vitejs.dev/config/
  9. export default defineConfig(({ mode, command }) => {
  10. const env = loadEnv(mode, process.cwd())
  11. // 全局 scss 资源
  12. const scssResources: string[] = []
  13. fs.readdirSync('src/assets/styles/resources').forEach((dirname) => {
  14. if (fs.statSync(`src/assets/styles/resources/${dirname}`).isFile()) {
  15. scssResources.push(`@use "/src/assets/styles/resources/${dirname}" as *;`)
  16. }
  17. })
  18. return {
  19. // 静态资源基础路径
  20. base: env.VITE_BASE_PATH || '/',
  21. // 开发服务器选项 https://cn.vitejs.dev/config/server-options
  22. server: {
  23. open: true,
  24. host: '0.0.0.0', // 监听所有接口
  25. port: 9000,
  26. proxy: {
  27. '/proxy': {
  28. target: env.VITE_APP_API_BASEURL,
  29. changeOrigin: command === 'serve' && env.VITE_OPEN_PROXY === 'true',
  30. rewrite: path => path.replace(/\/proxy/, ''),
  31. },
  32. },
  33. },
  34. // 构建选项 https://cn.vitejs.dev/config/build-options
  35. build: {
  36. outDir: mode === 'production' ? 'dist' : `dist-${mode}`,
  37. sourcemap: env.VITE_BUILD_SOURCEMAP === 'true',
  38. },
  39. define: {
  40. __SYSTEM_INFO__: JSON.stringify({
  41. pkg: {
  42. version: pkg.version,
  43. dependencies: pkg.dependencies,
  44. devDependencies: pkg.devDependencies,
  45. },
  46. lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  47. }),
  48. },
  49. plugins: createVitePlugins(mode, command === 'build'),
  50. resolve: {
  51. alias: {
  52. '@': path.resolve(__dirname, 'src'),
  53. '#': path.resolve(__dirname, 'src/types'),
  54. },
  55. },
  56. css: {
  57. preprocessorOptions: {
  58. scss: {
  59. api: 'modern-compiler',
  60. additionalData: scssResources.join(''),
  61. },
  62. },
  63. },
  64. }
  65. })