vite.config.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 globbyTransform from './build/vite/plugin/transform/globby';
  6. import dynamicImportTransform from './build/vite/plugin/transform/dynamic-import';
  7. import { loadEnv } from './build/utils';
  8. import { createRollupPlugin, createVitePlugins } from './build/vite/plugin';
  9. const pkg = require('./package.json');
  10. const viteEnv = loadEnv();
  11. const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE, VITE_DYNAMIC_IMPORT } = viteEnv;
  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. const viteConfig: UserConfig = {
  21. root,
  22. alias,
  23. /**
  24. * port
  25. * @default '3000'
  26. */
  27. port: VITE_PORT,
  28. /**
  29. * Base public path when served in production.
  30. * @default '/'
  31. */
  32. base: VITE_PUBLIC_PATH,
  33. /**
  34. * Transpile target for esbuild.
  35. * @default 'es2020'
  36. */
  37. esbuildTarget: 'es2019',
  38. // terser options
  39. terserOptions: {
  40. compress: {
  41. drop_console: VITE_DROP_CONSOLE,
  42. },
  43. },
  44. define: {
  45. __VERSION__: pkg.version,
  46. // setting vue-i18-next
  47. // Suppress warning
  48. __VUE_I18N_LEGACY_API__: false,
  49. __VUE_I18N_FULL_INSTALL__: false,
  50. __INTLIFY_PROD_DEVTOOLS__: false,
  51. },
  52. cssPreprocessOptions: {
  53. less: {
  54. modifyVars: modifyVars,
  55. javascriptEnabled: true,
  56. },
  57. },
  58. // 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
  59. optimizeDeps: {
  60. include: [
  61. 'echarts/map/js/china',
  62. 'ant-design-vue/es/locale/zh_CN',
  63. 'ant-design-vue/es/locale/en_US',
  64. '@ant-design/icons-vue',
  65. ],
  66. },
  67. proxy: createProxy(VITE_PROXY),
  68. plugins: createVitePlugins(viteEnv),
  69. rollupInputOptions: {
  70. plugins: createRollupPlugin(),
  71. },
  72. transforms: [
  73. globbyTransform({
  74. resolvers: resolvers,
  75. root: root,
  76. alias: alias,
  77. includes: [resolve('src/router'), resolve('src/locales')],
  78. }),
  79. dynamicImportTransform(VITE_DYNAMIC_IMPORT),
  80. ],
  81. };
  82. export default viteConfig;