vite.config.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import type { UserConfig, Resolver } from 'vite';
  2. import { resolve } from 'path';
  3. import { loadEnv } from 'vite';
  4. import { modifyVars } from './build/config/lessModifyVars';
  5. import { createProxy } from './build/vite/proxy';
  6. import { configManualChunk } from './build/vite/optimizer';
  7. import globbyTransform from './build/vite/plugin/transform/globby';
  8. import dynamicImportTransform from './build/vite/plugin/transform/dynamic-import';
  9. import { wrapperEnv } from './build/utils';
  10. import { createRollupPlugin, createVitePlugins } from './build/vite/plugin';
  11. const pkg = require('./package.json');
  12. function pathResolve(dir: string) {
  13. return resolve(__dirname, '.', dir);
  14. }
  15. const alias: Record<string, string> = {
  16. '/@/': pathResolve('src'),
  17. };
  18. const root: string = process.cwd();
  19. const resolvers: Resolver[] = [];
  20. export default (mode: 'development' | 'production'): UserConfig => {
  21. const env = loadEnv(mode, root);
  22. const viteEnv = wrapperEnv(env);
  23. const {
  24. VITE_PORT,
  25. VITE_PUBLIC_PATH,
  26. VITE_PROXY,
  27. VITE_DROP_CONSOLE,
  28. VITE_DYNAMIC_IMPORT,
  29. } = viteEnv;
  30. return {
  31. root,
  32. alias,
  33. /**
  34. * port
  35. * @default '3000'
  36. */
  37. port: VITE_PORT,
  38. /**
  39. * Base public path when served in production.
  40. * @default '/'
  41. */
  42. base: VITE_PUBLIC_PATH,
  43. /**
  44. * Transpile target for esbuild.
  45. * @default 'es2020'
  46. */
  47. esbuildTarget: 'es2019',
  48. // terser options
  49. terserOptions: {
  50. compress: {
  51. keep_infinity: true,
  52. drop_console: VITE_DROP_CONSOLE,
  53. },
  54. },
  55. define: {
  56. __VERSION__: pkg.version,
  57. // setting vue-i18-next
  58. // Suppress warning
  59. __VUE_I18N_LEGACY_API__: false,
  60. __VUE_I18N_FULL_INSTALL__: false,
  61. __INTLIFY_PROD_DEVTOOLS__: false,
  62. },
  63. cssPreprocessOptions: {
  64. less: {
  65. modifyVars: modifyVars,
  66. javascriptEnabled: true,
  67. },
  68. },
  69. // 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
  70. optimizeDeps: {
  71. include: [
  72. 'qs',
  73. 'echarts/map/js/china',
  74. 'ant-design-vue/es/locale/zh_CN',
  75. 'ant-design-vue/es/locale/en_US',
  76. '@ant-design/icons-vue',
  77. ],
  78. },
  79. transforms: [
  80. globbyTransform({
  81. resolvers: resolvers,
  82. root: root,
  83. alias: alias,
  84. includes: [resolve('src/router'), resolve('src/locales')],
  85. }),
  86. dynamicImportTransform(VITE_DYNAMIC_IMPORT),
  87. ],
  88. proxy: createProxy(VITE_PROXY),
  89. plugins: createVitePlugins(viteEnv, mode),
  90. rollupInputOptions: {
  91. plugins: createRollupPlugin(),
  92. },
  93. rollupOutputOptions: {
  94. compact: true,
  95. manualChunks: configManualChunk,
  96. },
  97. };
  98. };