Skip to content

Commit

Permalink
fix(optimizer): handle externals in pnp compat
Browse files Browse the repository at this point in the history
  • Loading branch information
swandir committed Jan 18, 2022
1 parent 7fae8d7 commit cd19392
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Expand Up @@ -68,6 +68,24 @@ export function esbuildDepPlugin(
return resolver(id, _importer, undefined, ssr)
}

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 @@ -122,21 +140,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 @@ -209,15 +213,18 @@ export function esbuildDepPlugin(
if (isRunningWithYarnPnp) {
build.onResolve(
{ filter: /.*/ },
async ({ path, importer, kind, resolveDir, namespace }) => ({
// pass along resolveDir for entries
path: await resolve(
path,
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 require('fs').promises.readFile(args.path),
Expand Down

0 comments on commit cd19392

Please sign in to comment.