vite.config.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. terserOptions: {
  36. compress: {
  37. keep_infinity: true,
  38. drop_console: VITE_DROP_CONSOLE,
  39. },
  40. },
  41. // minify: 'esbuild',
  42. rollupOptions: {
  43. output: {
  44. compact: true,
  45. },
  46. },
  47. commonjsOptions: {
  48. ignore: [
  49. // xlsx
  50. 'fs',
  51. 'crypto',
  52. 'stream',
  53. ],
  54. },
  55. },
  56. define: {
  57. __VERSION__: pkg.version,
  58. // setting vue-i18-next
  59. // Suppress warning
  60. __VUE_I18N_LEGACY_API__: false,
  61. __VUE_I18N_FULL_INSTALL__: false,
  62. __INTLIFY_PROD_DEVTOOLS__: false,
  63. },
  64. css: {
  65. preprocessorOptions: {
  66. less: {
  67. modifyVars: {
  68. // reference: Avoid repeated references
  69. hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
  70. ...modifyVars,
  71. },
  72. javascriptEnabled: true,
  73. },
  74. },
  75. },
  76. plugins: [
  77. vue(),
  78. vueJsx(),
  79. ...(VITE_LEGACY && isBuild ? [legacy()] : []),
  80. ...createVitePlugins(viteEnv, isBuild, mode),
  81. ],
  82. optimizeDeps: {
  83. include: [
  84. 'moment',
  85. '@ant-design/icons-vue',
  86. 'echarts/map/js/china',
  87. 'ant-design-vue/es/locale/zh_CN',
  88. 'moment/locale/zh-cn',
  89. 'ant-design-vue/es/locale/en_US',
  90. ],
  91. },
  92. };
  93. };