diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index edb82450a9..ce4666e811 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -454,6 +454,9 @@ importers: themes/theme-default: dependencies: + '@vuepress/helper': + specifier: workspace:* + version: link:../../tools/helper '@vuepress/plugin-active-header-links': specifier: workspace:* version: link:../../plugins/plugin-active-header-links diff --git a/themes/theme-default/package.json b/themes/theme-default/package.json index 2d80a4c6c6..cee6f3b720 100644 --- a/themes/theme-default/package.json +++ b/themes/theme-default/package.json @@ -42,6 +42,7 @@ "copy": "cpx \"src/**/*.{d.ts,vue,scss}\" lib" }, "dependencies": { + "@vuepress/helper": "workspace:*", "@vuepress/plugin-active-header-links": "workspace:*", "@vuepress/plugin-back-to-top": "workspace:*", "@vuepress/plugin-container": "workspace:*", diff --git a/themes/theme-default/src/client/config.ts b/themes/theme-default/src/client/config.ts index 053813b777..6aa0101d11 100644 --- a/themes/theme-default/src/client/config.ts +++ b/themes/theme-default/src/client/config.ts @@ -1,3 +1,4 @@ +import { hasGlobalComponent } from '@vuepress/helper/client' import { h } from 'vue' import { defineClientConfig } from 'vuepress/client' import type { ClientConfig } from 'vuepress/client' @@ -14,9 +15,10 @@ import './styles/index.scss' export default defineClientConfig({ enhance({ app, router }) { - app.component('Badge', Badge) - app.component('CodeGroup', CodeGroup) - app.component('CodeGroupItem', CodeGroupItem) + if (!hasGlobalComponent('Badge')) app.component('Badge', Badge) + if (!hasGlobalComponent('CodeGroup')) app.component('CodeGroup', CodeGroup) + if (!hasGlobalComponent('CodeGroupItem')) + app.component('CodeGroupItem', CodeGroupItem) // compat with @vuepress/plugin-external-link-icon app.component('AutoLinkExternalIcon', () => { diff --git a/tools/helper/src/client/utils/hasGlobalComponent.ts b/tools/helper/src/client/utils/hasGlobalComponent.ts new file mode 100644 index 0000000000..14574807df --- /dev/null +++ b/tools/helper/src/client/utils/hasGlobalComponent.ts @@ -0,0 +1,15 @@ +import type { App } from 'vue' +import { camelize, capitalize, getCurrentInstance } from 'vue' + +export const hasGlobalComponent = (name: string, app?: App): boolean => { + const globalComponents = (app?._instance || getCurrentInstance())?.appContext + .components + + if (!globalComponents) return false + + return ( + name in globalComponents || + camelize(name) in globalComponents || + capitalize(camelize(name)) in globalComponents + ) +} diff --git a/tools/helper/src/client/utils/index.ts b/tools/helper/src/client/utils/index.ts index b14b7762e1..1b1c857c56 100644 --- a/tools/helper/src/client/utils/index.ts +++ b/tools/helper/src/client/utils/index.ts @@ -1 +1,2 @@ export * from './data.js' +export * from './hasGlobalComponent.js'