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.jsconstEnvTypesPlugin=require('@xxanderwp/env-types-webpack-plugin').EnvTypesPlugin;module.exports={// ...plugins: [newEnvTypesPlugin('src/types/env.d.ts')],};// webpack.config.jsconstEnvTypesPlugin=require('@xxanderwp/env-types-webpack-plugin').EnvTypesPlugin;module.exports={// ...plugins: [newEnvTypesPlugin({// 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.000ZdeclarenamespaceNodeJS{interfaceProcessEnv{/** * Database configuration */DB_HOST?: string;/** * Database configuration */DB_PORT?: string;/** * API Keys * Production key */API_KEY?: string;}}export{};// Now you have full TypeScript autocomplete!constdbHost=process.env.DB_HOST;// Type: string | undefinedconstapiKey=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:
newEnvTypesPlugin('src/types/env.d.ts');// Equivalent to:newEnvTypesPlugin({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.