Skip to content

Commit

Permalink
fix(optimizer): browser mapping for yarn pnp (#6493)
Browse files Browse the repository at this point in the history
  • Loading branch information
swandir authored Jun 16, 2022
1 parent ccfccec commit c1c7af3
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ export function esbuildDepPlugin(
return resolver(id, _importer, undefined)
}

const resolveResult = (id: string, resolved: string) => {
if (resolved.startsWith(browserExternalId)) {
return {
path: id,
namespace: 'browser-external'
}
}
if (isExternalUrl(resolved)) {
return {
path: resolved,
external: true
}
}
return {
path: path.resolve(resolved)
}
}

return {
name: 'vite:dep-pre-bundle',
setup(build) {
Expand Down Expand Up @@ -156,21 +174,7 @@ export function esbuildDepPlugin(
// use vite's own resolver
const resolved = await resolve(id, importer, kind)
if (resolved) {
if (resolved.startsWith(browserExternalId)) {
return {
path: id,
namespace: 'browser-external'
}
}
if (isExternalUrl(resolved)) {
return {
path: resolved,
external: true
}
}
return {
path: path.resolve(resolved)
}
return resolveResult(id, resolved)
}
}
)
Expand Down Expand Up @@ -258,11 +262,20 @@ module.exports = Object.create(new Proxy({}, {
if (isRunningWithYarnPnp) {
build.onResolve(
{ filter: /.*/ },
async ({ path, importer, kind, resolveDir }) => ({
// pass along resolveDir for entries
path: await resolve(path, importer, kind, resolveDir)
})
async ({ path: id, importer, kind, resolveDir, namespace }) => {
const resolved = await resolve(
id,
importer,
kind,
// pass along resolveDir for entries
namespace === 'dep' ? resolveDir : undefined
)
if (resolved) {
return resolveResult(id, resolved)
}
}
)

build.onLoad({ filter: /.*/ }, async (args) => ({
contents: await fs.readFile(args.path),
loader: 'default'
Expand Down

0 comments on commit c1c7af3

Please sign in to comment.