Skip to content

Commit 1e8d9ac

Browse files
committed
docs: add dotenv options
1 parent f57f360 commit 1e8d9ac

File tree

3 files changed

+320
-3
lines changed

3 files changed

+320
-3
lines changed
Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
---
2+
title: Dotenv
3+
sort: 16
4+
contributors:
5+
- xiaoxiaojx
6+
related:
7+
- title: dotenv
8+
url: https://github.com/motdotla/dotenv
9+
- title: dotenv-expand
10+
url: https://github.com/motdotla/dotenv-expand
11+
---
12+
13+
<Badge text="5.103.0+" />
14+
The `dotenv` option enables webpack's built-in environment variable loading from
15+
`.env` files. ## `dotenv`
16+
17+
`boolean` `object`
18+
19+
Enable and configure the built-in Dotenv plugin to load environment variables from `.env` files.
20+
21+
**webpack.config.js**
22+
23+
```javascript
24+
module.exports = {
25+
//...
26+
dotenv: true,
27+
};
28+
```
29+
30+
Setting `dotenv` to `true` enables the plugin with default options. For custom configuration, pass an options object:
31+
32+
```javascript
33+
module.exports = {
34+
//...
35+
dotenv: {
36+
prefix: 'WEBPACK_',
37+
dir: true,
38+
template: ['.env', '.env.local', '.env.[mode]', '.env.[mode].local'],
39+
},
40+
};
41+
```
42+
43+
## Options
44+
45+
### `prefix`
46+
47+
`string` `string[]`
48+
49+
**Default:** `'WEBPACK_'`
50+
51+
Only expose environment variables that start with the specified prefix(es). This prevents accidental exposure of sensitive variables.
52+
53+
**webpack.config.js**
54+
55+
```javascript
56+
module.exports = {
57+
//...
58+
dotenv: {
59+
prefix: 'APP_', // Only expose APP_* variables
60+
},
61+
};
62+
```
63+
64+
**Multiple prefixes:**
65+
66+
```javascript
67+
module.exports = {
68+
//...
69+
dotenv: {
70+
prefix: ['APP_', 'CONFIG_'], // Expose both APP_* and CONFIG_* variables
71+
},
72+
};
73+
```
74+
75+
T> For security reasons, an empty string `''` is not allowed as a prefix, as it would expose all environment variables.
76+
77+
### `dir`
78+
79+
`boolean` `string`
80+
81+
**Default:** `true`
82+
83+
The directory from which `.env` files are loaded.
84+
85+
- `true` - Load from the project root (context)
86+
- `false` - Disable `.env` file loading
87+
- `string` - Relative path from project root or absolute path
88+
89+
**webpack.config.js**
90+
91+
```javascript
92+
module.exports = {
93+
//...
94+
dotenv: {
95+
dir: './config', // Load from ./config directory
96+
},
97+
};
98+
```
99+
100+
**Disable loading:**
101+
102+
```javascript
103+
module.exports = {
104+
//...
105+
dotenv: {
106+
dir: false, // Only use process.env variables
107+
},
108+
};
109+
```
110+
111+
### `template`
112+
113+
`string[]`
114+
115+
**Default:** `['.env', '.env.local', '.env.[mode]', '.env.[mode].local']`
116+
117+
Template patterns for `.env` file names. Use `[mode]` as a placeholder for the webpack mode (e.g., `development`, `production`).
118+
119+
Files are loaded in the order specified, with later files overriding earlier ones.
120+
121+
**webpack.config.js**
122+
123+
```javascript
124+
module.exports = {
125+
//...
126+
mode: 'production',
127+
dotenv: {
128+
template: ['.env', '.env.production'], // Only load these two files
129+
},
130+
};
131+
```
132+
133+
**Custom patterns:**
134+
135+
```javascript
136+
module.exports = {
137+
//...
138+
dotenv: {
139+
template: [
140+
'.env',
141+
'.env.local',
142+
'.env.[mode]',
143+
'.env.[mode].local',
144+
'.env.override', // Always loaded last
145+
],
146+
},
147+
};
148+
```
149+
150+
T> The `[mode]` placeholder will be replaced with the current webpack mode. For example, in `production` mode, `.env.[mode]` becomes `.env.production`.
151+
152+
## File Priority
153+
154+
Environment files are loaded in order, with later files having higher priority:
155+
156+
1. `.env` - Loaded in all modes
157+
2. `.env.local` - Loaded in all modes, ignored by git (convention)
158+
3. `.env.[mode]` - Only loaded in specified mode (e.g., `.env.production`)
159+
4. `.env.[mode].local` - Only loaded in specified mode, ignored by git
160+
161+
Variables from later files override those from earlier files. Additionally, variables already set in `process.env` take the highest priority.
162+
163+
## Variable Expansion
164+
165+
Environment variables are automatically expanded using the [dotenv-expand](https://github.com/motdotla/dotenv-expand) syntax:
166+
167+
**.env**
168+
169+
```bash
170+
WEBPACK_API_BASE=https://api.example.com
171+
WEBPACK_API_URL=${WEBPACK_API_BASE}/v1
172+
WEBPACK_PORT=${WEBPACK_PORT:-3000} # Use WEBPACK_PORT from process.env, or 3000 as default
173+
```
174+
175+
**In your code:**
176+
177+
```javascript
178+
console.log(process.env.WEBPACK_API_URL); // "https://api.example.com/v1"
179+
console.log(process.env.WEBPACK_PORT); // Value of process.env.WEBPACK_PORT if set, otherwise "3000"
180+
```
181+
182+
T> During expansion, you can reference any variable from `process.env` (e.g., `${PORT}`), but only variables with the specified prefix (e.g., `WEBPACK_`) will be exposed in your final bundle. In the example above, `${WEBPACK_PORT:-3000}` references `WEBPACK_PORT` from `process.env` if it exists.
183+
184+
**Expansion behavior example:**
185+
186+
```bash
187+
# .env file
188+
WEBPACK_API_URL=${API_BASE:-https://default.com}/api
189+
```
190+
191+
```bash
192+
# Run with environment variable
193+
API_BASE=https://custom.com npm run build
194+
```
195+
196+
Result: `process.env.WEBPACK_API_URL` will be `"https://custom.com/api"` because `API_BASE` from `process.env` is used during expansion, even though `API_BASE` itself won't be exposed in the bundle (it lacks the `WEBPACK_` prefix).
197+
198+
## Usage Examples
199+
200+
### Basic Usage
201+
202+
Create a `.env` file in your project root:
203+
204+
**.env**
205+
206+
```bash
207+
WEBPACK_API_URL=https://api.example.com
208+
WEBPACK_FEATURE_FLAG=true
209+
SECRET_KEY=should-not-be-exposed # Won't be exposed (no WEBPACK_ prefix)
210+
```
211+
212+
**webpack.config.js**
213+
214+
```javascript
215+
module.exports = {
216+
//...
217+
dotenv: true, // Uses default prefix "WEBPACK_"
218+
};
219+
```
220+
221+
**In your application:**
222+
223+
```javascript
224+
console.log(process.env.WEBPACK_API_URL); // "https://api.example.com"
225+
console.log(process.env.WEBPACK_FEATURE_FLAG); // "true"
226+
console.log(process.env.SECRET_KEY); // undefined (not exposed)
227+
```
228+
229+
### Mode-Specific Configuration
230+
231+
Create mode-specific files:
232+
233+
**.env**
234+
235+
```bash
236+
WEBPACK_API_URL=https://api.example.com
237+
WEBPACK_DEBUG=false
238+
```
239+
240+
**.env.production**
241+
242+
```bash
243+
WEBPACK_API_URL=https://prod-api.example.com
244+
WEBPACK_DEBUG=false
245+
```
246+
247+
**.env.development**
248+
249+
```bash
250+
WEBPACK_API_URL=https://dev-api.example.com
251+
WEBPACK_DEBUG=true
252+
```
253+
254+
When building with `--mode production`, `WEBPACK_API_URL` will be `"https://prod-api.example.com"`.
255+
256+
### Multiple Prefixes
257+
258+
Expose variables with different prefixes:
259+
260+
**.env**
261+
262+
```bash
263+
APP_NAME=MyApp
264+
APP_VERSION=1.0.0
265+
CONFIG_TIMEOUT=5000
266+
CONFIG_RETRY=3
267+
PRIVATE_KEY=secret # Won't be exposed
268+
```
269+
270+
**webpack.config.js**
271+
272+
```javascript
273+
module.exports = {
274+
//...
275+
dotenv: {
276+
prefix: ['APP_', 'CONFIG_'],
277+
},
278+
};
279+
```
280+
281+
### Custom Directory and Template
282+
283+
Load environment files from a custom location with custom naming:
284+
285+
**webpack.config.js**
286+
287+
```javascript
288+
module.exports = {
289+
//...
290+
dotenv: {
291+
dir: './environments',
292+
template: ['.env.base', '.env.[mode]'],
293+
},
294+
};
295+
```
296+
297+
This will load:
298+
299+
- `./environments/.env.base`
300+
- `./environments/.env.production` (in production mode)
301+
302+
## Security Considerations
303+
304+
W> **Never commit sensitive data to version control!**
305+
306+
- Use `.gitignore` to exclude `.env.local` and `.env.[mode].local` files
307+
- Only expose environment variables with specific prefixes
308+
- Never use an empty string `''` as a prefix
309+
- Consider using different `.env` files for different environments
310+
- Store production secrets in your deployment platform's environment variables
311+
312+
**.gitignore**
313+
314+
```bash
315+
# local env files
316+
.env.local
317+
.env.*.local
318+
```

src/content/configuration/other-options.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Other Options
3-
sort: 21
3+
sort: 22
44
contributors:
55
- sokra
66
- skipjack
@@ -350,7 +350,6 @@ Snapshots for building context modules.
350350
- `hash`: Compare content hashes to determine invalidation (more expensive than `timestamp`, but changes less often).
351351
- `timestamp`: Compare timestamps to determine invalidation.
352352

353-
354353
### resolve
355354

356355
`object = {hash boolean = true, timestamp boolean = true}`

src/content/configuration/performance.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Performance
3-
sort: 16
3+
sort: 21
44
contributors:
55
- thelarkinn
66
- tbroadley

0 commit comments

Comments
 (0)