Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion playground/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div>
<div>
<TestComponent foo="test" />
<TestComponent foo="test" name="test" />
<TestGlobalComponent />
<TestTyped :hello="`test`" />
</div>
Expand Down
7 changes: 5 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default defineNuxtModule<ModuleOptions>({
const slots = slotNames
.trim()
.split(',')
.map(s => s.trim().split(':')[0].trim())
.map(s => s.trim().split(':')[0]?.trim())
.map(s => `<slot name="${s}" />`)
code = code.replace(/<template>/, `<template>\n${slots.join('\n')}\n`)
}
Expand Down Expand Up @@ -137,7 +137,10 @@ export default defineNuxtModule<ModuleOptions>({
...options,
components: [],
metaSources: {},
transformers
transformers,
beforeWrite: async (schema: NuxtComponentMeta) => {
return await nuxt.callHook('component-meta:schema' as any, schema) || schema
}
}

// Resolve loaded components
Expand Down
12 changes: 9 additions & 3 deletions src/parser/meta-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type { ComponentMetaParserOptions, NuxtComponentMeta } from '../types/par
import { defu } from 'defu'
import { refineMeta } from './utils'


export function useComponentMetaParser (
{
outputDir = join(process.cwd(), '.component-meta/'),
Expand All @@ -21,7 +20,8 @@ export function useComponentMetaParser (
transformers = [],
debug = false,
metaFields,
metaSources = {}
metaSources = {},
beforeWrite
}: ComponentMetaParserOptions
) {
/**
Expand Down Expand Up @@ -116,8 +116,14 @@ export function useComponentMetaParser (
/**
* Write the output file.
*/
const updateOutput = (content?: string) => {
const updateOutput = async (content?: string) => {
const path = outputPath + '.mjs'

// Call beforeWrite hook if provided
if (beforeWrite && !content) {
components = await beforeWrite(components)
}

if (!existsSync(dirname(path))) { fs.mkdirSync(dirname(path), { recursive: true }) }
if (existsSync(path)) { fs.unlinkSync(path) }
fs.writeFileSync(
Expand Down
1 change: 1 addition & 0 deletions src/types/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ export interface ModuleOptions {
export interface ModuleHooks {
'component-meta:transformers'(data: TransformersHookData): void
'component-meta:extend'(data: ExtendHookData): void
'component-meta:schema'(schema: NuxtComponentMeta): NuxtComponentMeta | Promise<NuxtComponentMeta>
}
1 change: 1 addition & 0 deletions src/types/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ModuleOptions } from './module'
export type ComponentMetaParserOptions = Omit<ModuleOptions, 'components' | 'metaSources'> & {
components: Component[]
metaSources?: NuxtComponentMeta
beforeWrite?: (schema: NuxtComponentMeta) => Promise<NuxtComponentMeta> | NuxtComponentMeta
}
export type ComponentData = Omit<Component, 'filePath' | 'shortPath'> & {
meta: ComponentMeta
Expand Down
11 changes: 6 additions & 5 deletions src/utils/unplugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createUnplugin } from 'unplugin'
import { useComponentMetaParser } from '../parser/meta-parser'
import type { ComponentMetaParser, ComponentMetaParserOptions } from '../parser/meta-parser';
import type { ComponentMetaParser } from '../parser/meta-parser'
import type { ComponentMetaParserOptions } from '../types/parser'

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

Expand All @@ -12,14 +13,14 @@ export const metaPlugin = createUnplugin<ComponentMetaUnpluginOptions>(({ parser
return {
name: 'vite-plugin-nuxt-component-meta',
enforce: 'post',
buildStart () {
async buildStart () {
// avoid parsing meta twice in SSR
if (_configResolved?.build.ssr) {
return
}

instance?.fetchComponents()
instance?.updateOutput()
await instance?.updateOutput()
},
buildEnd () {
if (!_configResolved?.env.DEV && _configResolved?.env.PROD) {
Expand All @@ -32,10 +33,10 @@ export const metaPlugin = createUnplugin<ComponentMetaUnpluginOptions>(({ parser
configResolved (config) {
_configResolved = config
},
handleHotUpdate ({ file }) {
async handleHotUpdate ({ file }) {
if (instance && Object.entries(instance.components).some(([, comp]: any) => comp.fullPath === file)) {
instance.fetchComponent(file)
instance.updateOutput()
await instance.updateOutput()
}
}
}
Expand Down
Loading