Skip to content

Commit

Permalink
fix(cli): suppress unactionable vite warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Sep 30, 2024
1 parent 70cc26b commit 3c0029f
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion packages/sanity/src/_internal/cli/server/getViteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -143,6 +143,7 @@ export async function getViteConfig(options: ViteOptions): Promise<InlineConfig>
emptyOutDir: false, // Rely on CLI to do this

rollupOptions: {
onwarn: onRollupWarn,
external: createExternalFromImportMap(importMap),
input: {
sanity: path.join(cwd, '.sanity', 'runtime', 'app.js'),
Expand All @@ -154,6 +155,32 @@ export async function getViteConfig(options: ViteOptions): Promise<InlineConfig>
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
*
Expand Down

0 comments on commit 3c0029f

Please sign in to comment.