Skip to content

Commit

Permalink
feat(helper): add hasGlobalComponent (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope authored Feb 1, 2024
1 parent 247c792 commit 2472fce
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions themes/theme-default/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*",
Expand Down
8 changes: 5 additions & 3 deletions themes/theme-default/src/client/config.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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', () => {
Expand Down
15 changes: 15 additions & 0 deletions tools/helper/src/client/utils/hasGlobalComponent.ts
Original file line number Diff line number Diff line change
@@ -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
)
}
1 change: 1 addition & 0 deletions tools/helper/src/client/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './data.js'
export * from './hasGlobalComponent.js'

0 comments on commit 2472fce

Please sign in to comment.