plugins.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 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 { codeInspectorPlugin } from "code-inspector-plugin";
  14. // import { vitePluginFakeServer } from "vite-plugin-fake-server";
  15. import vitePluginRequire from "vite-plugin-require"; // 确保导入路径正确
  16. export function getPluginsList(
  17. VITE_CDN: boolean,
  18. VITE_COMPRESSION: ViteCompression
  19. ): PluginOption[] {
  20. const lifecycle = process.env.npm_lifecycle_event;
  21. return [
  22. tailwindcss(),
  23. vue(),
  24. // jsx、tsx语法支持
  25. vueJsx(),
  26. /**
  27. * 在页面上按住组合键时,鼠标在页面移动即会在 DOM 上出现遮罩层并显示相关信息,点击一下将自动打开 IDE 并将光标定位到元素对应的代码位置
  28. * Mac 默认组合键 Option + Shift
  29. * Windows 默认组合键 Alt + Shift
  30. * 更多用法看 https://inspector.fe-dev.cn/guide/start.html
  31. */
  32. codeInspectorPlugin({
  33. bundler: "vite",
  34. hideConsole: true
  35. }),
  36. // viteBuildInfo(),
  37. /**
  38. * 开发环境下移除非必要的vue-router动态路由警告No match found for location with path
  39. * 非必要具体看 https://github.com/vuejs/router/issues/521 和 https://github.com/vuejs/router/issues/359
  40. * vite-plugin-router-warn只在开发环境下启用,只处理vue-router文件并且只在服务启动或重启时运行一次,性能消耗可忽略不计
  41. */
  42. removeNoMatch(),
  43. // mock支持
  44. // vitePluginFakeServer({
  45. // logger: false,
  46. // include: "mock",
  47. // infixName: false,
  48. // enableProd: true
  49. // }),
  50. // svg组件化支持
  51. svgLoader(),
  52. // 自动按需加载图标
  53. Icons({
  54. compiler: "vue3",
  55. scale: 1
  56. }),
  57. VITE_CDN ? cdn : null,
  58. configCompressPlugin(VITE_COMPRESSION),
  59. // 线上环境删除console
  60. removeConsole({ external: ["src/assets/iconfont/iconfont.js"] }),
  61. // 打包分析
  62. lifecycle === "report"
  63. ? visualizer({ open: true, brotliSize: true, filename: "report.html" })
  64. : (null as any),
  65. vitePluginRequire.default({
  66. // 假设插件接受一个配置对象
  67. fileRegex:/(.jsx?|.tsx?|.vue)$/ ,
  68. translateType: 'import', // 将 require 转换为 import 语句
  69. // 其他可能的选项...
  70. })
  71. ];
  72. }