Skip to content

Commit 850dde6

Browse files
committed
feat: add parseAtBuild option
1 parent 2d3ee3d commit 850dde6

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

src/module.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ export default defineNuxtModule<ModuleOptions>({
105105
}
106106
},
107107
globalsOnly: false,
108-
onlyParseMetas: false
108+
onlyParseMetas: false,
109+
parseAtBuild: false
109110
}),
110111
async setup (options, nuxt) {
111112
const resolver = createResolver(import.meta.url)
112113

113114
const isComponentIncluded = (component: any) => {
114115
if (!options?.globalsOnly) { return true }
115-
116116
if (component.global) { return true }
117117

118118
return (options.include || []).find((excludeRule) => {
@@ -187,6 +187,13 @@ export default defineNuxtModule<ModuleOptions>({
187187
parser.init(),
188188
parser.stubOutput(),
189189
])
190+
191+
if (options.parseAtBuild) {
192+
// Parse components now so other modules can access metadata during setup
193+
parser.fetchComponents()
194+
nuxt.callHook('component-meta:parsed', parser.components)
195+
parser.updateOutput()
196+
}
190197
})
191198

192199
if (!options.onlyParseMetas) {
@@ -217,7 +224,7 @@ export default defineNuxtModule<ModuleOptions>({
217224
// Vite plugin
218225
nuxt.hook('vite:extend', (vite: any) => {
219226
vite.config.plugins = vite.config.plugins || []
220-
vite.config.plugins.push(metaPlugin.vite({ nuxt, parser, parserOptions }))
227+
vite.config.plugins.push(metaPlugin.vite({ nuxt, parser, parserOptions, parseAtBuild: options.parseAtBuild }))
221228
})
222229

223230
// Inject output alias

src/options.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ export interface ModuleOptions {
7171
* Disable anything that is not related to the component meta parsing.
7272
*/
7373
onlyParseMetas?: boolean
74+
/**
75+
* Whether to parse component meta at build time.
76+
*
77+
* If false, it will only parse at runtime.
78+
*/
79+
parseAtBuild?: boolean
7480
}
7581

7682
export interface ModuleHooks {

src/unplugin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { createUnplugin } from 'unplugin'
22
import { type ComponentMetaParser, useComponentMetaParser, type ComponentMetaParserOptions } from './parser'
33
import type { Nuxt } from 'nuxt/schema'
44

5-
type ComponentMetaUnpluginOptions = { nuxt: Nuxt, parser?: ComponentMetaParser, parserOptions: ComponentMetaParserOptions }
5+
type ComponentMetaUnpluginOptions = { nuxt: Nuxt, parser?: ComponentMetaParser, parserOptions: ComponentMetaParserOptions, parseAtBuild?: boolean }
66

77
// @ts-ignore -- arguments types are not correct
8-
export const metaPlugin = createUnplugin<ComponentMetaUnpluginOptions>(({ nuxt, parser, parserOptions }) => {
8+
export const metaPlugin = createUnplugin<ComponentMetaUnpluginOptions>(({ nuxt, parser, parserOptions, parseAtBuild }) => {
99
const instance = parser || useComponentMetaParser(parserOptions)
1010
let _configResolved: any
1111

@@ -14,7 +14,7 @@ export const metaPlugin = createUnplugin<ComponentMetaUnpluginOptions>(({ nuxt,
1414
enforce: 'post',
1515
buildStart () {
1616
// avoid parsing meta twice in SSR
17-
if (_configResolved?.build.ssr) {
17+
if (_configResolved?.build.ssr || parseAtBuild) {
1818
return
1919
}
2020

0 commit comments

Comments
 (0)