vite.config.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // sourcemap: true,
  39. polyfillDynamicImport: VITE_LEGACY,
  40. terserOptions: {
  41. compress: {
  42. keep_infinity: true,
  43. drop_console: VITE_DROP_CONSOLE,
  44. },
  45. },
  46. brotliSize: false,
  47. chunkSizeWarningLimit: 1200,
  48. },
  49. define: {
  50. __VERSION__: pkg.version,
  51. // setting vue-i18-next
  52. // Suppress warning
  53. __VUE_I18N_LEGACY_API__: false,
  54. __VUE_I18N_FULL_INSTALL__: false,
  55. __INTLIFY_PROD_DEVTOOLS__: false,
  56. },
  57. css: {
  58. preprocessorOptions: {
  59. less: {
  60. modifyVars: {
  61. // reference: Avoid repeated references
  62. hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
  63. ...generateModifyVars(),
  64. },
  65. javascriptEnabled: true,
  66. },
  67. },
  68. },
  69. plugins: [
  70. vue(),
  71. vueJsx(),
  72. ...(VITE_LEGACY && isBuild ? [legacy()] : []),
  73. ...createVitePlugins(viteEnv, isBuild),
  74. ],
  75. optimizeDeps: {
  76. include: ['@iconify/iconify'],
  77. },
  78. };
  79. };