Skip to content

Commit 64b700a

Browse files
committed
fix(typescript-sdk): prevent bundlers from requiring unused @ai-sdk providers
1 parent 70362e4 commit 64b700a

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

sdks/typescript/src/providers/ai-sdk-provider.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,42 @@ export class VercelAIProvider implements LLMProvider {
8383

8484
/**
8585
* Get the configured language model.
86-
* Uses dynamic imports so consumers only need to install the provider packages they use.
86+
*
87+
* Uses dynamic imports so consumers only need to install the provider packages
88+
* they actually use. The `webpackIgnore` / `turbopackIgnore` / `@vite-ignore`
89+
* magic comments stop bundlers (Next.js, webpack, Vite/Rollup, etc.) from
90+
* statically resolving the uninstalled providers at build time — without them,
91+
* bundling fails with "Module not found: Can't resolve '@ai-sdk/...'" even
92+
* though the import is guarded at runtime.
8793
*/
8894
private async getModel() {
8995
const apiKey = this.config.apiKey;
9096

9197
switch (this.config.type) {
9298
case 'openai': {
93-
const { createOpenAI } = await import('@ai-sdk/openai').catch(() => {
99+
const { createOpenAI } = await import(
100+
/* webpackIgnore: true */ /* turbopackIgnore: true */ /* @vite-ignore */ '@ai-sdk/openai'
101+
).catch(() => {
94102
throw new Error(
95103
'To use the OpenAI provider, install its adapter: npm install @ai-sdk/openai'
96104
);
97105
});
98106
return createOpenAI(apiKey ? { apiKey } : {})(this.model);
99107
}
100108
case 'anthropic': {
101-
const { createAnthropic } = await import('@ai-sdk/anthropic').catch(() => {
109+
const { createAnthropic } = await import(
110+
/* webpackIgnore: true */ /* turbopackIgnore: true */ /* @vite-ignore */ '@ai-sdk/anthropic'
111+
).catch(() => {
102112
throw new Error(
103113
'To use the Anthropic provider, install its adapter: npm install @ai-sdk/anthropic'
104114
);
105115
});
106116
return createAnthropic(apiKey ? { apiKey } : {})(this.model);
107117
}
108118
case 'google': {
109-
const { createGoogleGenerativeAI } = await import('@ai-sdk/google').catch(() => {
119+
const { createGoogleGenerativeAI } = await import(
120+
/* webpackIgnore: true */ /* turbopackIgnore: true */ /* @vite-ignore */ '@ai-sdk/google'
121+
).catch(() => {
110122
throw new Error(
111123
'To use the Google provider, install its adapter: npm install @ai-sdk/google'
112124
);

0 commit comments

Comments
 (0)