Skip to content

Commit 6e70ad3

Browse files
authored
feat: add flag for global components (#27)
1 parent 60cfbaf commit 6e70ad3

File tree

6 files changed

+45
-1
lines changed

6 files changed

+45
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div>I am global</div>
3+
</template>

playground/nuxt.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { defineNuxtConfig } from 'nuxt'
22
import nuxtMetaModule from '../src/module'
33

44
export default defineNuxtConfig({
5+
components: {
6+
dirs: [
7+
{
8+
path: '~/components/global',
9+
global: true
10+
},
11+
'~/components'
12+
]
13+
},
514
modules: [
615
nuxtMetaModule
716
]

src/module.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ export default defineNuxtModule<ModuleOptions>({
2020
const path = resolveModule((component as any).filePath, { paths: nuxt.options.rootDir })
2121
const source = await readFile(path, { encoding: 'utf-8' })
2222

23-
return parseComponent(name, source)
23+
const { props, slots } = parseComponent(name, source)
24+
return {
25+
name,
26+
global: Boolean(component.global),
27+
props,
28+
slots
29+
}
2430
})
2531
)
2632
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div>I am global</div>
3+
</template>

test/fixtures/basic/nuxt.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { defineNuxtConfig } from 'nuxt'
22
import nuxtMetaModule from '../../..'
33

44
export default defineNuxtConfig({
5+
components: {
6+
dirs: [
7+
{
8+
path: '~/components/global',
9+
global: true
10+
},
11+
'~/components'
12+
]
13+
},
514
modules: [
615
nuxtMetaModule
716
]

test/module.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,18 @@ describe('fixtures:basic', async () => {
3939
name: 'hello'
4040
}])
4141
})
42+
43+
test('Global component', async () => {
44+
const component = await $fetch('/api/component-meta/TestGlobalComponent')
45+
46+
expect(component).ownProperty('name')
47+
expect(component).ownProperty('props')
48+
expect(Array.isArray(component.props)).toBeTruthy()
49+
expect(component).ownProperty('slots')
50+
expect(Array.isArray(component.slots)).toBeTruthy()
51+
52+
expect(component).toMatchObject({
53+
global: true
54+
})
55+
})
4256
})

0 commit comments

Comments
 (0)