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 74b55b8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
51 changes: 24 additions & 27 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Expand Up @@ -2,8 +2,8 @@ import path from 'path'
import { Plugin } from 'esbuild'
import { knownAssetTypes } from '../constants'
import { ResolvedConfig } from '..'
import chalk from 'chalk'
import { isBuiltin, isRunningWithYarnPnp } from '../utils'
import { isRunningWithYarnPnp } from '../utils'
import { browserExternalId } from '../plugins/resolve'

const externalTypes = [
'css',
Expand Down Expand Up @@ -53,35 +53,32 @@ 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) {
if (resolved.startsWith(browserExternalId)) {
return {
path: id,
namespace: 'browser-external'
}
}
return {
path: resolved
}
}
}
} 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
8 changes: 6 additions & 2 deletions packages/vite/src/node/server/middlewares/error.ts
Expand Up @@ -20,11 +20,15 @@ export function prepareError(err: Error | RollupError): ErrorPayload['err'] {
}
}

export function buildErrorMessage(err: RollupError, args: string[] = []) {
export function buildErrorMessage(
err: RollupError,
args: string[] = [],
includeStack = true
) {
if (err.plugin) args.push(` Plugin: ${chalk.magenta(err.plugin)}`)
if (err.id) args.push(` File: ${chalk.cyan(err.id)}`)
if (err.frame) args.push(chalk.yellow(pad(err.frame)))
if (err.stack) args.push(pad(cleanStack(err.stack)))
if (includeStack && err.stack) args.push(pad(cleanStack(err.stack)))
return args.join('\n')
}

Expand Down
8 changes: 5 additions & 3 deletions packages/vite/src/node/server/pluginContainer.ts
Expand Up @@ -256,9 +256,11 @@ export async function createPluginContainer(
position?: number | { column: number; line: number }
) {
const err = formatError(e, position, this)
const msg = buildErrorMessage(err, [
chalk.yellow(`warning: ${err.message}`)
])
const msg = buildErrorMessage(
err,
[chalk.yellow(`warning: ${err.message}`)],
false
)
logger.warn(msg, {
clear: true,
timestamp: true
Expand Down

0 comments on commit 74b55b8

Please sign in to comment.