vite.config.website.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. open,
  15. outDir,
  16. port,
  17. reportCompressedSize,
  18. } from '/@/config'
  19. import { createVitePlugin, createWatch } from '/@vab/build'
  20. const lastBuildTime = dayjs().format('YYYY-MM-DD HH:mm:ss')
  21. //vue_shop_vite官网独立配置
  22. const minify = 'terser'
  23. const outputHash = true
  24. export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  25. process.env['VITE_APP_UPDATE_TIME'] = lastBuildTime
  26. process.env['VITE_USER_NODE_ENV'] = mode
  27. const root = process.cwd()
  28. const env = loadEnv(mode, root)
  29. createWatch(env)
  30. console.log(lastBuildTime)
  31. return {
  32. base,
  33. root,
  34. server: {
  35. open,
  36. port,
  37. hmr: {
  38. overlay: true,
  39. },
  40. host: '0.0.0.0',
  41. warmup: {
  42. clientFiles: ['./index.html', './library/{components,layouts}/*', './src/{views,plugins}/*'],
  43. },
  44. https,
  45. },
  46. resolve: {
  47. alias: {
  48. '~/': `${resolve(__dirname, '.')}/`,
  49. '/@/': `/${resolve(__dirname, 'src')}/`,
  50. '/@vab/': `/${resolve(__dirname, 'library')}/`,
  51. },
  52. },
  53. optimizeDeps: {
  54. include,
  55. exclude,
  56. },
  57. build: {
  58. assetsDir,
  59. chunkSizeWarningLimit,
  60. cssCodeSplit,
  61. outDir,
  62. reportCompressedSize,
  63. rollupOptions: {
  64. onwarn: () => {
  65. return
  66. },
  67. output: {
  68. chunkFileNames: outputHash ? 'static/js/[name]-[hash].js' : 'static/js/[name].js',
  69. entryFileNames: outputHash ? 'static/js/[name]-[hash].js' : 'static/js/[name].js',
  70. assetFileNames: outputHash ? 'static/[ext]/[name]-[hash].[ext]' : 'static/[ext]/[name].[ext]',
  71. manualChunks: {
  72. 'vsv-element-plus': ['element-plus'],
  73. 'vsv-nprogress': ['nprogress'],
  74. 'vsv-icon': ['vsv-icon'],
  75. 'vsv-echarts': ['echarts'],
  76. },
  77. },
  78. input: ['./index.html', './website.html'],
  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. },
  101. },
  102. devSourcemap: true,
  103. },
  104. plugins: createVitePlugin(env),
  105. define: {
  106. // 如果您必须使用华为组件库且打包报错,请放开该行,放开注释后会将您的环境变量暴露给华为组件库
  107. // 'process.env': { ...process.env },
  108. },
  109. }
  110. })