Skip to content

Commit e0cf57d

Browse files
feat(module): add component-meta:schema hook (#95)
1 parent e3eb2c4 commit e0cf57d

File tree

6 files changed

+23
-11
lines changed

6 files changed

+23
-11
lines changed

playground/pages/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div>
33
<div>
4-
<TestComponent foo="test" />
4+
<TestComponent foo="test" name="test" />
55
<TestGlobalComponent />
66
<TestTyped :hello="`test`" />
77
</div>

src/module.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export default defineNuxtModule<ModuleOptions>({
8686
const slots = slotNames
8787
.trim()
8888
.split(',')
89-
.map(s => s.trim().split(':')[0].trim())
89+
.map(s => s.trim().split(':')[0]?.trim())
9090
.map(s => `<slot name="${s}" />`)
9191
code = code.replace(/<template>/, `<template>\n${slots.join('\n')}\n`)
9292
}
@@ -137,7 +137,10 @@ export default defineNuxtModule<ModuleOptions>({
137137
...options,
138138
components: [],
139139
metaSources: {},
140-
transformers
140+
transformers,
141+
beforeWrite: async (schema: NuxtComponentMeta) => {
142+
return await nuxt.callHook('component-meta:schema' as any, schema) || schema
143+
}
141144
}
142145

143146
// Resolve loaded components

src/parser/meta-parser.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import type { ComponentMetaParserOptions, NuxtComponentMeta } from '../types/par
99
import { defu } from 'defu'
1010
import { refineMeta } from './utils'
1111

12-
1312
export function useComponentMetaParser (
1413
{
1514
outputDir = join(process.cwd(), '.component-meta/'),
@@ -21,7 +20,8 @@ export function useComponentMetaParser (
2120
transformers = [],
2221
debug = false,
2322
metaFields,
24-
metaSources = {}
23+
metaSources = {},
24+
beforeWrite
2525
}: ComponentMetaParserOptions
2626
) {
2727
/**
@@ -116,8 +116,14 @@ export function useComponentMetaParser (
116116
/**
117117
* Write the output file.
118118
*/
119-
const updateOutput = (content?: string) => {
119+
const updateOutput = async (content?: string) => {
120120
const path = outputPath + '.mjs'
121+
122+
// Call beforeWrite hook if provided
123+
if (beforeWrite && !content) {
124+
components = await beforeWrite(components)
125+
}
126+
121127
if (!existsSync(dirname(path))) { fs.mkdirSync(dirname(path), { recursive: true }) }
122128
if (existsSync(path)) { fs.unlinkSync(path) }
123129
fs.writeFileSync(

src/types/module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,5 @@ export interface ModuleOptions {
7272
export interface ModuleHooks {
7373
'component-meta:transformers'(data: TransformersHookData): void
7474
'component-meta:extend'(data: ExtendHookData): void
75+
'component-meta:schema'(schema: NuxtComponentMeta): NuxtComponentMeta | Promise<NuxtComponentMeta>
7576
}

src/types/parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ModuleOptions } from './module'
55
export type ComponentMetaParserOptions = Omit<ModuleOptions, 'components' | 'metaSources'> & {
66
components: Component[]
77
metaSources?: NuxtComponentMeta
8+
beforeWrite?: (schema: NuxtComponentMeta) => Promise<NuxtComponentMeta> | NuxtComponentMeta
89
}
910
export type ComponentData = Omit<Component, 'filePath' | 'shortPath'> & {
1011
meta: ComponentMeta

src/utils/unplugin.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createUnplugin } from 'unplugin'
22
import { useComponentMetaParser } from '../parser/meta-parser'
3-
import type { ComponentMetaParser, ComponentMetaParserOptions } from '../parser/meta-parser';
3+
import type { ComponentMetaParser } from '../parser/meta-parser'
4+
import type { ComponentMetaParserOptions } from '../types/parser'
45

56
type ComponentMetaUnpluginOptions = { parser?: ComponentMetaParser, parserOptions: ComponentMetaParserOptions }
67

@@ -12,14 +13,14 @@ export const metaPlugin = createUnplugin<ComponentMetaUnpluginOptions>(({ parser
1213
return {
1314
name: 'vite-plugin-nuxt-component-meta',
1415
enforce: 'post',
15-
buildStart () {
16+
async buildStart () {
1617
// avoid parsing meta twice in SSR
1718
if (_configResolved?.build.ssr) {
1819
return
1920
}
2021

2122
instance?.fetchComponents()
22-
instance?.updateOutput()
23+
await instance?.updateOutput()
2324
},
2425
buildEnd () {
2526
if (!_configResolved?.env.DEV && _configResolved?.env.PROD) {
@@ -32,10 +33,10 @@ export const metaPlugin = createUnplugin<ComponentMetaUnpluginOptions>(({ parser
3233
configResolved (config) {
3334
_configResolved = config
3435
},
35-
handleHotUpdate ({ file }) {
36+
async handleHotUpdate ({ file }) {
3637
if (instance && Object.entries(instance.components).some(([, comp]: any) => comp.fullPath === file)) {
3738
instance.fetchComponent(file)
38-
instance.updateOutput()
39+
await instance.updateOutput()
3940
}
4041
}
4142
}

0 commit comments

Comments
 (0)