-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
detect-component-usage.ts
39 lines (34 loc) · 1.23 KB
/
detect-component-usage.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
35
36
37
38
39
import { createUnplugin } from 'unplugin'
import { isAbsolute, join, relative } from 'pathe'
interface DetectComponentUsageOptions {
rootDir: string
exclude?: Array<RegExp | string>
detectedComponents: Set<string>
}
export const DetectComponentUsagePlugin = (options: DetectComponentUsageOptions) => createUnplugin(() => {
const importersToExclude = options?.exclude || []
const detectComponentUsagePatterns: Array<[importPattern: string | RegExp, name: string]> = [
['../packages/nuxt/src/pages/runtime/page', 'NuxtPage'],
['../packages/nuxt/src/app/components/nuxt-layout', 'NuxtLayout']
]
return {
name: 'nuxt:detect-component-usage',
enforce: 'pre',
resolveId (id, importer) {
if (!importer) { return }
if (id[0] === '.') {
id = join(importer, '..', id)
}
if (isAbsolute(id)) {
id = relative(options.rootDir, id)
}
if (importersToExclude.some(p => typeof p === 'string' ? importer === p : p.test(importer))) { return }
for (const [pattern, name] of detectComponentUsagePatterns) {
if (pattern instanceof RegExp ? pattern.test(id) : pattern === id) {
options.detectedComponents.add(name)
}
}
return null
}
}
})