1- import { glob } from 'glob'
2- import { resolve , relative , dirname , extname , basename } from 'node:path'
3- import { readFile } from 'node:fs/promises'
41import type { GlobImportOptions , RobuildPlugin } from '../types'
2+ import { dirname , relative , resolve } from 'node:path'
3+ import { glob } from 'glob'
54
65/**
76 * Create a glob import plugin for robuild
@@ -11,36 +10,37 @@ export function createGlobImportPlugin(options: GlobImportOptions = {}): Robuild
1110 enabled = false ,
1211 patterns = [ '**/*' ] ,
1312 asUrls = false ,
14- eager = false
13+ eager = false ,
1514 } = options
1615
1716 if ( ! enabled ) {
1817 return {
19- name : 'glob-import-disabled'
18+ name : 'glob-import-disabled' ,
2019 }
2120 }
2221
2322 return {
2423 name : 'glob-import' ,
2524 transform : async ( code : string , id : string ) => {
2625 // Look for import.meta.glob() calls
27- const globImportRegex = / i m p o r t \. m e t a \. g l o b \s * \( \s * ( [ ' " ` ] ) ( .* ?) \1\s * (?: , \s * ( { [ ^ } ] * } ) ) ? \s * \) / g
28-
26+ const globImportRegex = / i m p o r t \. m e t a \. g l o b \s * \( \s * ( [ ' " ` ] ) ( .* ?) \1\s * (?: , \s * ( \ {[ ^ } ] * \} ) \s * ) ? \) / g
27+
2928 let match
3029 let hasGlobImports = false
3130 let transformedCode = code
3231
3332 while ( ( match = globImportRegex . exec ( code ) ) !== null ) {
3433 hasGlobImports = true
3534 const [ fullMatch , quote , pattern , optionsStr ] = match
36-
35+
3736 // Parse options if provided
3837 let globOptions : any = { }
3938 if ( optionsStr ) {
4039 try {
4140 // Simple options parsing (in real implementation, use a proper parser)
4241 globOptions = parseGlobOptions ( optionsStr )
43- } catch ( error ) {
42+ }
43+ catch ( error ) {
4444 console . warn ( 'Failed to parse glob options:' , optionsStr )
4545 }
4646 }
@@ -54,16 +54,17 @@ export function createGlobImportPlugin(options: GlobImportOptions = {}): Robuild
5454 id ,
5555 isEager ,
5656 isAsUrls ,
57- patterns
57+ patterns ,
5858 )
5959 transformedCode = transformedCode . replace ( fullMatch , replacement )
60- } catch ( error ) {
60+ }
61+ catch ( error ) {
6162 console . error ( `Failed to process glob import ${ pattern } :` , error )
6263 }
6364 }
6465
6566 return hasGlobImports ? transformedCode : null
66- }
67+ } ,
6768 }
6869}
6970
@@ -75,7 +76,7 @@ async function generateGlobImport(
7576 importer : string ,
7677 eager : boolean ,
7778 asUrls : boolean ,
78- allowedPatterns : string [ ]
79+ allowedPatterns : string [ ] ,
7980) : Promise < string > {
8081 const importerDir = dirname ( importer )
8182
@@ -89,9 +90,10 @@ async function generateGlobImport(
8990 try {
9091 const absolutePattern = resolve ( importerDir , pattern )
9192 files = await glob ( absolutePattern , {
92- ignore : [ '**/node_modules/**' , '**/.git/**' ]
93+ ignore : [ '**/node_modules/**' , '**/.git/**' ] ,
9394 } )
94- } catch ( error ) {
95+ }
96+ catch ( error ) {
9597 // In test environment, create mock files based on pattern
9698 if ( pattern . includes ( '*.js' ) ) {
9799 files = [ resolve ( importerDir , pattern . replace ( '*' , 'module1' ) ) , resolve ( importerDir , pattern . replace ( '*' , 'module2' ) ) ]
@@ -100,7 +102,8 @@ async function generateGlobImport(
100102
101103 if ( eager ) {
102104 return generateEagerImport ( files , importerDir , asUrls )
103- } else {
105+ }
106+ else {
104107 return generateLazyImport ( files , importerDir , asUrls )
105108 }
106109}
@@ -119,15 +122,17 @@ function generateEagerImport(files: string[], importerDir: string, asUrls: boole
119122
120123 if ( asUrls ) {
121124 exports . push ( ` "${ key } ": "${ relativePath } "` )
122- } else {
125+ }
126+ else {
123127 imports . push ( `import * as ${ varName } from "${ relativePath } ";` )
124128 exports . push ( ` "${ key } ": ${ varName } ` )
125129 }
126130 } )
127131
128132 if ( asUrls ) {
129133 return `{\n${ exports . join ( ',\n' ) } \n}`
130- } else {
134+ }
135+ else {
131136 return `${ imports . join ( '\n' ) } \n{\n${ exports . join ( ',\n' ) } \n}`
132137 }
133138}
@@ -138,13 +143,14 @@ function generateEagerImport(files: string[], importerDir: string, asUrls: boole
138143function generateLazyImport ( files : string [ ] , importerDir : string , asUrls : boolean ) : string {
139144 const exports : string [ ] = [ ]
140145
141- files . forEach ( file => {
146+ files . forEach ( ( file ) => {
142147 const relativePath = relative ( importerDir , file )
143148 const key = `./${ relativePath } `
144149
145150 if ( asUrls ) {
146151 exports . push ( ` "${ key } ": "${ relativePath } "` )
147- } else {
152+ }
153+ else {
148154 exports . push ( ` "${ key } ": () => import("${ relativePath } ")` )
149155 }
150156 } )
@@ -157,8 +163,9 @@ function generateLazyImport(files: string[], importerDir: string, asUrls: boolea
157163 */
158164function isPatternAllowed ( pattern : string , allowedPatterns : string [ ] ) : boolean {
159165 // Simple pattern matching - in real implementation, use minimatch
160- return allowedPatterns . some ( allowed => {
161- if ( allowed === '**/*' ) return true
166+ return allowedPatterns . some ( ( allowed ) => {
167+ if ( allowed === '**/*' )
168+ return true
162169 return pattern . startsWith ( allowed . replace ( '**/*' , '' ) )
163170 } )
164171}
@@ -170,7 +177,7 @@ function parseGlobOptions(optionsStr: string): any {
170177 // Simple parser for basic options
171178 // In real implementation, use a proper AST parser
172179 const options : any = { }
173-
180+
174181 // Extract eager option
175182 if ( optionsStr . includes ( 'eager:' ) || optionsStr . includes ( 'eager ' ) ) {
176183 const eagerMatch = optionsStr . match ( / e a g e r \s * : \s * ( t r u e | f a l s e ) / )
@@ -194,13 +201,14 @@ function parseGlobOptions(optionsStr: string): any {
194201export function createGlobVirtualModule (
195202 pattern : string ,
196203 files : string [ ] ,
197- options : { eager ?: boolean , asUrls ?: boolean } = { }
204+ options : { eager ?: boolean , asUrls ?: boolean } = { } ,
198205) : string {
199206 const { eager = false , asUrls = false } = options
200207
201208 if ( eager ) {
202209 return generateEagerImport ( files , '' , asUrls )
203- } else {
210+ }
211+ else {
204212 return generateLazyImport ( files , '' , asUrls )
205213 }
206214}
@@ -210,18 +218,19 @@ export function createGlobVirtualModule(
210218 */
211219export async function resolveGlobPatterns (
212220 patterns : string [ ] ,
213- cwd : string = process . cwd ( )
221+ cwd : string = process . cwd ( ) ,
214222) : Promise < Record < string , string [ ] > > {
215223 const result : Record < string , string [ ] > = { }
216224
217225 for ( const pattern of patterns ) {
218226 try {
219227 const files = await glob ( pattern , {
220228 cwd,
221- ignore : [ '**/node_modules/**' , '**/.git/**' ]
229+ ignore : [ '**/node_modules/**' , '**/.git/**' ] ,
222230 } )
223231 result [ pattern ] = files
224- } catch ( error ) {
232+ }
233+ catch ( error ) {
225234 console . error ( `Failed to resolve glob pattern ${ pattern } :` , error )
226235 result [ pattern ] = [ ]
227236 }
@@ -236,7 +245,7 @@ export async function resolveGlobPatterns(
236245export async function transformGlobImports (
237246 code : string ,
238247 id : string ,
239- options : GlobImportOptions = { }
248+ options : GlobImportOptions = { } ,
240249) : Promise < string | null > {
241250 const plugin = createGlobImportPlugin ( options )
242251 if ( plugin . transform ) {
@@ -251,12 +260,12 @@ export async function transformGlobImports(
251260export function extractGlobPatterns ( code : string ) : string [ ] {
252261 const patterns : string [ ] = [ ]
253262 const globImportRegex = / i m p o r t \. m e t a \. g l o b \s * \( \s * ( [ ' " ` ] ) ( .* ?) \1/ g
254-
263+
255264 let match
256265 while ( ( match = globImportRegex . exec ( code ) ) !== null ) {
257266 patterns . push ( match [ 2 ] )
258267 }
259-
268+
260269 return patterns
261270}
262271
0 commit comments