1
- import { readFileSync , writeFileSync } from 'fs'
1
+ import fsp from 'fs/promises '
2
2
import { createUnplugin } from 'unplugin'
3
3
import { createComponentMetaCheckerByJsonConfig } from 'vue-component-meta'
4
4
import { resolveModule } from '@nuxt/kit'
5
5
import { join } from 'pathe'
6
- import { defu } from 'defu '
6
+ import { ModuleOptions } from './options '
7
7
8
- export const metaPlugin = createUnplugin < any > (
8
+ export const metaPlugin = createUnplugin < Required < ModuleOptions > > (
9
9
( options ) => {
10
10
const outputPath = join ( options . outputDir , 'component-meta' )
11
11
@@ -60,16 +60,16 @@ export const metaPlugin = createUnplugin<any>(
60
60
/**
61
61
* Output is needed for Nitro
62
62
*/
63
- const updateOutput = ( ) => {
64
- // Main export of comopnent datas
65
- writeFileSync (
63
+ const updateOutput = async ( ) => {
64
+ // Main export of component data
65
+ await fsp . writeFile (
66
66
outputPath + '.mjs' ,
67
67
getVirtualModuleContent ( ) ,
68
68
'utf-8'
69
69
)
70
70
}
71
71
72
- const fetchComponent = ( component : string | any ) => {
72
+ const fetchComponent = async ( component : string | any ) => {
73
73
try {
74
74
if ( typeof component === 'string' ) {
75
75
if ( components [ component ] ) {
@@ -88,7 +88,7 @@ export const metaPlugin = createUnplugin<any>(
88
88
if ( ! component ?. fullPath || ! component ?. pascalName ) { return }
89
89
90
90
// Read component code
91
- let code = readFileSync ( component . fullPath , 'utf-8' )
91
+ let code = await fsp . readFile ( component . fullPath , 'utf-8' )
92
92
93
93
// Support transformers
94
94
if ( options ?. transformers && options . transformers . length > 0 ) {
@@ -104,41 +104,29 @@ export const metaPlugin = createUnplugin<any>(
104
104
105
105
const { props, slots, events, exposed } = checker . getComponentMeta ( component . fullPath )
106
106
107
- component . meta . slots = [
108
- ...component . meta . slots ,
109
- ...slots
110
- ]
111
- component . meta . events = [
112
- ...component . meta . events ,
113
- ...events
114
- ]
115
- component . meta . exposed = [
116
- ...component . meta . exposed ,
117
- ...exposed
118
- ]
119
- component . meta . props = [
120
- ...component . meta . props ,
121
- ...props
122
- . filter ( prop => ! prop . global )
123
- . sort ( ( a , b ) => {
124
- // sort required properties first
125
- if ( ! a . required && b . required ) {
126
- return 1
127
- }
128
- if ( a . required && ! b . required ) {
129
- return - 1
130
- }
131
- // then ensure boolean properties are sorted last
132
- if ( a . type === 'boolean' && b . type !== 'boolean' ) {
133
- return 1
134
- }
135
- if ( a . type !== 'boolean' && b . type === 'boolean' ) {
136
- return - 1
137
- }
138
-
139
- return 0
140
- } )
141
- ]
107
+ component . meta . slots = slots
108
+ component . meta . events = events
109
+ component . meta . exposed = exposed
110
+ component . meta . props = props
111
+ . filter ( prop => ! prop . global )
112
+ . sort ( ( a , b ) => {
113
+ // sort required properties first
114
+ if ( ! a . required && b . required ) {
115
+ return 1
116
+ }
117
+ if ( a . required && ! b . required ) {
118
+ return - 1
119
+ }
120
+ // then ensure boolean properties are sorted last
121
+ if ( a . type === 'boolean' && b . type !== 'boolean' ) {
122
+ return 1
123
+ }
124
+ if ( a . type !== 'boolean' && b . type === 'boolean' ) {
125
+ return - 1
126
+ }
127
+
128
+ return 0
129
+ } )
142
130
143
131
components [ component . pascalName ] = component
144
132
} catch ( e ) {
@@ -147,22 +135,20 @@ export const metaPlugin = createUnplugin<any>(
147
135
}
148
136
}
149
137
150
- const fetchComponents = ( ) => Object . values ( components ) . forEach ( fetchComponent )
151
-
152
- fetchComponents ( )
153
-
154
- updateOutput ( )
138
+ const fetchComponents = ( ) => Promise . all ( Object . values ( components ) . map ( fetchComponent ) )
155
139
156
140
return {
157
141
name : 'vite-plugin-nuxt-component-meta' ,
158
-
159
142
enforce : 'post' ,
160
-
143
+ async buildStart ( ) {
144
+ await fetchComponents ( )
145
+ await updateOutput ( )
146
+ } ,
161
147
vite : {
162
- handleHotUpdate ( { file } ) {
148
+ async handleHotUpdate ( { file } ) {
163
149
if ( Object . entries ( components ) . some ( ( [ , comp ] : any ) => comp . fullPath === file ) ) {
164
- fetchComponent ( file )
165
- updateOutput ( )
150
+ await fetchComponent ( file )
151
+ await updateOutput ( )
166
152
}
167
153
}
168
154
}
0 commit comments