Skip to content

Commit ecbc09a

Browse files
authored
fix(module): await component registration (#1398)
1 parent d90434f commit ecbc09a

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

packages/module/src/module.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,35 +54,34 @@ export default defineNuxtModule<ModuleOptions>({
5454

5555
// Manually scan `componentsDir` for components and register them for auto imports
5656
try {
57-
readdirSync(componentsPath)
58-
.forEach(async (dir) => {
59-
try {
60-
const filePath = await resolvePath(join(COMPONENT_DIR_PATH, dir, 'index'), { extensions: ['.ts', '.js'] })
61-
const content = readFileSync(filePath, { encoding: 'utf8' })
62-
const ast = parseSync(filePath, content, {
63-
sourceType: 'module',
64-
})
57+
await Promise.all(readdirSync(componentsPath).map(async (dir) => {
58+
try {
59+
const filePath = await resolvePath(join(COMPONENT_DIR_PATH, dir, 'index'), { extensions: ['.ts', '.js'] })
60+
const content = readFileSync(filePath, { encoding: 'utf8' })
61+
const ast = parseSync(filePath, content, {
62+
sourceType: 'module',
63+
})
6564

66-
const exportedKeys: string[] = ast.program.body
67-
.filter(node => node.type === 'ExportNamedDeclaration')
68-
// @ts-expect-error parse return any
69-
.flatMap(node => node.specifiers?.map(specifier => specifier.exported?.name) || [])
70-
.filter((key: string) => /^[A-Z]/.test(key))
65+
const exportedKeys: string[] = ast.program.body
66+
.filter(node => node.type === 'ExportNamedDeclaration')
67+
// @ts-expect-error parse return any
68+
.flatMap(node => node.specifiers?.map(specifier => specifier.exported?.name) || [])
69+
.filter((key: string) => /^[A-Z]/.test(key))
7170

72-
exportedKeys.forEach((key) => {
73-
addComponent({
74-
name: `${prefix}${key}`, // name of the component to be used in vue templates
75-
export: key, // (optional) if the component is a named (rather than default) export
76-
filePath: resolve(filePath),
77-
priority: 1,
78-
})
71+
exportedKeys.forEach((key) => {
72+
addComponent({
73+
name: `${prefix}${key}`, // name of the component to be used in vue templates
74+
export: key, // (optional) if the component is a named (rather than default) export
75+
filePath: resolve(filePath),
76+
priority: 1,
7977
})
80-
}
81-
catch (err) {
82-
if (err instanceof Error)
83-
console.warn('Module error: ', err.message)
84-
}
85-
})
78+
})
79+
}
80+
catch (err) {
81+
if (err instanceof Error)
82+
console.warn('Module error: ', err.message)
83+
}
84+
}))
8685
}
8786
catch (err) {
8887
if (err instanceof Error)

0 commit comments

Comments
 (0)