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..5409385d2d 100644 --- a/packages/core/@types/index.d.ts +++ b/packages/core/@types/index.d.ts @@ -569,8 +569,10 @@ export function toPureObject (obj: T): T declare type PluginInstallFunction = (app: Mpx, ...options: any[]) => any -export type Plugin = PluginInstallFunction | { +export type Plugin = (PluginInstallFunction | { install: PluginInstallFunction +}) & { + __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 }