vite.config.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 { generateModifyVars } from './build/config/themeConfig';
  8. import { createProxy } from './build/vite/proxy';
  9. import { wrapperEnv } from './build/utils';
  10. import { createVitePlugins } from './build/vite/plugin';
  11. import { OUTPUT_DIR } from './build/constant';
  12. const pkg = require('./package.json');
  13. function pathResolve(dir: string) {
  14. return resolve(__dirname, '.', dir);
  15. }
  16. const root = process.cwd();
  17. export default ({ command, mode }: ConfigEnv): UserConfig => {
  18. const env = loadEnv(mode, root);
  19. const viteEnv = wrapperEnv(env);
  20. const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE, VITE_LEGACY } = viteEnv;
  21. const isBuild = command === 'build';
  22. return {
  23. base: VITE_PUBLIC_PATH,
  24. root,
  25. alias: [
  26. {
  27. find: /^\/@\//,
  28. replacement: pathResolve('src') + '/',
  29. },
  30. ],
  31. server: {
  32. port: VITE_PORT,
  33. proxy: createProxy(VITE_PROXY),
  34. hmr: {
  35. overlay: true,
  36. },
  37. },
  38. build: {
  39. outDir: OUTPUT_DIR,
  40. polyfillDynamicImport: VITE_LEGACY,
  41. terserOptions: {
  42. compress: {
  43. keep_infinity: true,
  44. drop_console: VITE_DROP_CONSOLE,
  45. },
  46. },
  47. brotliSize: false,
  48. chunkSizeWarningLimit: 1200,
  49. },
  50. define: {
  51. __VERSION__: pkg.version,
  52. // setting vue-i18-next
  53. // Suppress warning
  54. __VUE_I18N_LEGACY_API__: false,
  55. __VUE_I18N_FULL_INSTALL__: false,
  56. __INTLIFY_PROD_DEVTOOLS__: false,
  57. },
  58. css: {
  59. preprocessorOptions: {
  60. less: {
  61. modifyVars: {
  62. // reference: Avoid repeated references
  63. hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
  64. ...generateModifyVars(),
  65. },
  66. javascriptEnabled: true,
  67. },
  68. },
  69. },
  70. plugins: [
  71. vue(),
  72. vueJsx(),
  73. ...(VITE_LEGACY && isBuild ? [legacy()] : []),
  74. ...createVitePlugins(viteEnv, isBuild),
  75. ],
  76. optimizeDeps: {
  77. include: ['@iconify/iconify'],
  78. },
  79. };
  80. };