Skip to content

UNUSED_EXTERNAL_IMPORT build warning with Vite, Rollup, or Nuxt

当打包使用 @supabase/supabase-js 的应用时,你可能会看到类似这样的警告:

🌐 When bundling an application that uses @supabase/supabase-js, you may see warnings like:

1
"PostgrestError" is imported from external module "@supabase/postgrest-js" but never used in "...supabase-js/dist/index.mjs".
2
"FunctionRegion", "FunctionsError", "FunctionsFetchError", "FunctionsHttpError" and "FunctionsRelayError" are imported from external module "@supabase/functions-js" but never used in "...".

这是误报——你的包是正确的,没有缺少任何代码。

为什么会这样 #

🌐 Why this happens

@supabase/supabase-js 重新导出像 PostgrestErrorFunctionsError 这样的错误类型,所以你可以直接从 @supabase/supabase-js 导入它们。构建工具会把来自同一个包的所有导入合并成输出里的单个语句:

1
// dist/index.mjs (simplified)
2
import { PostgrestClient, PostgrestError } from '@supabase/postgrest-js'
3
// ^ used internally ^ re-exported for you

Vite/Rollup 会检查从那个 import 导入的哪些名字在 代码主体中 被引用,并将 PostgrestError 标记为未使用,因为它只出现在 export 语句中——没有被调用或赋值。实际上导出本身才是它真正的使用,但这个检查不会考虑 re-export。Tree-shaking 和打包大小不受影响。

🌐 Vite/Rollup checks which names from that import are referenced in the code body and flags PostgrestError as unused, because it only appears in an export statement — not called or assigned. The export itself is the real usage, but this check doesn't account for re-exports. Tree-shaking and bundle size are unaffected.

关闭警告 #

🌐 Suppress the warning

Vite / Rollup(vite.config.jsrollup.config.js#

🌐 Vite / Rollup (vite.config.js or rollup.config.js)

1
export default {
2
build: {
3
rollupOptions: {
4
onwarn(warning, warn) {
5
if (warning.code === 'UNUSED_EXTERNAL_IMPORT' && warning.exporter?.includes('@supabase/'))
6
return
7
warn(warning)
8
},
9
},
10
},
11
}

Nuxt(nuxt.config.ts#

🌐 Nuxt (nuxt.config.ts)

1
export default defineNuxtConfig({
2
vite: {
3
build: {
4
rollupOptions: {
5
onwarn(warning, warn) {
6
if (warning.code === 'UNUSED_EXTERNAL_IMPORT' && warning.exporter?.includes('@supabase/'))
7
return
8
warn(warning)
9
},
10
},
11
},
12
},
13
})