Skip to content

Commit c1c7af3

Browse files
authored
fix(optimizer): browser mapping for yarn pnp (#6493)
1 parent ccfccec commit c1c7af3

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

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

+32-19
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,24 @@ export function esbuildDepPlugin(
8080
return resolver(id, _importer, undefined)
8181
}
8282

83+
const resolveResult = (id: string, resolved: string) => {
84+
if (resolved.startsWith(browserExternalId)) {
85+
return {
86+
path: id,
87+
namespace: 'browser-external'
88+
}
89+
}
90+
if (isExternalUrl(resolved)) {
91+
return {
92+
path: resolved,
93+
external: true
94+
}
95+
}
96+
return {
97+
path: path.resolve(resolved)
98+
}
99+
}
100+
83101
return {
84102
name: 'vite:dep-pre-bundle',
85103
setup(build) {
@@ -156,21 +174,7 @@ export function esbuildDepPlugin(
156174
// use vite's own resolver
157175
const resolved = await resolve(id, importer, kind)
158176
if (resolved) {
159-
if (resolved.startsWith(browserExternalId)) {
160-
return {
161-
path: id,
162-
namespace: 'browser-external'
163-
}
164-
}
165-
if (isExternalUrl(resolved)) {
166-
return {
167-
path: resolved,
168-
external: true
169-
}
170-
}
171-
return {
172-
path: path.resolve(resolved)
173-
}
177+
return resolveResult(id, resolved)
174178
}
175179
}
176180
)
@@ -258,11 +262,20 @@ module.exports = Object.create(new Proxy({}, {
258262
if (isRunningWithYarnPnp) {
259263
build.onResolve(
260264
{ filter: /.*/ },
261-
async ({ path, importer, kind, resolveDir }) => ({
262-
// pass along resolveDir for entries
263-
path: await resolve(path, importer, kind, resolveDir)
264-
})
265+
async ({ path: id, importer, kind, resolveDir, namespace }) => {
266+
const resolved = await resolve(
267+
id,
268+
importer,
269+
kind,
270+
// pass along resolveDir for entries
271+
namespace === 'dep' ? resolveDir : undefined
272+
)
273+
if (resolved) {
274+
return resolveResult(id, resolved)
275+
}
276+
}
265277
)
278+
266279
build.onLoad({ filter: /.*/ }, async (args) => ({
267280
contents: await fs.readFile(args.path),
268281
loader: 'default'

0 commit comments

Comments
 (0)