diff --git a/packages/sanity/src/_internal/cli/server/getViteConfig.ts b/packages/sanity/src/_internal/cli/server/getViteConfig.ts index af1f156ef0d..dc1892be255 100644 --- a/packages/sanity/src/_internal/cli/server/getViteConfig.ts +++ b/packages/sanity/src/_internal/cli/server/getViteConfig.ts @@ -3,7 +3,7 @@ import path from 'node:path' import {type UserViteConfig} from '@sanity/cli' import debug from 'debug' import readPkgUp from 'read-pkg-up' -import {type ConfigEnv, type InlineConfig} from 'vite' +import {type ConfigEnv, type InlineConfig, type Rollup} from 'vite' import {createExternalFromImportMap} from './createExternalFromImportMap' import {getSanityPkgExportAliases} from './getBrowserAliases' @@ -143,6 +143,7 @@ export async function getViteConfig(options: ViteOptions): Promise emptyOutDir: false, // Rely on CLI to do this rollupOptions: { + onwarn: onRollupWarn, external: createExternalFromImportMap(importMap), input: { sanity: path.join(cwd, '.sanity', 'runtime', 'app.js'), @@ -154,6 +155,32 @@ export async function getViteConfig(options: ViteOptions): Promise return viteConfig } +function onRollupWarn(warning: Rollup.RollupLog, warn: Rollup.LoggingFunction) { + if (suppressUnusedImport(warning)) { + return + } + + warn(warning) +} + +function suppressUnusedImport(warning: Rollup.RollupLog & {ids?: string[]}): boolean { + if (warning.code !== 'UNUSED_EXTERNAL_IMPORT') return false + + // Suppress: + // ``` + // "useDebugValue" is imported from external module "react"… + // ``` + if (warning.names?.includes('useDebugValue')) { + warning.names = warning.names.filter((n) => n !== 'useDebugValue') + if (warning.names.length === 0) return true + } + + // If some library does something unexpected, we suppress since it isn't actionable + if (warning.ids?.every((id) => id.includes('/node_modules/'))) return true + + return false +} + /** * Ensure Sanity entry chunk is always loaded *