Skip to content

Commit 9872740

Browse files
committed
fix(module): scan layers when using component detection
Resolves #5389
1 parent df1bbe9 commit 9872740

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

src/templates.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,13 @@ export function getTemplates(options: ModuleOptions, uiConfig: Record<string, an
113113
let sources = ''
114114

115115
if (!!nuxt && !!resolve && options.experimental?.componentDetection) {
116-
const detectedComponents = await detectUsedComponents(
116+
const dirs = [...new Set([
117117
nuxt.options.rootDir,
118+
...(nuxt.options._layers?.map(layer => layer.config.rootDir).filter(Boolean) || [])
119+
])]
120+
121+
const detectedComponents = await detectUsedComponents(
122+
dirs,
118123
options.prefix!,
119124
resolve('./runtime/components'),
120125
Array.isArray(options.experimental.componentDetection) ? options.experimental.componentDetection : undefined

src/utils/components.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function resolveComponentDependencies(
6565
* Detect components used in the project by scanning source files
6666
*/
6767
export async function detectUsedComponents(
68-
rootDir: string,
68+
dirs: string[],
6969
prefix: string,
7070
componentDir: string,
7171
includeComponents?: string[]
@@ -79,33 +79,35 @@ export async function detectUsedComponents(
7979
}
8080
}
8181

82-
// Scan all source files for component usage
83-
const appFiles = globSync(['**/*.{vue,ts,js,tsx,jsx}'], {
84-
cwd: rootDir,
85-
ignore: ['node_modules/**', '.nuxt/**', 'dist/**']
86-
})
87-
8882
// Pattern to match:
8983
// - <UButton in templates
9084
// - UButton in script (imports, usage)
9185
// - <LazyUButton (lazy components)
9286
// - LazyUButton in script
9387
const componentPattern = new RegExp(`<(?:Lazy)?${prefix}([A-Z][a-zA-Z]+)|\\b(?:Lazy)?${prefix}([A-Z][a-zA-Z]+)\\b`, 'g')
9488

95-
for (const file of appFiles) {
96-
try {
97-
const filePath = join(rootDir, file)
98-
const content = await readFile(filePath, 'utf-8')
99-
const matches = content.matchAll(componentPattern)
100-
101-
for (const match of matches) {
102-
const componentName = match[1] || match[2]
103-
if (componentName) {
104-
detectedComponents.add(componentName)
89+
// Scan all source files for component usage across all layers
90+
for (const dir of dirs) {
91+
const appFiles = globSync(['**/*.{vue,ts,js,tsx,jsx}'], {
92+
cwd: dir,
93+
ignore: ['node_modules/**', '.nuxt/**', 'dist/**']
94+
})
95+
96+
for (const file of appFiles) {
97+
try {
98+
const filePath = join(dir, file)
99+
const content = await readFile(filePath, 'utf-8')
100+
const matches = content.matchAll(componentPattern)
101+
102+
for (const match of matches) {
103+
const componentName = match[1] || match[2]
104+
if (componentName) {
105+
detectedComponents.add(componentName)
106+
}
105107
}
108+
} catch {
109+
// Ignore files that can't be read
106110
}
107-
} catch {
108-
// Ignore files that can't be read
109111
}
110112
}
111113

0 commit comments

Comments
 (0)