vite.config.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. rollupOptions: {
  43. output: {
  44. compact: true,
  45. manualChunks: undefined,
  46. },
  47. },
  48. },
  49. define: {
  50. __VERSION__: pkg.version,
  51. // setting vue-i18-next
  52. // Suppress warning
  53. __VUE_I18N_LEGACY_API__: false,
  54. __VUE_I18N_FULL_INSTALL__: false,
  55. __INTLIFY_PROD_DEVTOOLS__: false,
  56. },
  57. css: {
  58. preprocessorOptions: {
  59. less: {
  60. modifyVars: {
  61. // reference: Avoid repeated references
  62. hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
  63. ...modifyVars,
  64. },
  65. javascriptEnabled: true,
  66. },
  67. },
  68. },
  69. plugins: [
  70. vue(),
  71. vueJsx(),
  72. ...(VITE_LEGACY && isBuild ? [legacy()] : []),
  73. ...createVitePlugins(viteEnv, isBuild, mode),
  74. ],
  75. optimizeDeps: {
  76. include: [
  77. 'moment',
  78. '@ant-design/icons-vue',
  79. 'echarts/map/js/china',
  80. 'ant-design-vue/es/locale/zh_CN',
  81. 'moment/locale/zh-cn',
  82. 'ant-design-vue/es/locale/en_US',
  83. 'resize-observer-polyfill',
  84. ],
  85. },
  86. };
  87. };