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 重新导出像 PostgrestError 和 FunctionsError 这样的错误类型,所以你可以直接从 @supabase/supabase-js 导入它们。构建工具会把来自同一个包的所有导入合并成输出里的单个语句:
1// dist/index.mjs (simplified)2import { PostgrestClient, PostgrestError } from '@supabase/postgrest-js'3// ^ used internally ^ re-exported for youVite/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.js 或 rollup.config.js#
🌐 Vite / Rollup (vite.config.js or rollup.config.js)
1export default {2 build: {3 rollupOptions: {4 onwarn(warning, warn) {5 if (warning.code === 'UNUSED_EXTERNAL_IMPORT' && warning.exporter?.includes('@supabase/'))6 return7 warn(warning)8 },9 },10 },11}Nuxt(nuxt.config.ts#
🌐 Nuxt (nuxt.config.ts)
这个问题已经在 @nuxtjs/supabase 2.0.4 版本中解决了。如果你使用的是该版本或更高版本,就不需要再使用这个解决方法了。
🌐 This issue has been resolved in @nuxtjs/supabase version 2.0.4. If you are on that version or later, you do not need to apply this workaround.
1export default defineNuxtConfig({2 vite: {3 build: {4 rollupOptions: {5 onwarn(warning, warn) {6 if (warning.code === 'UNUSED_EXTERNAL_IMPORT' && warning.exporter?.includes('@supabase/'))7 return8 warn(warning)9 },10 },11 },12 },13})