Skip to content

Commit

Permalink
fix(optimizer): attempt resolve node builtin first before externalizing
Browse files Browse the repository at this point in the history
fix #1746
  • Loading branch information
yyx990803 committed Jan 27, 2021
1 parent b5987c1 commit a69fa1e
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,35 +53,40 @@ export function esbuildDepPlugin(
}
)

build.onResolve({ filter: /^[\w@]/ }, async ({ path: id, importer }) => {
// ensure esbuild uses our resolved entires of optimized deps in all
// cases
if (id in qualified) {
return {
path: path.resolve(qualified[id])
}
} else if (!isBuiltin(id)) {
// use vite resolver
const resolved = await resolve(id, importer)
if (resolved) {
build.onResolve(
{ filter: /^[\w@][^:]/ },
async ({ path: id, importer }) => {
// ensure esbuild uses our resolved entires of optimized deps in all
// cases
if (id in qualified) {
return {
path: resolved
path: path.resolve(qualified[id])
}
} else {
// use vite resolver
const resolved = await resolve(id, importer)
if (resolved) {
return {
path: resolved
}
}

if (isBuiltin(id)) {
// redirect node-builtins to empty module for browser
config.logger.warn(
chalk.yellow(
`externalized node built-in "${id}" to empty module. ` +
`(imported by: ${chalk.white.dim(importer)})`
)
)
return {
path: id,
namespace: 'browser-external'
}
}
}
} else {
// redirect node-builtins to empty module for browser
config.logger.warn(
chalk.yellow(
`externalized node built-in "${id}" to empty module. ` +
`(imported by: ${chalk.white.dim(importer)})`
)
)
return {
path: id,
namespace: 'browser-external'
}
}
})
)

build.onLoad(
{ filter: /.*/, namespace: 'browser-external' },
Expand Down

0 comments on commit a69fa1e

Please sign in to comment.