diff --git a/packages/vite/src/node/optimizer/scan.ts b/packages/vite/src/node/optimizer/scan.ts index e600e7ff69f96f..3fa22c1d88da9e 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -481,7 +481,7 @@ function esbuildScanPlugin( // for jsx/tsx, we need to access the content and check for // presence of import.meta.glob, since it results in import relationships // but isn't crawled by esbuild. - build.onLoad({ filter: JS_TYPES_RE }, ({ path: id }) => { + build.onLoad({ filter: JS_TYPES_RE }, async ({ path: id }) => { let ext = path.extname(id).slice(1) if (ext === 'mjs') ext = 'js' @@ -494,6 +494,16 @@ function esbuildScanPlugin( config.optimizeDeps?.esbuildOptions?.loader?.[`.${ext}`] || (ext as Loader) + if (contents.includes('import.meta.glob')) { + return { + loader: loader, + contents: + ( + await transformGlobImport(contents, id, config.root, resolve) + )?.s.toString() || contents + } + } + return { loader, contents