vite.config.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. * Entry. Use this to specify a js entry file in use cases where an
  55. * `index.html` does not exist (e.g. serving vite assets from a different host)
  56. * @default 'index.html'
  57. */
  58. entry: 'public/index.html',
  59. /**
  60. * 端口号
  61. * @default '3000'
  62. */
  63. port: VITE_PORT,
  64. /**
  65. * 服务地址
  66. * @default 'localhost'
  67. */
  68. hostname: 'localhost',
  69. /**
  70. * 运行自动打开浏览器·
  71. * @default 'false'
  72. */
  73. open: false,
  74. /**
  75. * 压缩代码
  76. * boolean | 'terser' | 'esbuild'
  77. * @default 'terser'
  78. */
  79. minify: isDevFn() ? 'esbuild' : 'terser',
  80. /**
  81. * 基本公共路径
  82. * @default '/'
  83. */
  84. base: VITE_PUBLIC_PATH,
  85. /**
  86. * 打包输入路径
  87. * @default 'dist'
  88. */
  89. outDir: 'dist',
  90. /**
  91. * @default 'false'
  92. */
  93. sourcemap: false,
  94. /**
  95. * 资源输出路径
  96. * @default '_assets'
  97. */
  98. assetsDir: '_assets',
  99. /**
  100. * 静态资源小于该大小将会内联,默认4096kb
  101. * @default '4096'
  102. */
  103. assetsInlineLimit: 4096,
  104. /**
  105. * esbuild转换目标。
  106. * @default 'es2020'
  107. */
  108. esbuildTarget: 'es2020',
  109. silent: false,
  110. // 别名
  111. alias: {
  112. '/@/': pathResolve('src'),
  113. },
  114. // terser配置
  115. terserOptions: {
  116. compress: {
  117. // 是否删除console
  118. drop_console: VITE_DROP_CONSOLE,
  119. },
  120. },
  121. define: {
  122. __VERSION__: pkg.version,
  123. },
  124. // css预处理
  125. cssPreprocessOptions: {
  126. less: {
  127. modifyVars: modifyVars,
  128. javascriptEnabled: true,
  129. },
  130. },
  131. // 会使用 rollup 对 包重新编译,将编译成符合 esm 模块规范的新的包放入 node_modules/.vite_opt_cache
  132. optimizeDeps: {
  133. include: [
  134. 'echarts',
  135. 'echarts/map/js/china',
  136. 'ant-design-vue/es/locale/zh_CN',
  137. '@ant-design/icons-vue',
  138. 'moment/locale/zh-cn',
  139. ],
  140. },
  141. // 本地跨域代理
  142. proxy: createProxy(VITE_PROXY),
  143. plugins: [PurgeIcons(), ...vitePlugins],
  144. rollupOutputOptions: {},
  145. rollupInputOptions: {
  146. // TODO
  147. // external: VITE_USE_CDN ? externals : [],
  148. plugins: rollupPlugins,
  149. },
  150. };
  151. // 扩展配置, 往打包后的html注入内容
  152. // 只针对生产环境
  153. // TODO 目前只是简单手动注入实现,后续vite应该会提供配置项
  154. export const htmlConfig: {
  155. title: string;
  156. addHm?: boolean;
  157. cdnConf?: {
  158. css?: string[];
  159. js?: string[];
  160. };
  161. useCdn: boolean;
  162. minify: {
  163. enable: boolean;
  164. removeComments: boolean;
  165. collapseWhitespace: boolean;
  166. minifyJS: boolean;
  167. minifyCSS: boolean;
  168. };
  169. } = {
  170. // html title
  171. title: VITE_GLOB_APP_TITLE,
  172. // 百度统计,不需要可以删除
  173. // 用于打包部署站点使用。实际项目可以删除
  174. addHm: isSiteMode(),
  175. // 使用cdn打包
  176. // TODO Cdn esm使用方式需要只能支持google,暂时关闭,后续查询更好的方式
  177. useCdn: false,
  178. // useCdn: VITE_USE_CDN,
  179. // cdn列表
  180. cdnConf,
  181. minify: {
  182. enable: true,
  183. removeComments: true,
  184. collapseWhitespace: true,
  185. minifyJS: true,
  186. minifyCSS: true,
  187. },
  188. };
  189. export default {
  190. ...viteConfig,
  191. transforms: [globbyTransform(viteConfig)],
  192. } as UserConfig;