vite.config.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import {dirname, relative, resolve} from 'path'
  2. import type {ConfigEnv, UserConfig} from 'vite'
  3. import {loadEnv, normalizePath} from 'vite'
  4. import {createVitePlugins} from './build/vite'
  5. import {exclude, include} from "./build/vite/optimize"
  6. // 当前执行node命令时文件夹的地址(工作目录)
  7. const root = process.cwd()
  8. // 路径查找
  9. function pathResolve(dir: string) {
  10. return resolve(root, '.', dir)
  11. }
  12. function getRelativeScssUsePath(filename: string, targetPath: string) {
  13. const cleanFilename = filename.split('?')[0]
  14. const relativePath = normalizePath(relative(dirname(cleanFilename), targetPath))
  15. return relativePath.startsWith('.') ? relativePath : `./${relativePath}`
  16. }
  17. // https://vitejs.dev/config/
  18. export default ({command, mode}: ConfigEnv): UserConfig => {
  19. let env = {} as any
  20. const isBuild = command === 'build'
  21. if (!isBuild) {
  22. env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), root)
  23. } else {
  24. env = loadEnv(mode, root)
  25. }
  26. const variablesScssPath = pathResolve('src/styles/variables.scss')
  27. return {
  28. base: env.VITE_BASE_PATH,
  29. root: root,
  30. // 服务端渲染
  31. server: {
  32. port: env.VITE_PORT, // 端口号
  33. host: "0.0.0.0",
  34. open: env.VITE_OPEN === 'true',
  35. // 本地跨域代理. 目前注释的原因:暂时没有用途,server 端已经支持跨域
  36. // proxy: {
  37. // ['/admin-api']: {
  38. // target: env.VITE_BASE_URL,
  39. // ws: false,
  40. // changeOrigin: true,
  41. // rewrite: (path) => path.replace(new RegExp(`^/admin-api`), ''),
  42. // },
  43. // },
  44. },
  45. // 项目使用的vite插件。 单独提取到build/vite/plugin中管理
  46. plugins: createVitePlugins(isBuild, env),
  47. css: {
  48. lightningcss: {
  49. // Preserve legacy star-hack declarations by stripping invalid syntax during minification.
  50. errorRecovery: true
  51. },
  52. preprocessorOptions: {
  53. scss: {
  54. additionalData: (source: string, filename: string) => {
  55. const normalizedFilename = normalizePath(filename)
  56. // Windows 下更容易触发重复注入:定义或显式转导变量的文件,不能再次注入同一个
  57. // `@use ... as *`,否则 Sass 会报 duplicate global variables。
  58. if (
  59. normalizedFilename.endsWith('/src/styles/variables.scss') ||
  60. normalizedFilename.endsWith('/src/styles/global.module.scss')
  61. ) {
  62. return source
  63. }
  64. return `@use "${getRelativeScssUsePath(filename, variablesScssPath)}" as *;\n${source}`
  65. },
  66. api: 'modern-compiler'
  67. }
  68. }
  69. },
  70. resolve: {
  71. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css'],
  72. alias: [
  73. {
  74. find: /\@\//,
  75. replacement: `${pathResolve('src')}/`
  76. }
  77. ]
  78. },
  79. build: {
  80. chunkSizeWarningLimit: 2000,
  81. minify: 'oxc',
  82. outDir: env.VITE_OUT_DIR || 'dist',
  83. reportCompressedSize: false,
  84. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  85. rollupOptions: {
  86. output: {
  87. minify: {
  88. compress: {
  89. dropDebugger: env.VITE_DROP_DEBUGGER === 'true',
  90. dropConsole: env.VITE_DROP_CONSOLE === 'true'
  91. }
  92. },
  93. codeSplitting: {
  94. groups: [
  95. { name: 'echarts', test: /node_modules[\\/]echarts[\\/]/ }, // 将 echarts 单独打包,参考 https://gitee.com/yudaocode/yudao-ui-admin-vue3/issues/IAB1SX 讨论
  96. { name: 'form-create', test: /node_modules[\\/]@form-create[\\/]element-ui[\\/]/ }, // 参考 https://github.com/yudaocode/yudao-ui-admin-vue3/issues/148 讨论
  97. { name: 'form-designer', test: /node_modules[\\/]@form-create[\\/]designer[\\/]/ }
  98. ]
  99. }
  100. },
  101. },
  102. },
  103. optimizeDeps: {include, exclude}
  104. }
  105. }