Skip to content

Commit 00ac278

Browse files
authored
perf: use fs/promises to improve the performance (#46)
1 parent 571f952 commit 00ac278

File tree

3 files changed

+60
-71
lines changed

3 files changed

+60
-71
lines changed

src/module.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { existsSync, readFileSync } from 'fs'
2-
import type { MetaCheckerOptions } from 'vue-component-meta'
32
import {
43
addServerHandler,
54
createResolver,
@@ -9,25 +8,12 @@ import {
98
addTemplate
109
} from '@nuxt/kit'
1110
import { join } from 'pathe'
12-
import type { ComponentsDir, ComponentsOptions } from '@nuxt/schema'
13-
import createJITI from 'jiti'
11+
import type { ComponentsDir } from '@nuxt/schema'
1412
import { withoutLeadingSlash } from 'ufo'
15-
import type { HookData } from './types'
1613
import { metaPlugin } from './unplugin'
14+
import { ModuleOptions } from './options'
1715

18-
export interface ModuleOptions {
19-
outputDir?: string
20-
rootDir?: string
21-
silent?: boolean
22-
componentDirs: (string | ComponentsDir)[]
23-
components?: ComponentsOptions[]
24-
checkerOptions?: MetaCheckerOptions
25-
transformers?: ((component: any, code: string) => ({ component: any, code: string }))[]
26-
}
27-
28-
export interface ModuleHooks {
29-
'component-meta:transformers'(data: HookData): void
30-
}
16+
export * from './options'
3117

3218
export default defineNuxtModule<ModuleOptions>({
3319
meta: {
@@ -115,7 +101,7 @@ export default defineNuxtModule<ModuleOptions>({
115101
// Vite plugin
116102
nuxt.hook('vite:extend', (vite: any) => {
117103
vite.config.plugins = vite.config.plugins || []
118-
vite.config.plugins.push(metaPlugin.vite(options))
104+
vite.config.plugins.push(metaPlugin.vite(options as Required<ModuleOptions>))
119105
})
120106

121107
// Inject output alias

src/options.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { MetaCheckerOptions } from 'vue-component-meta'
2+
import { ComponentsDir, ComponentsOptions } from '@nuxt/schema'
3+
import { HookData } from './types'
4+
5+
export interface ModuleOptions {
6+
outputDir?: string
7+
rootDir?: string
8+
silent?: boolean
9+
componentDirs: (string | ComponentsDir)[]
10+
components?: ComponentsOptions[]
11+
checkerOptions?: MetaCheckerOptions
12+
transformers?: ((component: any, code: string) => ({ component: any; code: string }))[]
13+
}
14+
15+
export interface ModuleHooks {
16+
'component-meta:transformers'(data: HookData): void
17+
}

src/unplugin.ts

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { readFileSync, writeFileSync } from 'fs'
1+
import fsp from 'fs/promises'
22
import { createUnplugin } from 'unplugin'
33
import { createComponentMetaCheckerByJsonConfig } from 'vue-component-meta'
44
import { resolveModule } from '@nuxt/kit'
55
import { join } from 'pathe'
6-
import { defu } from 'defu'
6+
import { ModuleOptions } from './options'
77

8-
export const metaPlugin = createUnplugin<any>(
8+
export const metaPlugin = createUnplugin<Required<ModuleOptions>>(
99
(options) => {
1010
const outputPath = join(options.outputDir, 'component-meta')
1111

@@ -60,16 +60,16 @@ export const metaPlugin = createUnplugin<any>(
6060
/**
6161
* Output is needed for Nitro
6262
*/
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(
6666
outputPath + '.mjs',
6767
getVirtualModuleContent(),
6868
'utf-8'
6969
)
7070
}
7171

72-
const fetchComponent = (component: string | any) => {
72+
const fetchComponent = async (component: string | any) => {
7373
try {
7474
if (typeof component === 'string') {
7575
if (components[component]) {
@@ -88,7 +88,7 @@ export const metaPlugin = createUnplugin<any>(
8888
if (!component?.fullPath || !component?.pascalName) { return }
8989

9090
// Read component code
91-
let code = readFileSync(component.fullPath, 'utf-8')
91+
let code = await fsp.readFile(component.fullPath, 'utf-8')
9292

9393
// Support transformers
9494
if (options?.transformers && options.transformers.length > 0) {
@@ -104,41 +104,29 @@ export const metaPlugin = createUnplugin<any>(
104104

105105
const { props, slots, events, exposed } = checker.getComponentMeta(component.fullPath)
106106

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+
})
142130

143131
components[component.pascalName] = component
144132
} catch (e) {
@@ -147,22 +135,20 @@ export const metaPlugin = createUnplugin<any>(
147135
}
148136
}
149137

150-
const fetchComponents = () => Object.values(components).forEach(fetchComponent)
151-
152-
fetchComponents()
153-
154-
updateOutput()
138+
const fetchComponents = () => Promise.all(Object.values(components).map(fetchComponent))
155139

156140
return {
157141
name: 'vite-plugin-nuxt-component-meta',
158-
159142
enforce: 'post',
160-
143+
async buildStart () {
144+
await fetchComponents()
145+
await updateOutput()
146+
},
161147
vite: {
162-
handleHotUpdate ({ file }) {
148+
async handleHotUpdate ({ file }) {
163149
if (Object.entries(components).some(([, comp]: any) => comp.fullPath === file)) {
164-
fetchComponent(file)
165-
updateOutput()
150+
await fetchComponent(file)
151+
await updateOutput()
166152
}
167153
}
168154
}

0 commit comments

Comments
 (0)