-
Notifications
You must be signed in to change notification settings - Fork 8
/
unplugin.ts
34 lines (31 loc) · 1.12 KB
/
unplugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { createUnplugin } from 'unplugin'
import { type ComponentMetaParser, useComponentMetaParser, type ComponentMetaParserOptions } from './parser'
type ComponentMetaUnpluginOptions = { parser?: ComponentMetaParser, parserOptions: ComponentMetaParserOptions }
export const metaPlugin = createUnplugin<ComponentMetaUnpluginOptions>(
({ parser, parserOptions }) => {
const instance = parser || useComponentMetaParser(parserOptions)
let _configResolved: any
return {
name: 'vite-plugin-nuxt-component-meta',
enforce: 'post',
async buildStart () {
// avoid parsing meta twice in SSR
if (_configResolved?.build.ssr) {
return
}
await instance.fetchComponents()
await instance.updateOutput()
},
vite: {
configResolved (config) {
_configResolved = config
},
async handleHotUpdate ({ file }) {
if (Object.entries(instance.components).some(([, comp]: any) => comp.fullPath === file)) {
await instance.fetchComponent(file)
await instance.updateOutput()
}
}
}
}
})