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)) {