@@ -200,6 +200,29 @@ function esbuildScanPlugin(
200
200
external : ! entries . includes ( path )
201
201
} )
202
202
203
+ const doTransformGlobImport = async (
204
+ contents : string ,
205
+ id : string ,
206
+ loader : Loader
207
+ ) => {
208
+ let transpiledContents
209
+ // transpile because `transformGlobImport` only expects js
210
+ if ( loader !== 'js' ) {
211
+ transpiledContents = ( await transform ( contents , { loader } ) ) . code
212
+ } else {
213
+ transpiledContents = contents
214
+ }
215
+
216
+ const result = await transformGlobImport (
217
+ transpiledContents ,
218
+ id ,
219
+ config . root ,
220
+ resolve
221
+ )
222
+
223
+ return result ?. s . toString ( ) || transpiledContents
224
+ }
225
+
203
226
return {
204
227
name : 'vite:dep-scan' ,
205
228
setup ( build ) {
@@ -305,26 +328,9 @@ function esbuildScanPlugin(
305
328
306
329
const key = `${ path } ?id=${ scriptId ++ } `
307
330
if ( contents . includes ( 'import.meta.glob' ) ) {
308
- let transpiledContents
309
- // transpile because `transformGlobImport` only expects js
310
- if ( loader !== 'js' ) {
311
- transpiledContents = ( await transform ( contents , { loader } ) )
312
- . code
313
- } else {
314
- transpiledContents = contents
315
- }
316
-
317
331
scripts [ key ] = {
318
332
loader : 'js' , // since it is transpiled
319
- contents :
320
- (
321
- await transformGlobImport (
322
- transpiledContents ,
323
- path ,
324
- config . root ,
325
- resolve
326
- )
327
- ) ?. s . toString ( ) || transpiledContents ,
333
+ contents : await doTransformGlobImport ( contents , path , loader ) ,
328
334
pluginData : {
329
335
htmlType : { loader }
330
336
}
@@ -481,7 +487,7 @@ function esbuildScanPlugin(
481
487
// for jsx/tsx, we need to access the content and check for
482
488
// presence of import.meta.glob, since it results in import relationships
483
489
// but isn't crawled by esbuild.
484
- build . onLoad ( { filter : JS_TYPES_RE } , ( { path : id } ) => {
490
+ build . onLoad ( { filter : JS_TYPES_RE } , async ( { path : id } ) => {
485
491
let ext = path . extname ( id ) . slice ( 1 )
486
492
if ( ext === 'mjs' ) ext = 'js'
487
493
@@ -494,6 +500,13 @@ function esbuildScanPlugin(
494
500
config . optimizeDeps ?. esbuildOptions ?. loader ?. [ `.${ ext } ` ] ||
495
501
( ext as Loader )
496
502
503
+ if ( contents . includes ( 'import.meta.glob' ) ) {
504
+ return {
505
+ loader : 'js' , // since it is transpiled,
506
+ contents : await doTransformGlobImport ( contents , id , loader )
507
+ }
508
+ }
509
+
497
510
return {
498
511
loader,
499
512
contents
0 commit comments