Skip to content

Commit

Permalink
fix(optimizer): log esbuild error when scanning deps (#11977)
Browse files Browse the repository at this point in the history
Co-authored-by: bluwy <bjornlu.dev@gmail.com>
  • Loading branch information
fi3ework and bluwy authored Feb 22, 2023
1 parent f2ad222 commit 20e6060
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/node/optimizer/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ async function createDepsOptimizer(
// do another optimize step
postScanOptimizationResult = runOptimizeDeps(config, knownDeps)
} catch (e) {
logger.error(e.message)
logger.error(e.stack || e.message)
} finally {
resolve()
depsOptimizer.scanProcessing = undefined
Expand Down
46 changes: 32 additions & 14 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path'
import { performance } from 'node:perf_hooks'
import glob from 'fast-glob'
import type { Loader, OnLoadResult, Plugin } from 'esbuild'
import { build, transform } from 'esbuild'
import { build, formatMessages, transform } from 'esbuild'
import colors from 'picocolors'
import type { ResolvedConfig } from '..'
import {
Expand Down Expand Up @@ -106,19 +106,37 @@ export async function scanImports(config: ResolvedConfig): Promise<{
const { plugins = [], ...esbuildOptions } =
config.optimizeDeps?.esbuildOptions ?? {}

await build({
absWorkingDir: process.cwd(),
write: false,
stdin: {
contents: entries.map((e) => `import ${JSON.stringify(e)}`).join('\n'),
loader: 'js',
},
bundle: true,
format: 'esm',
logLevel: 'error',
plugins: [...plugins, plugin],
...esbuildOptions,
})
try {
await build({
absWorkingDir: process.cwd(),
write: false,
stdin: {
contents: entries.map((e) => `import ${JSON.stringify(e)}`).join('\n'),
loader: 'js',
},
bundle: true,
format: 'esm',
logLevel: 'silent',
plugins: [...plugins, plugin],
...esbuildOptions,
})
} catch (e) {
const prependMessage = colors.red(`\
Failed to scan for dependencies from entries:
${entries.join('\n')}
`)
if (e.errors) {
const msgs = await formatMessages(e.errors, {
kind: 'error',
color: true,
})
e.message = prependMessage + msgs.join('\n')
} else {
e.message = prependMessage + e.message
}
throw e
}

debug(`Scan completed in ${(performance.now() - start).toFixed(2)}ms:`, deps)

Expand Down

0 comments on commit 20e6060

Please sign in to comment.