Skip to content

Commit

Permalink
leave process.env and dotenv for astro.config.mjs as is, and leave co…
Browse files Browse the repository at this point in the history
…mments for reminder
  • Loading branch information
nemanjam committed Jun 22, 2024
1 parent d3d5207 commit 87e5a0a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/todo3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
20 changes: 17 additions & 3 deletions env.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 87e5a0a

Please sign in to comment.