Webpack plugin that automatically generates TypeScript definitions for environment variables from .env files.
- π Automatic generation - Types are generated on every build and when
.envfiles change - π Comment preservation - JSDoc comments from your
.envfiles are included in generated types - β‘ Fast rebuilds - Generated
.d.tsfile is excluded from webpack watching - π― TypeScript native - Full TypeScript support with type definitions
- π§ Configurable - Customize file paths, watch patterns, and more
- πͺ Zero dependencies - No external runtime dependencies
npm install --save-dev @xxanderwp/env-types-webpack-pluginOr with yarn:
yarn add -D @xxanderwp/env-types-webpack-plugin// webpack.config.js
const EnvTypesPlugin = require('@xxanderwp/env-types-webpack-plugin').EnvTypesPlugin;
module.exports = {
// ...
plugins: [new EnvTypesPlugin('src/types/env.d.ts')],
};// webpack.config.js
const EnvTypesPlugin = require('@xxanderwp/env-types-webpack-plugin').EnvTypesPlugin;
module.exports = {
// ...
plugins: [
new EnvTypesPlugin({
// Path to output .d.ts file (required)
outputPath: 'src/types/env.d.ts',
// List of .env files to watch (optional)
envFiles: ['.env.local', '.env'],
// Disable console output (optional)
silent: false,
}),
],
};# Database configuration
DB_HOST=localhost
DB_PORT=5432
# API Keys
API_KEY=your-api-key # Production key// β οΈ AUTO-GENERATED FILE β DO NOT EDIT
// Source: .env
// Generated: 2024-01-15T10:30:00.000Z
declare namespace NodeJS {
interface ProcessEnv {
/**
* Database configuration
*/
DB_HOST?: string;
/**
* Database configuration
*/
DB_PORT?: string;
/**
* API Keys
* Production key
*/
API_KEY?: string;
}
}
export {};// Now you have full TypeScript autocomplete!
const dbHost = process.env.DB_HOST; // Type: string | undefined
const apiKey = process.env.API_KEY; // Type: string | undefined| Option | Type | Default | Description |
|---|---|---|---|
outputPath |
string |
required | Path to output .d.ts file |
envFiles |
string[] |
['.env', '.env.example'] |
List of .env files to watch (in priority order) |
generatorScript |
string |
'dist/EnvTypesGenerator.js' |
Path to custom generator script |
disablePartialType |
boolean |
false |
Disable partial types |
silent |
boolean |
false |
Disable console logs |
addExportEnds |
boolean |
false |
Add export {}; at end |
namespace |
string |
NodeJS |
Optional namespace for the generated types |
interface |
string |
ProcessEnv |
Optional interface name for the generated types |
useValuesAsTypes |
boolean |
false |
Use values as types instead of string literals |
You can pass a string directly as a shorthand for outputPath:
new EnvTypesPlugin('src/types/env.d.ts');
// Equivalent to:
new EnvTypesPlugin({ outputPath: 'src/types/env.d.ts' });- On Initial Build: The plugin generates TypeScript definitions from your
.envfiles - On
.envChanges: When you modify.envfiles in watch mode, types are regenerated - Webpack Exclusion: The generated
.d.tsfile is automatically excluded from webpack's file watching to prevent rebuild loops - Comment Extraction: Comments above or inline with environment variables become JSDoc comments in the generated types
Make sure your tsconfig.json includes the generated types:
{
"compilerOptions": {
"typeRoots": ["./node_modules/@types", "./src/types"]
},
"include": ["src/**/*"]
}- Node.js >= 14.0.0
- Webpack >= 5.0.0
MIT Β© XXanderWP
Contributions are welcome! Please feel free to submit a Pull Request.
If you find a bug or have a feature request, please open an issue on GitHub.