The content of this section is derived from the content of the following links and is subject to the CC BY 4.0 license.
The following contents can be assumed to be the result of modifications and deletions based on the original contents if not specifically stated.
The EnvironmentPlugin
is shorthand for using the DefinePlugin
on process.env
keys.
string[] | Record<string, string>
The EnvironmentPlugin
accepts either an array of keys or an object mapping its keys to their default values.
This is equivalent to the following DefinePlugin
application:
Not specifying the environment variable raises an "EnvironmentPlugin
- ${key}
environment variable is undefined" error.
Alternatively, the EnvironmentPlugin
supports an object, which maps keys to their default values. The default value for a key is taken if the key is undefined in process.env
.
Variables coming from process.env
are always strings.
Unlike DefinePlugin
, default values are applied to JSON.stringify
by the EnvironmentPlugin
.
Default values of null
and undefined
behave differently. Use undefined
for variables that must be provided during bundling, or null
if they are optional.
If an environment variable is not found during bundling and no default value was provided, Rspack will throw an error instead of a warning.
Let's investigate the result when running the previous EnvironmentPlugin
configuration on a test file entry.js
:
When executing NODE_ENV=production
Rspack in the terminal to build, entry.js
becomes this:
Running DEBUG=false
Rspack yields:
The following EnvironmentPlugin
configuration provides process.env.GIT_VERSION
(such as "v5.4.0-2-g25139f57f") and process.env.GIT_AUTHOR_DATE
(such as "2020-11-04T12:25:16+01:00") corresponding to the last Git commit of the repository:
The third-party DotenvPlugin
(dotenv-webpack
) allows you to expose (a subset of) dotenv variables: