From 58b8027cbcf563076430d96c56aa911bad92137e Mon Sep 17 00:00:00 2001 From: candy-Tong <563378816@qq.com> Date: Tue, 25 Oct 2022 22:55:14 +0800 Subject: [PATCH] feat: transform import.meta.glob when scan JS/TS #10634 --- packages/vite/src/node/optimizer/scan.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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