Vite Env
The Laravel TypeScript Publisher reads the VITE_ variables from your project's .env file and generates a vite-env.d.ts declaration file. The file augments Vite's own ImportMetaEnv interface, so import.meta.env.VITE_APP_NAME is typed on the frontend without a declaration file you maintain by hand.
The feature is on by default. It needs no @tolki/ts runtime, no attributes, and no per-class configuration.
Anatomy of the Generated File
Given a .env file that contains these variables:
APP_NAME=MyApp
DB_CONNECTION=mysql
VITE_APP_NAME="${APP_NAME}"The package generates this vite-env.d.ts:
/// <reference types="vite/client" />
interface ImportMetaEnv {
readonly VITE_APP_NAME: string;
}
interface ImportMeta {
readonly env: ImportMetaEnv;
}The output follows three rules:
- Only
VITE_variables:APP_NAMEandDB_CONNECTIONare skipped. This matches Vite's own convention for which environment variables reach client-side code. - Merged with Vite's types:
/// <reference types="vite/client" />pulls in Vite's own ambient types, so theseImportMetaEnvandImportMetadeclarations merge with Vite's base declarations instead of replacing them. - Always
string: every variable is typed asstring, whatever its value in.env(true,123, and so on), because Vite exposes.envvalues inimport.meta.envas strings.
If the source file doesn't exist, or it has no VITE_ variables, no file is written.
Source File Resolution
The package reads the first source file it finds, in this order:
vite_env.source_file, if you set it. It can be an absolute path, or a path relative to the project root..env, if it exists at the project root..env.example, as the fallback. This helps in CI or a fresh clone, where the gitignored.envmay not exist yet but the committed.env.exampledoes.
This example reads a production env file instead:
// config/ts-publish.php
'vite_env' => [
'source_file' => '.env.production',
],Parsing Rules
The package reads variable names only, one line at a time, with these rules:
- Each line is trimmed before it's checked.
- Blank lines and lines that start with
#are skipped. - A line with no
=is skipped, because there's no name to read. - The name is everything before the first
=. Values, quotes, and inline comments aren't read or validated. - Only names that start with
VITE_are kept. Everything else, such asAPP_NAMEandDB_CONNECTION, is ignored. - The names are sorted alphabetically, and duplicates are removed.
export Prefixes
A line written as export VITE_APP_NAME=MyApp is skipped, because its name doesn't start with VITE_. Remove the export prefix to include the variable.
Output Location
The file goes to vite_env.output_directory if you set it, and to the global output_directory otherwise. vite_env.filename sets the filename, which defaults to vite-env.d.ts.
The Vite plugin doesn't watch your .env file. After you add or remove a VITE_ variable, run php artisan ts:publish to update the declarations.
No Filtering, Attributes, or Per-Item Config
Vite Env has no PHP classes to publish, so it has no included, excluded, or additional_directories settings and doesn't support #[TsExclude]. The same is true of Broadcast Channels.
To leave a variable out, don't prefix it with VITE_. Vite won't expose it to client code either. To turn off the feature, set vite_env.enabled to false.
Configuration Reference
The Configuration Reference lists every vite_env.* key.