Skip to content

Commit af8ca60

Browse files
dickeylthbluwy
andauthored
feat: enable optimizeDeps.esbuildOptions.loader (#6840)
Co-authored-by: bluwy <[email protected]>
1 parent 694c1ce commit af8ca60

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

Diff for: packages/vite/src/node/optimizer/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,12 @@ export async function runOptimizeDeps(
417417
try {
418418
exportsData = parse(entryContent) as ExportsData
419419
} catch {
420+
const loader = esbuildOptions.loader?.[path.extname(filePath)] || 'jsx'
420421
debug(
421-
`Unable to parse dependency: ${id}. Trying again with a JSX transform.`
422+
`Unable to parse dependency: ${id}. Trying again with a ${loader} transform.`
422423
)
423424
const transformed = await transformWithEsbuild(entryContent, filePath, {
424-
loader: 'jsx'
425+
loader
425426
})
426427
// Ensure that optimization won't fail by defaulting '.js' to the JSX parser.
427428
// This is useful for packages such as Gatsby.

Diff for: packages/vite/src/node/optimizer/scan.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -463,21 +463,26 @@ function esbuildScanPlugin(
463463
contents = config.esbuild.jsxInject + `\n` + contents
464464
}
465465

466+
const loader =
467+
config.optimizeDeps?.esbuildOptions?.loader?.[`.${ext}`] ||
468+
(ext as Loader)
469+
466470
if (contents.includes('import.meta.glob')) {
467471
return transformGlob(
468472
contents,
469473
id,
470474
config.root,
471-
ext as Loader,
475+
loader,
472476
resolve,
473477
config.logger
474478
).then((contents) => ({
475-
loader: ext as Loader,
479+
loader,
476480
contents
477481
}))
478482
}
483+
479484
return {
480-
loader: ext as Loader,
485+
loader,
481486
contents
482487
}
483488
})

0 commit comments

Comments
 (0)