vite.config.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import type { UserConfig, ConfigEnv } from 'vite';
  2. import pkg from './package.json';
  3. import dayjs from 'dayjs';
  4. import { loadEnv } from 'vite';
  5. import { resolve } from 'path';
  6. import { generateModifyVars } from './build/generate/generateModifyVars';
  7. import { createProxy } from './build/vite/proxy';
  8. import { wrapperEnv } from './build/utils';
  9. import { createVitePlugins } from './build/vite/plugin';
  10. import { OUTPUT_DIR } from './build/constant';
  11. function pathResolve(dir: string) {
  12. return resolve(process.cwd(), '.', dir);
  13. }
  14. const { dependencies, devDependencies, name, version } = pkg;
  15. const __APP_INFO__ = {
  16. pkg: { dependencies, devDependencies, name, version },
  17. lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  18. };
  19. export default ({ command, mode }: ConfigEnv): UserConfig => {
  20. const root = process.cwd();
  21. const env = loadEnv(mode, root);
  22. // The boolean type read by loadEnv is a string. This function can be converted to boolean type
  23. const viteEnv = wrapperEnv(env);
  24. const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE } = viteEnv;
  25. const isBuild = command === 'build';
  26. return {
  27. base: VITE_PUBLIC_PATH,
  28. root,
  29. resolve: {
  30. alias: [
  31. {
  32. find: 'vue-i18n',
  33. replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
  34. },
  35. // @/xxxx => src/xxxx
  36. {
  37. find: /@\//,
  38. replacement: pathResolve('src') + '/',
  39. },
  40. // #/xxxx => types/xxxx
  41. {
  42. find: /#\//,
  43. replacement: pathResolve('types') + '/',
  44. },
  45. ],
  46. },
  47. server: {
  48. https: false,
  49. // Listening on all local IPs
  50. host: true,
  51. port: VITE_PORT,
  52. // Load proxy configuration from .env
  53. proxy: createProxy(VITE_PROXY),
  54. open: true, //vite项目启动时自动打开浏览器
  55. },
  56. esbuild: {
  57. drop: VITE_DROP_CONSOLE ? ['console', 'debugger'] : [],
  58. },
  59. build: {
  60. target: 'es2022',
  61. cssTarget: 'chrome80',
  62. outDir: OUTPUT_DIR,
  63. minify: 'terser',
  64. terserOptions: {
  65. compress: {
  66. keep_infinity: true,
  67. // Used to delete console in production environment
  68. drop_console: VITE_DROP_CONSOLE,
  69. drop_debugger: true,
  70. },
  71. },
  72. // Turning off reportCompressedSize display can slightly reduce packaging time
  73. reportCompressedSize: false,
  74. chunkSizeWarningLimit: 2000,
  75. rollupOptions: {
  76. input: {
  77. index: pathResolve('index.html'),
  78. },
  79. // 静态资源分类打包
  80. output: {
  81. chunkFileNames: 'static/js/[name]-[hash].js',
  82. entryFileNames: 'static/js/[name]-[hash].js',
  83. assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
  84. },
  85. },
  86. },
  87. define: {
  88. // setting vue-i18-next
  89. // Suppress warning
  90. __INTLIFY_PROD_DEVTOOLS__: false,
  91. __APP_INFO__: JSON.stringify(__APP_INFO__),
  92. },
  93. css: {
  94. preprocessorOptions: {
  95. less: {
  96. modifyVars: generateModifyVars(),
  97. javascriptEnabled: true,
  98. },
  99. },
  100. },
  101. // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
  102. plugins: createVitePlugins(viteEnv, isBuild),
  103. optimizeDeps: {
  104. esbuildOptions: {
  105. target: 'es2020',
  106. },
  107. // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
  108. include: [
  109. '@vue/runtime-core',
  110. '@vue/shared',
  111. '@iconify/iconify',
  112. 'ant-design-vue/es/locale/zh_CN',
  113. 'ant-design-vue/es/locale/zh_TW',
  114. 'ant-design-vue/es/locale/en_US',
  115. `monaco-editor/esm/vs/language/json/json.worker`,
  116. `monaco-editor/esm/vs/language/css/css.worker`,
  117. `monaco-editor/esm/vs/language/html/html.worker`,
  118. `monaco-editor/esm/vs/language/typescript/ts.worker`,
  119. `monaco-editor/esm/vs/editor/editor.worker`,
  120. ],
  121. },
  122. };
  123. };