From 2f7d1c1f630f9dbbd44ce235e86afc09e4b9cc85 Mon Sep 17 00:00:00 2001 From: candy-Tong <563378816@qq.com> Date: Tue, 25 Oct 2022 23:51:39 +0800 Subject: [PATCH 1/2] 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..a28d1f2b12a320 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, + contents: + ( + await transformGlobImport(contents, id, config.root, resolve) + )?.s.toString() || contents + } + } + return { loader, contents From e83687c0d0da6c03d66ca1c8cfa3f28833d2a3c2 Mon Sep 17 00:00:00 2001 From: candy-Tong <563378816@qq.com> Date: Wed, 26 Oct 2022 22:57:17 +0800 Subject: [PATCH 2/2] refactor: extracting TS transform and transformGlobImport into a function --- packages/vite/src/node/optimizer/scan.ts | 49 +++++++++++++----------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/packages/vite/src/node/optimizer/scan.ts b/packages/vite/src/node/optimizer/scan.ts index a28d1f2b12a320..7ea28fffa6e8c3 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -200,6 +200,29 @@ function esbuildScanPlugin( external: !entries.includes(path) }) + const doTransformGlobImport = async ( + contents: string, + id: string, + loader: Loader + ) => { + let transpiledContents + // transpile because `transformGlobImport` only expects js + if (loader !== 'js') { + transpiledContents = (await transform(contents, { loader })).code + } else { + transpiledContents = contents + } + + const result = await transformGlobImport( + transpiledContents, + id, + config.root, + resolve + ) + + return result?.s.toString() || transpiledContents + } + return { name: 'vite:dep-scan', setup(build) { @@ -305,26 +328,9 @@ function esbuildScanPlugin( const key = `${path}?id=${scriptId++}` if (contents.includes('import.meta.glob')) { - let transpiledContents - // transpile because `transformGlobImport` only expects js - if (loader !== 'js') { - transpiledContents = (await transform(contents, { loader })) - .code - } else { - transpiledContents = contents - } - scripts[key] = { loader: 'js', // since it is transpiled - contents: - ( - await transformGlobImport( - transpiledContents, - path, - config.root, - resolve - ) - )?.s.toString() || transpiledContents, + contents: await doTransformGlobImport(contents, path, loader), pluginData: { htmlType: { loader } } @@ -496,11 +502,8 @@ function esbuildScanPlugin( if (contents.includes('import.meta.glob')) { return { - loader, - contents: - ( - await transformGlobImport(contents, id, config.root, resolve) - )?.s.toString() || contents + loader: 'js', // since it is transpiled, + contents: await doTransformGlobImport(contents, id, loader) } }