@@ -26,6 +26,9 @@ browsers as well.
26
26
- [ External dependencies] ( #external-dependencies )
27
27
- [ Translations] ( #translations )
28
28
- [ PHP and Twig translations] ( #php-and-twig-translations )
29
+ - [ Environment variables] ( #environment-variables )
30
+ - [ Other injected variables] ( #other-injected-variables )
31
+ - [ Other ` .env ` files] ( #other-env-files )
29
32
- [ Other WordPress focused bundlers] ( #other-wordpress-focused-bundlers )
30
33
- [ Roadmap] ( #roadmap )
31
34
- [ LICENSE] ( #license )
@@ -280,6 +283,40 @@ means you are no longer need to use `wp-cli i18n make-pot/make-json` to extract
280
283
281
284
` .mo ` files will also me compiled from all your ` .po ` files.
282
285
286
+ ## Environment variables
287
+
288
+ You can define environment variables in a ` .env ` file located in the root of you project, right by you ` package.json `
289
+ file. ` wp-bundler ` will inject any env variable defined in those that starts with ` WP_ ` .
290
+
291
+ - ` WP_API_KEY ` => injected
292
+ - ` API_KEY ` => not injected
293
+
294
+ Then in your application code you can access those by reading ` process.env.WP_API_KEY ` (or whatever the variable was
295
+ called).
296
+
297
+ The variables defined in ` .env ` will not override any environment variables already set.
298
+
299
+ ### Other injected variables
300
+
301
+ Except environment variable prefixed with ` WP_ ` ` wp-bundler ` will also inject the following variables:
302
+
303
+ - ` process.env.NODE_ENV ` : ` 'production' ` during build, ` 'development' ` in watch mode
304
+ - ` __DEV__ ` : ` false ` during build, ` true ` in watch mode
305
+ - ` __PROD__ ` : ` true ` during build, ` false ` in watch mode
306
+
307
+ ### Other ` .env ` files
308
+
309
+ Except the ` .env ` file you can use a few other ` .env ` files to inject variables from. The list below defines which files
310
+ are read during which script. Files to the left have more priority than files to the right. Meaning that variables
311
+ coming from a file to the left will override a variable coming from a file to the right.
312
+
313
+ - ` wp-bundler ` (or ` --mode prod ` ): ` .env.production.local ` > ` .env.local ` > ` .env.production ` > ` .env `
314
+ - ` wp-bundler --watch ` (or ` --mode dev ` ): ` .env.development.local ` > ` .env.local ` > ` .env.development ` > ` .env `
315
+
316
+ With this structure you could have a ` .env ` file tracked by ` git ` and then allow developers to override these defaults
317
+ with their own ` .env.local ` files, which should not be checked into ` git ` . This is the same mechanism as
318
+ [ ` create-react-app ` uses] ( https://create-react-app.dev/docs/adding-custom-environment-variables/#adding-development-environment-variables-in-env ) .
319
+
283
320
## Other WordPress focused bundlers
284
321
285
322
The following projects might interest you if ` wp-bundler ` doesn't meet your requirements.
0 commit comments