diff --git a/openapi-ts.config.ts b/openapi-ts.config.ts index 31dcbc46..1d1c3a76 100644 --- a/openapi-ts.config.ts +++ b/openapi-ts.config.ts @@ -1,3 +1,7 @@ +// This config runs in Node, outside Astro, so nothing has loaded `.env` yet. It must come +// before the import below: `consts.ts` reads the environment as it is evaluated. +import "dotenv/config"; + import { defineConfig } from "@hey-api/openapi-ts"; import { defaultPlugins } from "@hey-api/openapi-ts"; diff --git a/src/utils/consts.ts b/src/utils/consts.ts index d924359d..16f6b74f 100644 --- a/src/utils/consts.ts +++ b/src/utils/consts.ts @@ -1,9 +1,15 @@ -import dotenv from "dotenv"; - -if (typeof process !== "undefined") { - dotenv.config(); -} - +/** + * Reads configuration from whichever environment this module ends up in. + * + * Nothing is loaded from disk here. This file travels into the browser bundle — components + * call `getDefaultCurrency()` and friends — and it also runs on Cloudflare Workers, where + * there is no filesystem to read a `.env` from. Calling `dotenv.config()` from here used to + * throw `process.cwd is not a function` in the browser, which killed hydration for every + * island that reached this module. + * + * Astro loads `.env` into `import.meta.env` on its own. The one caller that runs outside + * Astro is `openapi-ts.config.ts`, and it loads `.env` itself. + */ let runtimeEnv: Record; if (typeof import.meta !== "undefined" && import.meta.env) {