vite.config.ts 2.1 KB

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