plugins.ts 2.6 KB

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