Skip to content

Commit 742926f

Browse files
committed
refactor: bail server start on dep-optimization error
1 parent 61aec65 commit 742926f

File tree

2 files changed

+24
-34
lines changed

2 files changed

+24
-34
lines changed

packages/vite/src/node/build.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,17 @@ export function onRollupWarning(
387387
}
388388

389389
if (id && isBuiltin(id)) {
390-
let importingDep
390+
let importingDep: string | undefined
391391
if (importer) {
392392
const pkg = JSON.parse(lookupFile(importer, ['package.json']) || `{}`)
393393
if (pkg.name) {
394394
importingDep = pkg.name
395395
}
396396
}
397-
if (importingDep && allowNodeBuiltins.includes(importingDep)) {
397+
if (
398+
importingDep &&
399+
allowNodeBuiltins.some((allowed) => importingDep!.startsWith(allowed))
400+
) {
398401
return
399402
}
400403
const dep = importingDep

packages/vite/src/node/optimizer/index.ts

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ export async function optimizeDeps(
179179
plugins: [
180180
aliasPlugin({ entries: config.alias }),
181181
depAssetExternalPlugin(config),
182-
// TODO user pre plugins?
183182
resolvePlugin({
184183
root: config.root,
185184
dedupe: config.dedupe,
@@ -190,7 +189,6 @@ export async function optimizeDeps(
190189
preferConst: true,
191190
namedExports: true
192191
}),
193-
// TODO user normal/post plugins?
194192
commonjsPlugin({
195193
include: [/node_modules/],
196194
extensions: ['.js', '.cjs']
@@ -215,38 +213,27 @@ export async function optimizeDeps(
215213
}
216214
writeFile(dataPath, JSON.stringify(data, null, 2))
217215
} catch (e) {
218-
if (asCommand) {
219-
throw e
220-
} else {
221-
logger.error(
222-
chalk.red(`\nDep optimization failed with error:\n${e.message}`)
216+
delete e.watchFiles
217+
logger.error(chalk.red(`\nDep optimization failed with error:`))
218+
if (e.code === 'PARSE_ERROR') {
219+
e.message += `\n\n${chalk.cyan(
220+
path.relative(root, e.loc.file)
221+
)}\n${chalk.dim(e.frame)}`
222+
} else if (e.message.match('Node built-in')) {
223+
e.message += chalk.yellow(
224+
`\n\nTip:\nMake sure your "dependencies" only include packages that you\n` +
225+
`intend to use in the browser. If it's a Node.js package, it\n` +
226+
`should be in "devDependencies".\n\n` +
227+
`If you do intend to use this dependency in the browser and the\n` +
228+
`dependency does not actually use these Node built-ins in the\n` +
229+
`browser, you can add the dependency (not the built-in) to the\n` +
230+
`"optimizeDeps.allowNodeBuiltins" option in vite.config.js.\n\n` +
231+
`If that results in a runtime error, then unfortunately the\n` +
232+
`package is not distributed in a web-friendly format. You should\n` +
233+
`open an issue in its repo, or look for a modern alternative.`
223234
)
224-
if (e.code === 'PARSE_ERROR') {
225-
logger.error(
226-
chalk.cyan(path.relative(root, e.loc.file)) +
227-
`\n` +
228-
chalk.dim(e.frame)
229-
)
230-
} else if (e.message.match('Node built-in')) {
231-
logger.warn(
232-
chalk.yellow(
233-
`Tip:\nMake sure your "dependencies" only include packages that you\n` +
234-
`intend to use in the browser. If it's a Node.js package, it\n` +
235-
`should be in "devDependencies".\n\n` +
236-
`If you do intend to use this dependency in the browser and the\n` +
237-
`dependency does not actually use these Node built-ins in the\n` +
238-
`browser, you can add the dependency (not the built-in) to the\n` +
239-
`"optimizeDeps.allowNodeBuiltins" option in vite.config.js.\n\n` +
240-
`If that results in a runtime error, then unfortunately the\n` +
241-
`package is not distributed in a web-friendly format. You should\n` +
242-
`open an issue in its repo, or look for a modern alternative.`
243-
)
244-
// TODO link to docs once we have it
245-
)
246-
} else {
247-
throw e
248-
}
249235
}
236+
throw e
250237
}
251238
}
252239

0 commit comments

Comments
 (0)