You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 8293bde
Browse filesBrowse the repository at this point in the historyBrowse files
feat(dotenv): type ENV through a vite-plus varlock plugin, add no varlock dependency
The generated types augment `varlock/env`, which TypeScript resolves from
the project, so `ENV` needed a direct `varlock` dependency. Ship a varlock
plugin in `vite-plus` (the `./plugin` export that varlock requires) that
registers `@generateVitePlusTypes`. It runs varlock's own TypeScript
generator and adds the same augmentation for `vite-plus/dotenv`, so
varlock writes the final file itself on every load. The plugin is one
CommonJS file: varlock runs `.cjs` plugins with its own
`varlock/plugin-lib`, so it works with the varlock copy that loads it.
`vp dotenv init` now leaves `package.json` unchanged. It writes the
dependency only so that `varlock init` skips its own install, restores
the file, switches the new schema to `@plugin(vite-plus)
@generateVitePlusTypes(..., importMetaEnv=none)`, and runs one load to
write the types. A project that already depends on `varlock`, or has no
`vite-plus` in node_modules, keeps the plain `varlock init` behavior.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -41,14 +42,16 @@ Run `vp dotenv init` in a project that already has `.env` files:
41
42
vp dotenv init
42
43
```
43
44
44
-
`init` scans your `.env` files and your code, writes `.env.schema` with decorators inferred for each variable, and adds `!.env.schema` to `.gitignore` when the file would otherwise be ignored. Review the schema and mark each secret with `@sensitive`. In a project with a `package.json`, `init` also adds `@generateTsTypes(path=env.d.ts)`. Add `importMetaEnv=none` to it. Otherwise the generated types add every variable to `import.meta.env`, sensitive ones included, but Vite exposes only `VITE_*` variables there.
45
+
`init` scans your `.env` files and your code, writes `.env.schema` with decorators inferred for each variable, and adds `!.env.schema` to `.gitignore` when the file would otherwise be ignored. Review the schema and mark each secret with `@sensitive`.
45
46
46
-
`vp dotenv init` also adds `varlock` to the `dependencies` in `package.json`, at the version that Vite+ bundles, and runs `vp install`. Keep it if TypeScript code reads `ENV`. `ENV` gets its keys only from the types that `@generateTsTypes` writes. Those types extend the `varlock/env` module, and TypeScript must resolve that module from your project. With pnpm, the copy that Vite+ bundles is not visible from your project, so every `ENV` property is a type error. Keep the dependency at the version that Vite+ bundles (`vp --version` lists it), for two reasons:
47
+
`vp dotenv init` also sets up the types for `ENV`. `varlock init` writes `@generateTsTypes(path=env.d.ts)`, and `vp dotenv init` replaces it with `@plugin(vite-plus)` and `@generateVitePlusTypes(path=env.d.ts, importMetaEnv=none)`. The Vite+ generator writes the same types, and also makes them apply to `ENV`from `vite-plus/dotenv`. `importMetaEnv=none` leaves `import.meta.env` to Vite's own types, because Vite exposes only `VITE_*` variables there.
47
48
48
-
- With the same version, the package manager installs one copy, so `ENV` from `vite-plus/dotenv` gets the generated types.
49
-
- The Vite integration runs the varlock CLI that it finds in your project's `node_modules/.bin`, and it reads the output of that CLI. Another version can change that output.
49
+
Your project needs no `varlock` dependency. Vite+ bundles the CLI and the runtime, and the generated types reach `ENV` through `vite-plus/dotenv`. `varlock init` adds `varlock` to `package.json` by default, but `vp dotenv init` prevents that and leaves `package.json` unchanged. Add `varlock` yourself only in these cases:
50
50
51
-
If the init fails or you cancel it, `vp dotenv init` restores `package.json`. If no TypeScript code reads `ENV`, for example in a JavaScript project, you can remove the dependency. Vite+ then uses its bundled varlock everywhere.
51
+
- Code imports `ENV` from `varlock/env` and you use TypeScript, which must resolve that module from your project.
52
+
- A production server starts with `varlock run` (see [Server-side rendering](#server-side-rendering)).
53
+
54
+
If you add it, use the version that Vite+ bundles (`vp --version` lists it). The Vite integration runs the varlock CLI that it finds in your project's `node_modules/.bin`, and another version can change the output that the integration reads. In a project without `vite-plus` in `node_modules`, `vp dotenv init` runs `varlock init` unchanged.
52
55
53
56
In CI or for a coding agent, run `vp dotenv init --no-interactive` to use the default answers instead of prompts.
54
57
@@ -62,20 +65,20 @@ ENV.API_PORT; // number, not string
62
65
ENV.STRIPE_SECRET_KEY; // sensitive: never inlined into client code
63
66
```
64
67
65
-
`import.meta.env.VITE_*` keeps working for variables with the `VITE_` prefix. `ENV` is the recommended API: it is typed, it coerces numbers and booleans, and it does not need a prefix. Code that imports `ENV` from `varlock/env`, as in the varlock documentation, gets the same object.
68
+
`import.meta.env.VITE_*` keeps working for variables with the `VITE_` prefix. `ENV` is the recommended API: it is typed, it coerces numbers and booleans, and it does not need a prefix. Code that imports `ENV` from `varlock/env`, as in the varlock documentation, gets the same object. With TypeScript, that import needs a direct `varlock` dependency.
66
69
67
70
## The `.env.schema` file
68
71
69
72
A schema is a `.env` file with JSDoc-style comments. Lines above `# ---` hold root decorators, which apply to the whole file. A comment directly above a variable attaches decorators to that variable.
|`@defaultSensitive=false`| Variables are public unless marked `@sensitive`. Use `true` to flip the default. |
76
-
|`@defaultSensitive=inferFromPrefix('VITE_')`| Vite compatibility: `VITE_*` is public, everything else is sensitive. |
77
-
|`@currentEnv=$APP_ENV`| The variable that selects the environment, and so which `.env.[env]` files load. |
78
-
|`@generateTsTypes(path='env.d.ts', importMetaEnv=none)`| Write a type declaration for `ENV` on every load. Add the file to `tsconfig.json`. `importMetaEnv=none` leaves `import.meta.env` to Vite's own types. |
|`@defaultSensitive=false`| Variables are public unless marked `@sensitive`. Use `true` to flip the default.|
79
+
|`@defaultSensitive=inferFromPrefix('VITE_')`| Vite compatibility: `VITE_*` is public, everything else is sensitive.|
80
+
|`@currentEnv=$APP_ENV`| The variable that selects the environment, and so which `.env.[env]` files load.|
81
+
|`@plugin(vite-plus)``@generateVitePlusTypes(path='env.d.ts', importMetaEnv=none)`| Write a type declaration for `ENV`from `vite-plus/dotenv` and `varlock/env`on every load. It takes the options of `@generateTsTypes`. Add the file to `tsconfig.json`. `importMetaEnv=none` leaves `import.meta.env` to Vite's own types. |
'Set up `.env.schema` to type `ENV` from `vite-plus/dotenv` with `@plugin(vite-plus)` and `@generateVitePlusTypes`. The project needs no `varlock` dependency.',
74
+
);
75
+
}
51
76
return0;
52
77
}
53
78
@@ -65,7 +90,7 @@ async function main(): Promise<number> {
65
90
return1;
66
91
}
67
92
68
-
letvarlock: {cli: string;version: string};
93
+
letvarlock: VarlockPackage;
69
94
try{
70
95
varlock=resolveVarlockPackage();
71
96
}catch(error){
@@ -84,12 +109,10 @@ async function main(): Promise<number> {
0 commit comments