vite.config.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import type { UserConfig, Resolver } from 'vite';
  2. import { resolve } from 'path';
  3. import { modifyVars } from './build/config/lessModifyVars';
  4. import { createProxy } from './build/vite/proxy';
  5. import { configManualChunk } from './build/vite/optimizer';
  6. import globbyTransform from './build/vite/plugin/transform/globby';
  7. import dynamicImportTransform from './build/vite/plugin/transform/dynamic-import';
  8. import { loadEnv } from './build/utils';
  9. import { createRollupPlugin, createVitePlugins } from './build/vite/plugin';
  10. const pkg = require('./package.json');
  11. const viteEnv = loadEnv();
  12. const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE, VITE_DYNAMIC_IMPORT } = viteEnv;
  13. function pathResolve(dir: string) {
  14. return resolve(__dirname, '.', dir);
  15. }
  16. const alias: Record<string, string> = {
  17. '/@/': pathResolve('src'),
  18. };
  19. const root: string = process.cwd();
  20. const resolvers: Resolver[] = [];
  21. const viteConfig: UserConfig = {
  22. root,
  23. alias,
  24. /**
  25. * port
  26. * @default '3000'
  27. */
  28. port: VITE_PORT,
  29. /**
  30. * Base public path when served in production.
  31. * @default '/'
  32. */
  33. base: VITE_PUBLIC_PATH,
  34. /**
  35. * Transpile target for esbuild.
  36. * @default 'es2020'
  37. */
  38. esbuildTarget: 'es2019',
  39. // terser options
  40. terserOptions: {
  41. compress: {
  42. keep_infinity: true,
  43. drop_console: VITE_DROP_CONSOLE,
  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. cssPreprocessOptions: {
  55. less: {
  56. modifyVars: modifyVars,
  57. javascriptEnabled: true,
  58. },
  59. },
  60. // The package will be recompiled using rollup, and the new package compiled into the esm module specification will be put into node_modules/.vite_opt_cache
  61. optimizeDeps: {
  62. include: [
  63. 'echarts/map/js/china',
  64. 'ant-design-vue/es/locale/zh_CN',
  65. 'ant-design-vue/es/locale/en_US',
  66. '@ant-design/icons-vue',
  67. ],
  68. },
  69. transforms: [
  70. globbyTransform({
  71. resolvers: resolvers,
  72. root: root,
  73. alias: alias,
  74. includes: [resolve('src/router'), resolve('src/locales')],
  75. }),
  76. dynamicImportTransform(VITE_DYNAMIC_IMPORT),
  77. ],
  78. proxy: createProxy(VITE_PROXY),
  79. plugins: createVitePlugins(viteEnv),
  80. rollupInputOptions: {
  81. plugins: createRollupPlugin(),
  82. },
  83. rollupOutputOptions: {
  84. compact: true,
  85. manualChunks: configManualChunk,
  86. },
  87. };
  88. export default viteConfig;