vite.config.ts 2.3 KB

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