vite.config.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import type { UserConfig, ConfigEnv } from 'vite';
  2. import { resolve } from 'path';
  3. import vue from '@vitejs/plugin-vue';
  4. import vueJsx from '@vitejs/plugin-vue-jsx';
  5. import legacy from '@vitejs/plugin-legacy';
  6. import { loadEnv } from 'vite';
  7. import { modifyVars } from './build/config/lessModifyVars';
  8. import { createProxy } from './build/vite/proxy';
  9. import { wrapperEnv } from './build/utils';
  10. import { createVitePlugins } from './build/vite/plugin';
  11. const pkg = require('./package.json');
  12. function pathResolve(dir: string) {
  13. return resolve(__dirname, '.', dir);
  14. }
  15. const root: string = process.cwd();
  16. export default ({ command, mode }: ConfigEnv): UserConfig => {
  17. const env = loadEnv(mode, root);
  18. const viteEnv = wrapperEnv(env);
  19. const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE, VITE_LEGACY } = viteEnv;
  20. const isBuild = command === 'build';
  21. return {
  22. base: VITE_PUBLIC_PATH,
  23. root,
  24. alias: {
  25. '/@/': `${pathResolve('src')}/`,
  26. },
  27. server: {
  28. port: VITE_PORT,
  29. proxy: createProxy(VITE_PROXY),
  30. hmr: {
  31. overlay: true,
  32. },
  33. },
  34. build: {
  35. polyfillDynamicImport: VITE_LEGACY,
  36. terserOptions: {
  37. compress: {
  38. keep_infinity: true,
  39. drop_console: VITE_DROP_CONSOLE,
  40. },
  41. },
  42. // minify: 'esbuild',
  43. rollupOptions: {
  44. output: {
  45. compact: true,
  46. manualChunks: undefined,
  47. },
  48. },
  49. minify: 'esbuild',
  50. commonjsOptions: {
  51. ignore: [
  52. // xlsx
  53. 'fs',
  54. 'crypto',
  55. 'stream',
  56. ],
  57. },
  58. },
  59. define: {
  60. __VERSION__: pkg.version,
  61. // setting vue-i18-next
  62. // Suppress warning
  63. __VUE_I18N_LEGACY_API__: false,
  64. __VUE_I18N_FULL_INSTALL__: false,
  65. __INTLIFY_PROD_DEVTOOLS__: false,
  66. },
  67. css: {
  68. preprocessorOptions: {
  69. less: {
  70. modifyVars: {
  71. // reference: Avoid repeated references
  72. hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
  73. ...modifyVars,
  74. },
  75. javascriptEnabled: true,
  76. },
  77. },
  78. },
  79. plugins: [
  80. vue(),
  81. vueJsx(),
  82. ...(VITE_LEGACY && isBuild ? [legacy()] : []),
  83. ...createVitePlugins(viteEnv, isBuild, mode),
  84. ],
  85. optimizeDeps: {
  86. include: [
  87. 'moment',
  88. '@ant-design/icons-vue',
  89. 'echarts/map/js/china',
  90. 'ant-design-vue/es/locale/zh_CN',
  91. 'moment/locale/zh-cn',
  92. 'ant-design-vue/es/locale/en_US',
  93. 'resize-observer-polyfill',
  94. ],
  95. },
  96. };
  97. };