vite.config.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import autoprefixer from 'autoprefixer'
  2. import dayjs from 'dayjs'
  3. import { resolve } from 'path'
  4. import type { ConfigEnv, UserConfig } from 'vite'
  5. import { defineConfig, loadEnv } from 'vite'
  6. import { dependencies, devDependencies, name, version } from './package.json'
  7. import {
  8. assetsDir,
  9. base,
  10. chunkSizeWarningLimit,
  11. cssCodeSplit,
  12. minify,
  13. open,
  14. outDir,
  15. outputHash,
  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. const info = { dependencies, devDependencies, lastBuildTime, name, version }
  22. export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  23. process.env['VITE_APP_UPDATE_TIME'] = info.lastBuildTime
  24. process.env['VITE_USER_NODE_ENV'] = mode
  25. const root = process.cwd()
  26. const env = loadEnv(mode, root)
  27. createWatch(env)
  28. console.log(info.lastBuildTime)
  29. return {
  30. base,
  31. root,
  32. server: {
  33. open,
  34. port,
  35. hmr: {
  36. overlay: true,
  37. },
  38. host: '0.0.0.0',
  39. },
  40. resolve: {
  41. alias: {
  42. '~/': `${resolve(__dirname, '.')}/`,
  43. '/@/': `/${resolve(__dirname, 'src')}/`,
  44. '/@vab/': `/${resolve(__dirname, 'library')}/`,
  45. '/@types/': `/${resolve(__dirname, 'src/types')}/`,
  46. },
  47. },
  48. build: {
  49. assetsDir,
  50. chunkSizeWarningLimit,
  51. cssCodeSplit,
  52. outDir,
  53. reportCompressedSize,
  54. rollupOptions: {
  55. onwarn: () => {
  56. return
  57. },
  58. output: {
  59. chunkFileNames: outputHash ? 'static/js/[name]-[hash].js' : 'static/js/[name].js',
  60. entryFileNames: outputHash ? 'static/js/[name]-[hash].js' : 'static/js/[name].js',
  61. assetFileNames: outputHash ? 'static/[ext]/[name]-[hash].[ext]' : 'static/[ext]/[name].[ext]',
  62. },
  63. },
  64. minify,
  65. },
  66. css: {
  67. postcss: {
  68. plugins: [
  69. autoprefixer({ grid: true }),
  70. {
  71. postcssPlugin: 'internal:charset-removal',
  72. AtRule: {
  73. charset: (atRule: { name: string; remove: () => void }) => {
  74. if (atRule.name === 'charset') atRule.remove()
  75. },
  76. },
  77. },
  78. ],
  79. },
  80. preprocessorOptions: {
  81. scss: {
  82. sassOptions: { outputStyle: 'expanded' },
  83. // additionalData(content: string, loaderContext: string) {
  84. // return ['variables.scss'].includes(basename(loaderContext))
  85. // ? content
  86. // : `@use "~/library/styles/variables.scss" as *;${content}`
  87. // },
  88. },
  89. },
  90. devSourcemap: true,
  91. },
  92. plugins: createVitePlugin(env),
  93. }
  94. })