From abc6b942b4a65ae49c5c579630cce079ad7c4367 Mon Sep 17 00:00:00 2001 From: xuegan Date: Mon, 3 Aug 2026 11:46:02 +0800 Subject: [PATCH] feat(core): mark installed plugins --- docs-vitepress/api/global-api.md | 2 ++ packages/core/@types/index.d.ts | 9 +++++-- packages/core/__tests__/common/use.spec.js | 29 ++++++++++++++++++++++ packages/core/src/index.js | 1 + 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 packages/core/__tests__/common/use.spec.js diff --git a/docs-vitepress/api/global-api.md b/docs-vitepress/api/global-api.md index 66f0d4c4fe..3b6094b9f0 100644 --- a/docs-vitepress/api/global-api.md +++ b/docs-vitepress/api/global-api.md @@ -100,6 +100,8 @@ mpx.use(test) mpx.use(test, {prefix: 'mpx'}, 'otherparams') ``` +插件成功安装后,Mpx 会将插件对象或函数的 `__installed` 属性设置为 `true`,外部代码可以通过该标记判断插件是否已经安装。Mpx 内部仍使用独立的插件列表避免重复安装。 + ### watch watch 可以通过全局实例访问,也可以使用具名导出的方式,二者逻辑相同,我们推荐使用具名导出的方式。[查看详情](reactivity-api/computed-watch-api.html#watch) diff --git a/packages/core/@types/index.d.ts b/packages/core/@types/index.d.ts index 22e2a6e7f6..c79db013c4 100644 --- a/packages/core/@types/index.d.ts +++ b/packages/core/@types/index.d.ts @@ -567,10 +567,15 @@ interface ImplementOptions { export function toPureObject (obj: T): T -declare type PluginInstallFunction = (app: Mpx, ...options: any[]) => any +declare type PluginInstallHandler = (app: Mpx, ...options: any[]) => any + +declare type PluginInstallFunction = PluginInstallHandler & { + __installed?: boolean +} export type Plugin = PluginInstallFunction | { - install: PluginInstallFunction + install: PluginInstallHandler + __installed?: boolean } export type PluginFunction = T extends PluginInstallFunction ? T : T extends { install: infer U } ? U : never diff --git a/packages/core/__tests__/common/use.spec.js b/packages/core/__tests__/common/use.spec.js new file mode 100644 index 0000000000..0615b95fc4 --- /dev/null +++ b/packages/core/__tests__/common/use.spec.js @@ -0,0 +1,29 @@ +global.__mpx_mode__ = 'wx' +global.mpxGlobal = {} +global.wx = {} + +jest.mock('@mpxjs/perf', () => ({}), { virtual: true }) + +const Mpx = require('../../src').default + +describe('Mpx.use', () => { + it('should mark an object plugin as installed', () => { + const plugin = { + install: jest.fn() + } + + Mpx.use(plugin) + + expect(plugin.install).toHaveBeenCalledTimes(1) + expect(plugin.__installed).toBe(true) + }) + + it('should mark a function plugin as installed', () => { + const plugin = jest.fn() + + Mpx.use(plugin) + + expect(plugin).toHaveBeenCalledTimes(1) + expect(plugin.__installed).toBe(true) + }) +}) diff --git a/packages/core/src/index.js b/packages/core/src/index.js index 830dd60e0a..430a7bf0f4 100644 --- a/packages/core/src/index.js +++ b/packages/core/src/index.js @@ -110,6 +110,7 @@ function use (plugin, options = {}) { extendProps(Mpx, proxyMpx, rawProps, options) extendProps(Mpx.prototype, proxyMpx.prototype, rawPrototypeProps, options) installedPlugins.push(plugin) + plugin.__installed = true return this }