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. 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. {
  26. find: /^\/@\//,
  27. replacement: pathResolve('src') + '/',
  28. },
  29. ],
  30. server: {
  31. port: VITE_PORT,
  32. proxy: createProxy(VITE_PROXY),
  33. hmr: {
  34. overlay: true,
  35. },
  36. },
  37. build: {
  38. polyfillDynamicImport: VITE_LEGACY,
  39. terserOptions: {
  40. compress: {
  41. keep_infinity: true,
  42. drop_console: VITE_DROP_CONSOLE,
  43. },
  44. },
  45. brotliSize: false,
  46. chunkSizeWarningLimit: 1000,
  47. },
  48. define: {
  49. __VERSION__: pkg.version,
  50. // setting vue-i18-next
  51. // Suppress warning
  52. __VUE_I18N_LEGACY_API__: false,
  53. __VUE_I18N_FULL_INSTALL__: false,
  54. __INTLIFY_PROD_DEVTOOLS__: false,
  55. },
  56. css: {
  57. preprocessorOptions: {
  58. less: {
  59. modifyVars: {
  60. // reference: Avoid repeated references
  61. hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
  62. ...generateModifyVars(),
  63. },
  64. javascriptEnabled: true,
  65. },
  66. },
  67. },
  68. plugins: [
  69. vue(),
  70. vueJsx(),
  71. ...(VITE_LEGACY && isBuild ? [legacy()] : []),
  72. ...createVitePlugins(viteEnv, isBuild),
  73. ],
  74. optimizeDeps: {
  75. include: ['@iconify/iconify'],
  76. },
  77. };
  78. };