Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow onwarn to override vite default warning handling #12757

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -871,40 +871,50 @@ export function onRollupWarning(
warn: WarningHandler,
config: ResolvedConfig,
): void {
if (warning.code === 'UNRESOLVED_IMPORT') {
const id = warning.id
const exporter = warning.exporter
// throw unless it's commonjs external...
if (!id || !/\?commonjs-external$/.test(id)) {
throw new Error(
`[vite]: Rollup failed to resolve import "${exporter}" from "${id}".\n` +
`This is most likely unintended because it can break your application at runtime.\n` +
`If you do want to externalize this module explicitly add it to\n` +
`\`build.rollupOptions.external\``,
function viteWarn(warning: RollupWarning) {
if (warning.code === 'UNRESOLVED_IMPORT') {
const id = warning.id
const exporter = warning.exporter
// throw unless it's commonjs external...
if (!id || !/\?commonjs-external$/.test(id)) {
throw new Error(
`[vite]: Rollup failed to resolve import "${exporter}" from "${id}".\n` +
`This is most likely unintended because it can break your application at runtime.\n` +
`If you do want to externalize this module explicitly add it to\n` +
`\`build.rollupOptions.external\``,
)
}
}

if (
warning.plugin === 'rollup-plugin-dynamic-import-variables' &&
dynamicImportWarningIgnoreList.some((msg) =>
warning.message.includes(msg),
)
) {
return
}
}

if (
warning.plugin === 'rollup-plugin-dynamic-import-variables' &&
dynamicImportWarningIgnoreList.some((msg) => warning.message.includes(msg))
) {
return
}
if (!warningIgnoreList.includes(warning.code!)) {
return
}

if (!warningIgnoreList.includes(warning.code!)) {
const userOnWarn = config.build.rollupOptions?.onwarn
if (userOnWarn) {
userOnWarn(warning, warn)
} else if (warning.code === 'PLUGIN_WARNING') {
if (warning.code === 'PLUGIN_WARNING') {
config.logger.warn(
`${colors.bold(
colors.yellow(`[plugin:${warning.plugin}]`),
)} ${colors.yellow(warning.message)}`,
)
} else {
warn(warning)
}

warn(warning)
}

const userOnWarn = config.build.rollupOptions?.onwarn
if (userOnWarn) {
userOnWarn(warning, viteWarn)
} else {
viteWarn(warning)
}
}

Expand Down