vite.config.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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: '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. // 配置Dep优化行为
  126. // 会使用 rollup 对 包重新编译,将编译成符合 esm 模块规范的新的包放入 node_modules 下的 .
  127. optimizeDeps: {
  128. include: [
  129. 'echarts',
  130. 'echarts/map/js/china',
  131. 'ant-design-vue/es/locale/zh_CN',
  132. '@ant-design/icons-vue',
  133. 'moment/locale/zh-cn',
  134. ],
  135. },
  136. // 本地跨域代理
  137. proxy: createProxy(VITE_PROXY),
  138. plugins: [PurgeIcons(), ...vitePlugins],
  139. rollupOutputOptions: {},
  140. rollupInputOptions: {
  141. // TODO
  142. // external: VITE_USE_CDN ? externals : [],
  143. plugins: rollupPlugins,
  144. },
  145. };
  146. // 扩展配置, 往打包后的html注入内容
  147. // 只针对生产环境
  148. // TODO 目前只是简单手动注入实现,后续vite应该会提供配置项
  149. export const htmlConfig: {
  150. title: string;
  151. addHm?: boolean;
  152. cdnConf?: {
  153. css?: string[];
  154. js?: string[];
  155. };
  156. useCdn: boolean;
  157. minify: {
  158. enable: boolean;
  159. removeComments: boolean;
  160. collapseWhitespace: boolean;
  161. minifyJS: boolean;
  162. minifyCSS: boolean;
  163. };
  164. } = {
  165. // html title
  166. title: VITE_GLOB_APP_TITLE,
  167. // 百度统计,不需要可以删除
  168. // 用于打包部署站点使用。实际项目可以删除
  169. addHm: isSiteMode(),
  170. // 使用cdn打包
  171. // TODO Cdn esm使用方式需要只能支持google,暂时关闭,后续查询更好的方式
  172. useCdn: false,
  173. // useCdn: VITE_USE_CDN,
  174. // cdn列表
  175. cdnConf,
  176. minify: {
  177. enable: true,
  178. removeComments: true,
  179. collapseWhitespace: true,
  180. minifyJS: true,
  181. minifyCSS: true,
  182. },
  183. };
  184. export default {
  185. ...viteConfig,
  186. transforms: [globbyTransform(viteConfig)],
  187. } as UserConfig;