Vite Env
The Laravel TypeScript Publisher reads the VITE_-prefixed variables from your project's .env file and generates a vite-env.d.ts declaration file that augments Vite's own ImportMetaEnv interface — so import.meta.env.VITE_APP_NAME is fully typed on the frontend without hand-maintaining a separate declaration file.
As mentioned in Installation & Usage, this is the simplest generator in the package: no @tolki/ts runtime, no attributes, no per-item filtering — just a source file scan and a template render.
How the Declaration File Is Generated
- Resolve the source file —
vite_env.source_fileif configured; otherwise.envif it exists; otherwise.env.example. - Parse
VITE_-prefixed variables — read the source file line by line, skip blank lines and#comments, extract the variable name before the=on each remaining line, and keep only names starting withVITE_. - Sort and deduplicate the variable names.
- Render
vite-env.d.tsfrom the variable list. If noVITE_-prefixed variables were found (or the source file doesn't exist), nothing is generated — the writer returns an empty string and no file is written.
Anatomy of the Generated File
Given a .env file containing:
APP_NAME=MyApp
DB_CONNECTION=mysql
VITE_APP_NAME="${APP_NAME}"The package generates vite-env.d.ts:
/// <reference types="vite/client" />
interface ImportMetaEnv {
readonly VITE_APP_NAME: string;
}
interface ImportMeta {
readonly env: ImportMetaEnv;
}- Only
VITE_APP_NAMEis included —APP_NAMEandDB_CONNECTIONare skipped since they don't start withVITE_, matching Vite's own convention for which environment variables get exposed to client-side code. /// <reference types="vite/client" />pulls in Vite's own ambient types so theImportMetaEnv/ImportMetadeclarations here merge with (rather than replace) Vite's base declarations.- Every variable is typed as
string— regardless of the value written in the.envfile (true,123, etc.), since Vite always provides raw strings at runtime viaimport.meta.env.
Source File Resolution
The source file is resolved with this priority:
vite_env.source_file, if explicitly configured — an absolute path, or a path relative to the project root..env, if it exists at the project root..env.example, as the final fallback — useful in CI or a fresh clone where.env(gitignored) may not exist yet, but.env.example(committed) does.
// config/ts-publish.php
'vite_env' => [
'source_file' => '.env.production',
],Parsing Rules
- Lines are processed one at a time; leading/trailing whitespace is trimmed before checking.
- Blank lines and lines starting with
#(comments) are skipped. - A line without an
=is skipped — there's nothing to extract a variable name from. - The variable name is everything before the first
=on the line; values, quotes, and inline comments after the value are not parsed or validated, only the name matters. - Only names starting with
VITE_are kept; everything else (APP_NAME,DB_CONNECTION, etc.) is silently ignored. - The final list is sorted alphabetically and deduplicated before being passed to the template.
Output Location
The output directory is resolved with this priority:
vite_env.output_directory, if set.- The global
output_directory.
The filename is controlled by vite_env.filename (default vite-env.d.ts).
No Filtering, Attributes, or Per-Item Config
Like Broadcast Channels, Vite Env is a single-output feature with no per-class collection — there's no included/excluded/additional_directories config, and no #[TsExclude] support, since there's no PHP class to reflect on. To exclude a specific variable, simply don't prefix it with VITE_ (Vite itself won't expose it to client code either), or disable the feature entirely with vite_env.enabled = false.
Configuration Reference
The full list of vite_env.* config keys lives in the Configuration Reference.