1
1
import fs from 'node:fs/promises'
2
2
import type { Nuxt } from 'nuxt/schema'
3
3
import { glob } from 'tinyglobby'
4
- import { iconMatchRegex } from './icon-regex'
5
4
import type { ClientBundleScanOptions } from './types'
5
+ import { collectionNames } from './collection-names'
6
6
7
- export function extraIconUsages ( code : string , set : Set < string > , ignoreCollections : string [ ] ) {
8
- for ( const match of code . matchAll ( iconMatchRegex ) ) {
9
- if ( match && ! ignoreCollections . includes ( match [ 1 ] ) ) {
7
+ export function extraIconUsages (
8
+ code : string ,
9
+ set : Set < string > ,
10
+ matchRegex : RegExp ,
11
+ ) {
12
+ for ( const match of code . matchAll ( matchRegex ) ) {
13
+ if ( match ) {
10
14
set . add ( `${ match [ 1 ] } :${ match [ 2 ] } ` )
11
15
}
12
16
}
13
17
}
14
18
15
- export async function scanSourceFiles ( nuxt : Nuxt , scanOptions : ClientBundleScanOptions | true , set : Set < string > = new Set ( ) ) {
19
+ export function createMatchRegex (
20
+ collections : string [ ] | Set < string > ,
21
+ ) {
22
+ const collectionsRegex = [ ...collections ] . sort ( ( a , b ) => b . length - a . length ) . join ( '|' )
23
+ return new RegExp ( '\\b(?:i-)?(' + collectionsRegex + ')[:-]([a-z0-9-]+)\\b' , 'g' )
24
+ }
25
+
26
+ export async function scanSourceFiles (
27
+ nuxt : Nuxt ,
28
+ scanOptions : ClientBundleScanOptions | true ,
29
+ set : Set < string > = new Set ( ) ,
30
+ ) {
16
31
const {
17
32
globInclude = [ '**/*.{vue,jsx,tsx,md,mdc,mdx,yml,yaml}' ] ,
18
33
globExclude = [ 'node_modules' , 'dist' , 'build' , 'coverage' , 'test' , 'tests' , '.*' ] ,
19
34
ignoreCollections = [ ] ,
35
+ additionalCollections = [ ] ,
20
36
} = scanOptions === true ? { } : scanOptions
21
37
38
+ const collections = new Set ( [
39
+ ...collectionNames ,
40
+ ...additionalCollections ,
41
+ ] )
42
+ for ( const collection of ignoreCollections ) {
43
+ collections . delete ( collection )
44
+ }
45
+
46
+ const matchRegex = createMatchRegex ( collections )
47
+
22
48
const files = await glob (
23
49
globInclude ,
24
50
{
@@ -32,7 +58,7 @@ export async function scanSourceFiles(nuxt: Nuxt, scanOptions: ClientBundleScanO
32
58
await Promise . all (
33
59
files . map ( async ( file ) => {
34
60
const code = await fs . readFile ( file , 'utf-8' ) . catch ( ( ) => '' )
35
- extraIconUsages ( code , set , ignoreCollections )
61
+ extraIconUsages ( code , set , matchRegex )
36
62
} ) ,
37
63
)
38
64
0 commit comments