vite.config.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import type { ConfigEnv, UserConfig } from 'vite'
  2. import { loadEnv } from 'vite'
  3. import dayjs from 'dayjs'
  4. // import vue from "@vitejs/plugin-vue";
  5. import { resolve } from 'path'
  6. import pkg from './package.json'
  7. import { OUTPUT_DIR } from './build/constant'
  8. import { wrapperEnv } from './build/utils'
  9. import { createProxy } from './build/vite/proxy'
  10. import { createVitePlugins } from './build/vite/plugin/index'
  11. import { generateModifyVars } from './build/generate/generateModifyVars'
  12. const pathResolve = (dir: string) => {
  13. return resolve(process.cwd(), '.', dir)
  14. }
  15. const { dependencies, devDependencies, name, version } = pkg
  16. const __APP_INFO__ = {
  17. pkg: { dependencies, devDependencies, name, version },
  18. lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss')
  19. }
  20. // https://vitejs.dev/config/
  21. export default ({ command, mode }: ConfigEnv): UserConfig => {
  22. const root = process.cwd()
  23. const env = loadEnv(mode, pathResolve('env'))
  24. const viteEnv = wrapperEnv(env)
  25. const { VITE_PORT, VITE_PROXY, VITE_PUBLIC_PATH } = viteEnv
  26. // 是否构建
  27. const isBuild = command === 'build'
  28. return {
  29. base: VITE_PUBLIC_PATH,
  30. root,
  31. server: {
  32. // 监听到本地IP
  33. host: true,
  34. port: VITE_PORT,
  35. // 本地开发的代理配置
  36. proxy: createProxy(VITE_PROXY)
  37. },
  38. build: {
  39. target: 'es2015',
  40. outDir: OUTPUT_DIR,
  41. minify: 'esbuild',
  42. // 启用/禁用 brotli 压缩大小报告。压缩大型输出文件可能会很慢,因此禁用该功能可能会提高大型项目的构建性能。
  43. // brotliSize: false,
  44. // chunk 大小警告的限制(以 kbs 为单位)。
  45. chunkSizeWarningLimit: 2000,
  46. rollupOptions: {
  47. output: {
  48. // 用于输出静态资源的命名,[ext]表示文件扩展名
  49. assetFileNames: '[ext]/[name].[hash].[ext]',
  50. // 用于命名代码拆分时创建的共享块的输出命名
  51. // chunkFileNames: "js/[name].[hash].js",
  52. // 拆分js到模块文件夹
  53. chunkFileNames: (chunkInfo) => {
  54. const facadeModuleId = chunkInfo.facadeModuleId ? chunkInfo.facadeModuleId.split('/') : []
  55. const fileName = facadeModuleId[facadeModuleId.length - 2] || '[name]'
  56. // const reg = new RegExp(/\/src\/views(\S*)\/index\.vue/);
  57. // const itemPath = chunkInfo.facadeModuleId ? chunkInfo.facadeModuleId.match(reg) || [] : [];
  58. // if (chunkInfo.name && ["vue", "vue-router"].includes(chunkInfo.name)) {
  59. // return `assets/[name].js`;
  60. // }
  61. // if (chunkInfo.name && chunkInfo.name.includes("demo-")) {
  62. // return `js/demo/[name].[hash].js`;
  63. // }
  64. return `js/${fileName}/[name].[hash].js`
  65. }
  66. // manualChunks: (id) => {
  67. // const itemPath = id.match(RegExp(/\/src\/views\/demo(\S*)\/index\.vue/));
  68. // if (itemPath) {
  69. // const [, modPath] = itemPath[1].split("/");
  70. // return `demo-${modPath}`;
  71. // }
  72. // },
  73. }
  74. }
  75. },
  76. resolve: {
  77. // 别名
  78. alias: [
  79. // /@/xxxx => src/xxxx
  80. {
  81. find: /\/@\//,
  82. replacement: pathResolve('src') + '/'
  83. },
  84. // /#/xxxx => types/xxxx
  85. {
  86. find: /\/#\//,
  87. replacement: pathResolve('types') + '/'
  88. }
  89. ]
  90. },
  91. // 构建模式所需的特有配置
  92. plugins: createVitePlugins(viteEnv, isBuild),
  93. // css配置
  94. css: {
  95. preprocessorOptions: {
  96. less: {
  97. modifyVars: generateModifyVars(),
  98. javascriptEnabled: true
  99. }
  100. }
  101. },
  102. define: {
  103. __APP_INFO__: JSON.stringify(__APP_INFO__)
  104. }
  105. }
  106. }