vite.config.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 { isDevFn, loadEnv } from './build/utils';
  7. import { createRollupPlugin, createVitePlugins } from './build/vite/plugin';
  8. const pkg = require('./package.json');
  9. const viteEnv = loadEnv();
  10. const {
  11. VITE_PORT,
  12. VITE_PUBLIC_PATH,
  13. VITE_PROXY,
  14. VITE_DROP_CONSOLE,
  15. // VITE_USE_CDN,
  16. } = viteEnv;
  17. function pathResolve(dir: string) {
  18. return resolve(__dirname, '.', dir);
  19. }
  20. const viteConfig: UserConfig = {
  21. /**
  22. * Entry. Use this to specify a js entry file in use cases where an
  23. * `index.html` does not exist (e.g. serving vite assets from a different host)
  24. * @default 'index.html'
  25. */
  26. // TODO build error
  27. // entry: './public/index.html',
  28. /**
  29. * 端口号
  30. * @default '3000'
  31. */
  32. port: VITE_PORT,
  33. /**
  34. * 服务地址
  35. * @default 'localhost'
  36. */
  37. hostname: 'localhost',
  38. /**
  39. * 运行自动打开浏览器·
  40. * @default 'false'
  41. */
  42. open: false,
  43. /**
  44. * 压缩代码
  45. * boolean | 'terser' | 'esbuild'
  46. * @default 'terser'
  47. */
  48. minify: isDevFn() ? 'esbuild' : 'terser',
  49. /**
  50. * 基本公共路径
  51. * @default '/'
  52. */
  53. base: VITE_PUBLIC_PATH,
  54. /**
  55. * 打包输入路径
  56. * @default 'dist'
  57. */
  58. outDir: 'dist',
  59. /**
  60. * @default 'false'
  61. */
  62. sourcemap: false,
  63. /**
  64. * 资源输出路径
  65. * @default '_assets'
  66. */
  67. assetsDir: '_assets',
  68. /**
  69. * 静态资源小于该大小将会内联,默认4096kb
  70. * @default '4096'
  71. */
  72. assetsInlineLimit: 4096,
  73. /**
  74. * esbuild转换目标。
  75. * @default 'es2020'
  76. */
  77. esbuildTarget: 'es2020',
  78. silent: false,
  79. // 别名
  80. alias: {
  81. '/@/': pathResolve('src'),
  82. },
  83. // terser配置
  84. terserOptions: {
  85. compress: {
  86. // 是否删除console
  87. drop_console: VITE_DROP_CONSOLE,
  88. },
  89. },
  90. define: {
  91. __VERSION__: pkg.version,
  92. },
  93. // css预处理
  94. cssPreprocessOptions: {
  95. less: {
  96. modifyVars: modifyVars,
  97. javascriptEnabled: true,
  98. },
  99. },
  100. // 会使用 rollup 对 包重新编译,将编译成符合 esm 模块规范的新的包放入 node_modules/.vite_opt_cache
  101. optimizeDeps: {
  102. include: [
  103. 'echarts',
  104. 'echarts/map/js/china',
  105. 'ant-design-vue/es/locale/zh_CN',
  106. '@ant-design/icons-vue',
  107. 'moment/locale/zh-cn',
  108. ],
  109. },
  110. // 本地跨域代理
  111. proxy: createProxy(VITE_PROXY),
  112. plugins: createVitePlugins(viteEnv),
  113. rollupInputOptions: {
  114. // TODO
  115. // external: VITE_USE_CDN ? externals : [],
  116. plugins: createRollupPlugin(),
  117. },
  118. };
  119. export default {
  120. ...viteConfig,
  121. transforms: [globbyTransform(viteConfig)],
  122. } as UserConfig;