vite.config.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import autoprefixer from 'autoprefixer'
  2. import dayjs from 'dayjs'
  3. import { resolve } from 'node:path'
  4. import type { ConfigEnv, UserConfig } from 'vite'
  5. import { defineConfig, loadEnv } from 'vite'
  6. import {
  7. assetsDir,
  8. base,
  9. chunkSizeWarningLimit,
  10. cssCodeSplit,
  11. exclude,
  12. https,
  13. include,
  14. minify,
  15. open,
  16. outDir,
  17. outputHash,
  18. port,
  19. reportCompressedSize,
  20. } from '/@/config'
  21. import { createVitePlugin, createWatch } from '/@vab/build'
  22. const lastBuildTime = dayjs().format('YYYY-MM-DD HH:mm:ss')
  23. export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  24. process.env['VITE_APP_UPDATE_TIME'] = lastBuildTime
  25. process.env['VITE_USER_NODE_ENV'] = mode
  26. const root = process.cwd()
  27. const env = loadEnv(mode, root)
  28. createWatch(env)
  29. console.log(lastBuildTime)
  30. return {
  31. base,
  32. root,
  33. server: {
  34. open,
  35. port,
  36. hmr: {
  37. overlay: true,
  38. },
  39. host: '0.0.0.0',
  40. warmup: {
  41. clientFiles: ['./index.html', './library/{components,layouts}/*', './src/{views,plugins}/*'],
  42. },
  43. https,
  44. fs: {
  45. cachedChecks: true,
  46. },
  47. },
  48. resolve: {
  49. alias: {
  50. '~/': `${resolve(__dirname, '.')}/`,
  51. '/@/': `/${resolve(__dirname, 'src')}/`,
  52. '/@vab/': `/${resolve(__dirname, 'library')}/`,
  53. },
  54. },
  55. optimizeDeps: {
  56. include,
  57. exclude,
  58. },
  59. build: {
  60. assetsDir,
  61. chunkSizeWarningLimit,
  62. cssCodeSplit,
  63. outDir,
  64. reportCompressedSize,
  65. rollupOptions: {
  66. onwarn: () => {
  67. return
  68. },
  69. output: {
  70. chunkFileNames: outputHash ? 'static/js/[name]-[hash].js' : 'static/js/[name].js',
  71. entryFileNames: outputHash ? 'static/js/[name]-[hash].js' : 'static/js/[name].js',
  72. assetFileNames: outputHash ? 'static/[ext]/[name]-[hash].[ext]' : 'static/[ext]/[name].[ext]',
  73. manualChunks: {
  74. 'vsv-element-plus': ['element-plus'],
  75. 'vsv-nprogress': ['nprogress'],
  76. 'vsv-icon': ['vsv-icon'],
  77. 'vsv-echarts': ['echarts'],
  78. },
  79. },
  80. },
  81. minify,
  82. sourcemap: false,
  83. },
  84. css: {
  85. postcss: {
  86. plugins: [
  87. autoprefixer({ grid: true }) as any,
  88. {
  89. postcssPlugin: 'internal:charset-removal',
  90. AtRule: {
  91. charset: (atRule: { name: string; remove: () => void }) => {
  92. if (atRule.name === 'charset') atRule.remove()
  93. },
  94. },
  95. },
  96. ],
  97. },
  98. preprocessorOptions: {
  99. scss: {
  100. sassOptions: { outputStyle: 'expanded' },
  101. // additionalData(content: string, loaderContext: string) {
  102. // return ['variables.scss'].includes(basename(loaderContext))
  103. // ? content
  104. // : `@use "~/library/styles/variables.scss" as *;${content}`
  105. // },
  106. },
  107. },
  108. devSourcemap: true,
  109. },
  110. plugins: createVitePlugin(env),
  111. define: {
  112. // 如果您必须使用华为组件库且打包报错,请放开该行,放开注释后会将您的环境变量暴露给华为组件库
  113. // 'process.env': { ...process.env },
  114. },
  115. }
  116. })