vite.config.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { resolve } from 'path';
  2. import type { UserConfig, Plugin as VitePlugin } from 'vite';
  3. import visualizer from 'rollup-plugin-visualizer';
  4. import { modifyVars } from './build/config/glob/lessModifyVars';
  5. import {
  6. // externals,
  7. cdnConf,
  8. } from './build/config/vite/cdn';
  9. import { createProxy } from './build/config/vite/proxy';
  10. import { createMockServer } from 'vite-plugin-mock';
  11. import PurgeIcons from 'vite-plugin-purge-icons';
  12. import gzipPlugin from './build/plugin/gzip/index';
  13. import globbyTransform from './build/plugin/vite-plugin-context/transform';
  14. import { isDevFn, isReportMode, isProdFn, loadEnv, isBuildGzip, isSiteMode } from './build/utils';
  15. const pkg = require('./package.json');
  16. const {
  17. VITE_USE_MOCK,
  18. VITE_PORT,
  19. VITE_PUBLIC_PATH,
  20. VITE_PROXY,
  21. VITE_GLOB_APP_TITLE,
  22. VITE_DROP_CONSOLE,
  23. // VITE_USE_CDN,
  24. } = loadEnv();
  25. function pathResolve(dir: string) {
  26. return resolve(__dirname, '.', dir);
  27. }
  28. const rollupPlugins: any[] = [];
  29. const vitePlugins: VitePlugin[] = [];
  30. (() => {
  31. if (isProdFn()) {
  32. if (isReportMode()) {
  33. // report
  34. rollupPlugins.push(
  35. visualizer({ filename: './build/.cache/stats.html', open: true }) as Plugin
  36. );
  37. }
  38. if (isBuildGzip() || isSiteMode()) {
  39. rollupPlugins.push(gzipPlugin());
  40. }
  41. }
  42. if (isDevFn() && VITE_USE_MOCK) {
  43. // open mock
  44. vitePlugins.push(
  45. createMockServer({
  46. ignore: /^\_/,
  47. mockPath: 'mock',
  48. })
  49. );
  50. }
  51. })();
  52. const viteConfig: UserConfig = {
  53. /**
  54. * 端口号
  55. * @default '3000'
  56. */
  57. port: VITE_PORT,
  58. /**
  59. * 服务地址
  60. * @default 'localhost'
  61. */
  62. hostname: 'localhost',
  63. /**
  64. * 运行自动打开浏览器·
  65. * @default 'false'
  66. */
  67. open: false,
  68. /**
  69. * 压缩代码
  70. * boolean | 'terser' | 'esbuild'
  71. * @default 'terser'
  72. */
  73. minify: isDevFn() ? 'esbuild' : 'terser',
  74. /**
  75. * 基本公共路径
  76. * @default '/'
  77. */
  78. base: VITE_PUBLIC_PATH,
  79. /**
  80. * 打包输入路径
  81. * @default 'dist'
  82. */
  83. outDir: 'dist',
  84. /**
  85. * @default 'false'
  86. */
  87. sourcemap: false,
  88. /**
  89. * 资源输出路径
  90. * @default '_assets'
  91. */
  92. assetsDir: '_assets',
  93. /**
  94. * 静态资源小于该大小将会内联,默认4096kb
  95. * @default '4096'
  96. */
  97. assetsInlineLimit: 4096,
  98. /**
  99. * esbuild转换目标。
  100. * @default 'es2020'
  101. */
  102. esbuildTarget: 'es2020',
  103. silent: false,
  104. // 别名
  105. alias: {
  106. '/@/': pathResolve('src'),
  107. },
  108. // terser配置
  109. terserOption: {
  110. compress: {
  111. // 是否删除console
  112. drop_console: VITE_DROP_CONSOLE,
  113. },
  114. },
  115. define: {
  116. __VERSION__: pkg.version,
  117. },
  118. // css预处理
  119. cssPreprocessOptions: {
  120. less: {
  121. modifyVars: modifyVars,
  122. javascriptEnabled: true,
  123. },
  124. },
  125. // 会使用 rollup 对 包重新编译,将编译成符合 esm 模块规范的新的包放入 node_modules/.vite_opt_cache
  126. optimizeDeps: {
  127. include: [
  128. 'echarts',
  129. 'echarts/map/js/china',
  130. 'ant-design-vue/es/locale/zh_CN',
  131. '@ant-design/icons-vue',
  132. 'moment/locale/zh-cn',
  133. ],
  134. },
  135. // 本地跨域代理
  136. proxy: createProxy(VITE_PROXY),
  137. plugins: [PurgeIcons(), ...vitePlugins],
  138. rollupOutputOptions: {},
  139. rollupInputOptions: {
  140. // TODO
  141. // external: VITE_USE_CDN ? externals : [],
  142. plugins: rollupPlugins,
  143. },
  144. };
  145. // 扩展配置, 往打包后的html注入内容
  146. // 只针对生产环境
  147. // TODO 目前只是简单手动注入实现,后续vite应该会提供配置项
  148. export const htmlConfig: {
  149. title: string;
  150. addHm?: boolean;
  151. cdnConf?: {
  152. css?: string[];
  153. js?: string[];
  154. };
  155. useCdn: boolean;
  156. minify: {
  157. enable: boolean;
  158. removeComments: boolean;
  159. collapseWhitespace: boolean;
  160. minifyJS: boolean;
  161. minifyCSS: boolean;
  162. };
  163. } = {
  164. // html title
  165. title: VITE_GLOB_APP_TITLE,
  166. // 百度统计,不需要可以删除
  167. // 用于打包部署站点使用。实际项目可以删除
  168. addHm: isSiteMode(),
  169. // 使用cdn打包
  170. // TODO Cdn esm使用方式需要只能支持google,暂时关闭,后续查询更好的方式
  171. useCdn: false,
  172. // useCdn: VITE_USE_CDN,
  173. // cdn列表
  174. cdnConf,
  175. minify: {
  176. enable: true,
  177. removeComments: true,
  178. collapseWhitespace: true,
  179. minifyJS: true,
  180. minifyCSS: true,
  181. },
  182. };
  183. export default {
  184. ...viteConfig,
  185. transforms: [globbyTransform(viteConfig)],
  186. } as UserConfig;