Skip to content

Commit 89f9f00

Browse files
authored
clarify docs around envs in vite (#196)
1 parent a9d8941 commit 89f9f00

File tree

11 files changed

+49
-25
lines changed

11 files changed

+49
-25
lines changed

.changeset/short-squids-learn.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

example-monorepo/packages/astro-example/.astro/content-assets.mjs

Lines changed: 0 additions & 1 deletion
This file was deleted.

example-monorepo/packages/astro-example/.astro/content-modules.mjs

Lines changed: 0 additions & 1 deletion
This file was deleted.

example-monorepo/packages/astro-example/.astro/data-store.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

example-monorepo/packages/astro-example/.astro/settings.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

example-monorepo/packages/astro-example/.astro/types.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/vanilla-vite-example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"type": "module",
66
"scripts": {
77
"dev": "vite",
8-
"build": "tsc && vite build",
9-
"preview": "vite preview"
8+
"build": "APP_ENV=production tsc && vite build",
9+
"preview": "APP_ENV=production vite preview"
1010
},
1111
"devDependencies": {
1212
"@varlock/vite-integration": "workspace:*",

examples/vite-vue-example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"type": "module",
66
"scripts": {
77
"dev": "vite",
8-
"build": "vue-tsc -b && vite build",
9-
"preview": "vite preview"
8+
"build": "APP_ENV=production vue-tsc -b && vite build",
9+
"preview": "APP_ENV=production vite preview"
1010
},
1111
"dependencies": {
1212
"vue": "^3.5.18"

packages/varlock-website/src/content/docs/integrations/astro.mdx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,21 @@ To enable type-safety and IntelliSense for your env vars, enable the [`@generate
114114

115115
## Managing multiple environments
116116

117-
Varlock can load multiple _environment-specific_ `.env` files (e.g., `.env.development`, `.env.preview`, `.env.production`).
118-
119-
By default, Astro relies on Vite's [`MODE` flag](https://vite.dev/guide/env-and-mode.html#modes) to determine which env file(s) to load.
120-
121-
With varlock, instead you set your own environment flag using the [`@currentEnv` root decorator](/reference/root-decorators/#currentEnv), e.g. `APP_ENV`. See the [environments guide](/guides/environments) for more information.
117+
Varlock can load multiple _environment-specific_ `.env` files (e.g., `.env.development`, `.env.preview`, `.env.production`) by using the [`@currentEnv` root decorator](/reference/root-decorators/#currentenv). **This is different than Astro/Vite's default behaviour, which relies on it's own [`MODE` flag](https://vite.dev/guide/env-and-mode.html#modes).**
118+
119+
Usually this env var will be defaulted to something like `development` in your `.env.schema` file, and you can override it by overriding the value when running commands - for example `APP_ENV=production vite build`. For a JavaScript based project, this will often be done in your `package.json` scripts.
120+
121+
```diff lang="json" title="package.json" add=/APP_ENV=[a-z]+/
122+
{
123+
"scripts": {
124+
"dev": "astro dev",
125+
"build": "APP_ENV=production astro build",
126+
"preview": "APP_ENV=production vite preview",
127+
}
128+
}
129+
```
122130

131+
In some cases, you could also set the current environment value based on other vars already injected by your CI platform, like the current branch name. See the [environments guide](/guides/environments) for more information.
123132

124133
## Managing sensitive config values
125134

packages/varlock-website/src/content/docs/integrations/javascript.mdx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import ExecCommandWidget from '@/components/ExecCommandWidget.astro';
77

88
There are a few different ways to integrate Varlock into a JavaScript / Node.js application.
99

10-
Some tools/frameworks may require an additional package, or have more specific instructions. The following integrations/guides are available, with more to come soon:
11-
- [Next.js](/integrations/nextjs)
10+
Some tools/frameworks may require an additional package, or have more specific instructions. Check the Integrations section in the navigation for more details.
1211

1312
**Want to help us build more integrations? Join our <a href={process.env.PUBLIC_DISCORD_URL}>Discord</a>!**
1413

1514
## Node.js - `varlock/auto-load`
1615

17-
The best way to integrate varlock into a Node.js application (⚠️ version 22 or higher) is to import the `varlock/auto-load` module. This uses `execSync` to call out to the varlock CLI, sets resolved env vars into `process.env`, and initializes varlock's runtime code, including:
16+
The best way to integrate varlock into a plain Node.js application (⚠️ version 22 or higher) is to import the `varlock/auto-load` module. This uses `execSync` to call out to the varlock CLI, sets resolved env vars into `process.env`, and initializes varlock's runtime code, including:
1817

1918
- varlock's `ENV` object
2019
- log redaction (if enabled)
@@ -55,6 +54,18 @@ In `package.json` scripts, calling `varlock` directly will work, as your package
5554

5655
Even when using a deeper integration for your code, you may still need to use `varlock run` when calling external scripts/tools, like database migrations, to pass along resolved env vars.
5756

57+
:::tip[Setting the current environment]
58+
Varlock can load multiple environment-specific `.env` files (e.g., `.env.development`, `.env.production`) by using the [`@currentEnv` root decorator](/reference/root-decorators/#currentenv) to specify which env var will set the current environment.
59+
60+
If not using the default (usually `development`), you'll want to pass it in as an environment variable when running your command.
61+
62+
```bash
63+
APP_ENV=production varlock run -- node index.js
64+
```
65+
66+
See the [environments guide](/guides/environments) for more information about how to set the current environment.
67+
:::
68+
5869

5970

6071
## Front-end frameworks

0 commit comments

Comments
 (0)