From 87e5a0a8c818fbb3496b048e28216733516cdd8b Mon Sep 17 00:00:00 2001 From: nemanjam Date: Sat, 22 Jun 2024 12:20:02 +0200 Subject: [PATCH] leave process.env and dotenv for astro.config.mjs as is, and leave comments for reminder --- docs/todo3.md | 2 +- env.d.ts | 20 +++++++++++++++++--- src/config.ts | 4 ++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/docs/todo3.md b/docs/todo3.md index bf6e2d4..bc8c59f 100644 --- a/docs/todo3.md +++ b/docs/todo3.md @@ -344,5 +344,5 @@ share styleguide sitemap transistor favicon -rewrite env vars with new types and import.meta instead of process.env + rewrite env vars with new types and import.meta instead of process.env // ne moze naravno, ok je ovako ``` diff --git a/env.d.ts b/env.d.ts index 4ff691e..bbcedb2 100644 --- a/env.d.ts +++ b/env.d.ts @@ -1,8 +1,22 @@ declare namespace NodeJS { + /** for astro.config.mjs */ interface ProcessEnv { - NODE_ENV: 'development' | 'production' | 'test'; - SITE_URL: string; + readonly NODE_ENV: 'development' | 'production' | 'test'; + readonly SITE_URL: string; /** Optional in .env file but always defined in type. Default: false. */ - PREVIEW_MODE: boolean; + readonly PREVIEW_MODE: boolean; } } + +// same type repeated + +/** for import.meta.env for the rest of the code */ +interface ImportMetaEnv { + // NODE_ENV, SITE_URL... included by default + + readonly PREVIEW_MODE: boolean; +} + +interface ImportMeta { + readonly env: ImportMetaEnv; +} diff --git a/src/config.ts b/src/config.ts index 9f893cf..f779b56 100644 --- a/src/config.ts +++ b/src/config.ts @@ -8,6 +8,10 @@ import type { ConfigType } from './types/config'; /*------------------ load .env file -----------------*/ +// import.meta.env is not available in astro.config.mjs, only after the config is loaded. +// ! MUST use process.env for vars used in astro.config.mjs. +// https://github.com/withastro/astro/issues?q=.env+file+not+loaded + const NODE_ENV = process.env.NODE_ENV; if (!nodeEnvValues.includes(NODE_ENV)) {