Skip to content

Commit c53ffec

Browse files
authored
fix: transform import.meta.glob when scan JS/TS #10634 (#10635)
1 parent fa2e47f commit c53ffec

File tree

1 file changed

+32
-19
lines changed
  • packages/vite/src/node/optimizer

1 file changed

+32
-19
lines changed

packages/vite/src/node/optimizer/scan.ts

+32-19
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,29 @@ function esbuildScanPlugin(
200200
external: !entries.includes(path)
201201
})
202202

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+
203226
return {
204227
name: 'vite:dep-scan',
205228
setup(build) {
@@ -305,26 +328,9 @@ function esbuildScanPlugin(
305328

306329
const key = `${path}?id=${scriptId++}`
307330
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-
317331
scripts[key] = {
318332
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),
328334
pluginData: {
329335
htmlType: { loader }
330336
}
@@ -481,7 +487,7 @@ function esbuildScanPlugin(
481487
// for jsx/tsx, we need to access the content and check for
482488
// presence of import.meta.glob, since it results in import relationships
483489
// 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 }) => {
485491
let ext = path.extname(id).slice(1)
486492
if (ext === 'mjs') ext = 'js'
487493

@@ -494,6 +500,13 @@ function esbuildScanPlugin(
494500
config.optimizeDeps?.esbuildOptions?.loader?.[`.${ext}`] ||
495501
(ext as Loader)
496502

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+
497510
return {
498511
loader,
499512
contents

0 commit comments

Comments
 (0)