vite.config.ts 4.5 KB

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