diff --git a/src/shared/api/api.ts b/src/shared/api/api.ts index 196a1c4..8eca229 100644 --- a/src/shared/api/api.ts +++ b/src/shared/api/api.ts @@ -8,7 +8,10 @@ */ import ky, { type KyInstance } from 'ky' -const API_BASE_URL = (import.meta.env.VITE_API_BASE_URL ?? '').replace(/\/$/, '') +// import.meta 를 직접 쓰지 않는 이유: orval 이 mutator 를 cjs 로 esbuild 번들하는 과정에서 +// `import.meta is not available in cjs` 경고가 떠 SDK 재생성 로그가 시끄럽다. +// 대신 vite.config.ts 의 `define` 으로 빌드 타임 치환되는 글로벌을 사용한다. +const API_BASE_URL = (__VITE_API_BASE_URL__ ?? '').replace(/\/$/, '') export const kyInstance: KyInstance = ky.create({ timeout: 10_000, @@ -18,7 +21,7 @@ export const kyInstance: KyInstance = ky.create({ hooks: { beforeRequest: [ (request) => { - const apiKey = import.meta.env.VITE_API_KEY; + const apiKey = __VITE_API_KEY__; if (apiKey) request.headers.set('x-api-key', apiKey); }, // TODO: 인증 토큰 주입 diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 945a19a..a60880f 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -8,3 +8,7 @@ interface ImportMetaEnv { interface ImportMeta { readonly env: ImportMetaEnv; } + +// vite.config.ts `define` 로 빌드 시 치환되는 글로벌 — api 모듈에서 import.meta 회피용 +declare const __VITE_API_BASE_URL__: string; +declare const __VITE_API_KEY__: string; diff --git a/vite.config.ts b/vite.config.ts index 802f25d..517dab6 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -22,6 +22,12 @@ export default defineConfig(({ mode }) => { '@': path.resolve(__dirname, './src'), }, }, + // src/shared/api/api.ts 가 orval 의 mutator 로 cjs 번들될 때 `import.meta` 가 비어 + // 경고가 뜨던 문제를 우회. Vite 가 빌드 타임에 아래 글로벌을 실값으로 치환한다. + define: { + __VITE_API_BASE_URL__: JSON.stringify(env.VITE_API_BASE_URL ?? ''), + __VITE_API_KEY__: JSON.stringify(env.VITE_API_KEY ?? ''), + }, server: { port: 3000, host: '0.0.0.0',