vite.config.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. root,
  23. alias: {
  24. '/@/': `${pathResolve('src')}/`,
  25. },
  26. server: {
  27. port: VITE_PORT,
  28. proxy: createProxy(VITE_PROXY),
  29. hmr: {
  30. overlay: true,
  31. },
  32. },
  33. build: {
  34. base: VITE_PUBLIC_PATH,
  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. },
  47. },
  48. commonjsOptions: {
  49. ignore: [
  50. // xlsx
  51. 'fs',
  52. 'crypto',
  53. 'stream',
  54. ],
  55. },
  56. },
  57. define: {
  58. __VERSION__: pkg.version,
  59. // setting vue-i18-next
  60. // Suppress warning
  61. __VUE_I18N_LEGACY_API__: false,
  62. __VUE_I18N_FULL_INSTALL__: false,
  63. __INTLIFY_PROD_DEVTOOLS__: false,
  64. },
  65. css: {
  66. preprocessorOptions: {
  67. less: {
  68. modifyVars: {
  69. // reference: Avoid repeated references
  70. hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
  71. ...modifyVars,
  72. },
  73. javascriptEnabled: true,
  74. },
  75. },
  76. },
  77. plugins: [
  78. vue(),
  79. vueJsx(),
  80. ...(VITE_LEGACY && isBuild ? [legacy()] : []),
  81. ...createVitePlugins(viteEnv, isBuild, mode),
  82. ],
  83. optimizeDeps: {
  84. include: [
  85. 'moment',
  86. '@ant-design/icons-vue',
  87. 'echarts/map/js/china',
  88. 'ant-design-vue/es/locale/zh_CN',
  89. 'moment/locale/zh-cn',
  90. 'ant-design-vue/es/locale/en_US',
  91. ],
  92. },
  93. };
  94. };