vite.config.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import type { UserConfig } 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/context/transform';
  6. import dynamicImportTransform from './build/vite/plugin/dynamicImport/index';
  7. import { isDevFn, loadEnv } from './build/utils';
  8. import { createRollupPlugin, createVitePlugins } from './build/vite/plugin';
  9. const pkg = require('./package.json');
  10. const viteEnv = loadEnv();
  11. const {
  12. VITE_PORT,
  13. VITE_PUBLIC_PATH,
  14. VITE_PROXY,
  15. VITE_DROP_CONSOLE,
  16. // VITE_USE_CDN,
  17. } = viteEnv;
  18. function pathResolve(dir: string) {
  19. return resolve(__dirname, '.', dir);
  20. }
  21. const viteConfig: UserConfig = {
  22. /**
  23. * Entry. Use this to specify a js entry file in use cases where an
  24. * `index.html` does not exist (e.g. serving vite assets from a different host)
  25. * @default 'index.html'
  26. */
  27. // TODO build error
  28. // entry: 'public/index.html',
  29. /**
  30. * port
  31. * @default '3000'
  32. */
  33. port: VITE_PORT,
  34. /**
  35. * @default 'localhost'
  36. */
  37. hostname: 'localhost',
  38. /**
  39. * Run to open the browser automatically
  40. * @default 'false'
  41. */
  42. open: false,
  43. /**
  44. * Set to `false` to disable minification, or specify the minifier to use.
  45. * Available options are 'terser' or 'esbuild'.
  46. * @default 'terser'
  47. */
  48. minify: isDevFn() ? 'esbuild' : 'terser',
  49. /**
  50. * Base public path when served in production.
  51. * @default '/'
  52. */
  53. base: VITE_PUBLIC_PATH,
  54. /**
  55. * Directory relative from `root` where build output will be placed. If the
  56. * directory exists, it will be removed before the build.
  57. * @default 'dist'
  58. */
  59. outDir: 'dist',
  60. /**
  61. * Whether to generate sourcemap
  62. * @default false
  63. */
  64. sourcemap: false,
  65. /**
  66. * Directory relative from `outDir` where the built js/css/image assets will
  67. * be placed.
  68. * @default '_assets'
  69. */
  70. assetsDir: '_assets',
  71. /**
  72. * Static asset files smaller than this number (in bytes) will be inlined as
  73. * base64 strings. Default limit is `4096` (4kb). Set to `0` to disable.
  74. * @default 4096
  75. */
  76. assetsInlineLimit: 4096,
  77. /**
  78. * Transpile target for esbuild.
  79. * @default 'es2020'
  80. */
  81. esbuildTarget: 'es2020',
  82. /**
  83. * Whether to log asset info to console
  84. * @default false
  85. */
  86. silent: false,
  87. /**
  88. * Import alias. The entries can either be exact request -> request mappings
  89. * (exact, no wildcard syntax), or request path -> fs directory mappings.
  90. * When using directory mappings, the key **must start and end with a slash**.
  91. * ```
  92. */
  93. alias: {
  94. '/@/': pathResolve('src'),
  95. },
  96. // terser options
  97. terserOptions: {
  98. compress: {
  99. drop_console: VITE_DROP_CONSOLE,
  100. },
  101. },
  102. define: {
  103. __VERSION__: pkg.version,
  104. },
  105. cssPreprocessOptions: {
  106. less: {
  107. modifyVars: modifyVars,
  108. javascriptEnabled: true,
  109. },
  110. },
  111. // 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
  112. optimizeDeps: {
  113. include: ['echarts/map/js/china', 'ant-design-vue/es/locale/zh_CN', '@ant-design/icons-vue'],
  114. },
  115. // Local cross-domain proxy
  116. proxy: createProxy(VITE_PROXY),
  117. plugins: createVitePlugins(viteEnv),
  118. rollupInputOptions: {
  119. // TODO
  120. // external: VITE_USE_CDN ? externals : [],
  121. plugins: createRollupPlugin(),
  122. },
  123. };
  124. export default {
  125. ...viteConfig,
  126. transforms: [globbyTransform(viteConfig), dynamicImportTransform(viteEnv)],
  127. } as UserConfig;