vite.config.dev.ts 3.4 KB

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