vite.config.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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: {
  66. // reference : Avoid repeated references
  67. hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
  68. ...modifyVars,
  69. },
  70. javascriptEnabled: true,
  71. },
  72. },
  73. // 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
  74. optimizeDeps: {
  75. include: [
  76. 'qs',
  77. 'echarts/map/js/china',
  78. 'ant-design-vue/es/locale/zh_CN',
  79. 'ant-design-vue/es/locale/en_US',
  80. '@ant-design/icons-vue',
  81. ],
  82. },
  83. transforms: [
  84. globbyTransform({
  85. resolvers: resolvers,
  86. root: root,
  87. alias: alias,
  88. includes: [resolve('src/router'), resolve('src/locales')],
  89. }),
  90. dynamicImportTransform(VITE_DYNAMIC_IMPORT),
  91. ],
  92. proxy: createProxy(VITE_PROXY),
  93. plugins: createVitePlugins(viteEnv, mode),
  94. rollupInputOptions: {
  95. plugins: createRollupPlugin(),
  96. },
  97. rollupOutputOptions: {
  98. compact: true,
  99. manualChunks: configManualChunk,
  100. },
  101. };
  102. };