plugins.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { cdn } from "./cdn";
  2. import vue from "@vitejs/plugin-vue";
  3. import { viteBuildInfo } from "./info";
  4. import svgLoader from "vite-svg-loader";
  5. import type { PluginOption } from "vite";
  6. import vueJsx from "@vitejs/plugin-vue-jsx";
  7. import { configCompressPlugin } from "./compress";
  8. import removeNoMatch from "vite-plugin-router-warn";
  9. import { visualizer } from "rollup-plugin-visualizer";
  10. import removeConsole from "vite-plugin-remove-console";
  11. import { codeInspectorPlugin } from "code-inspector-plugin";
  12. import { vitePluginFakeServer } from "vite-plugin-fake-server";
  13. export function getPluginsList(
  14. VITE_CDN: boolean,
  15. VITE_COMPRESSION: ViteCompression
  16. ): PluginOption[] {
  17. const lifecycle = process.env.npm_lifecycle_event;
  18. return [
  19. vue(),
  20. // jsx、tsx语法支持
  21. vueJsx(),
  22. /**
  23. * 在页面上按住组合键时,鼠标在页面移动即会在 DOM 上出现遮罩层并显示相关信息,点击一下将自动打开 IDE 并将光标定位到元素对应的代码位置
  24. * Mac 默认组合键 Option + Shift
  25. * Windows 默认组合键 Alt + Shift
  26. * 更多用法看 https://inspector.fe-dev.cn/guide/start.html
  27. */
  28. codeInspectorPlugin({
  29. bundler: "vite",
  30. hideConsole: true
  31. }),
  32. viteBuildInfo(),
  33. /**
  34. * 开发环境下移除非必要的vue-router动态路由警告No match found for location with path
  35. * 非必要具体看 https://github.com/vuejs/router/issues/521 和 https://github.com/vuejs/router/issues/359
  36. * vite-plugin-router-warn只在开发环境下启用,只处理vue-router文件并且只在服务启动或重启时运行一次,性能消耗可忽略不计
  37. */
  38. removeNoMatch(),
  39. // mock支持
  40. vitePluginFakeServer({
  41. logger: false,
  42. include: "mock",
  43. infixName: false,
  44. enableProd: true
  45. }),
  46. // svg组件化支持
  47. svgLoader(),
  48. VITE_CDN ? cdn : null,
  49. configCompressPlugin(VITE_COMPRESSION),
  50. // 线上环境删除console
  51. removeConsole({ external: ["src/assets/iconfont/iconfont.js"] }),
  52. // 打包分析
  53. lifecycle === "report"
  54. ? visualizer({ open: true, brotliSize: true, filename: "report.html" })
  55. : (null as any)
  56. ];
  57. }