mirror of https://github.com/webpack/webpack.git
1.2 KiB
1.2 KiB
This example demonstrates using the DotenvPlugin via the dotenv top-level configuration option.
The DotenvPlugin loads environment variables from .env files and exposes them in your application through process.env.
Features Demonstrated
- Basic Environment Variables: Load variables with
WEBPACK_prefix - Variable Expansion: Reference other variables using
${VAR_NAME}syntax - Default Values: Use
${VAR:-default}for fallback values - Security: Non-prefixed variables are not exposed to the bundle
- Build-time Replacement: Variables are replaced at compile time for better optimization
Configuration
By default, the plugin:
- Loads
.envfile from the project root - Only exposes variables with
WEBPACK_prefix - Supports variable expansion and default values
- Replaces
process.env.WEBPACK_*with actual values at build time
.env
Environment variables file (.env.local and .env.*.local are ignored by git):
_{{.env}}_
example.js
_{{example.js}}_
webpack.config.js
_{{webpack.config.js}}_
dist/output.js
_{{dist/output.js}}_
Info
Unoptimized
_{{stdout}}_
Production mode
_{{production:stdout}}_