vite.config.ts 2.3 KB

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