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 DefinePlugin
replaces variables in your code with other values or expressions at compile time. This can be useful for allowing different behavior between development builds and production builds. If you perform logging in your development build but not in the production build you might use a global constant to determine whether logging takes place. That's where DefinePlugin
shines, set it and forget it rules for development and production builds.
Each key passed into DefinePlugin
is an identifier or multiple identifiers joined with .
.
typeof
to the key, it's only defined for typeof calls.The values will be inlined into the code allowing a minification pass to remove the redundant conditional.
When defining values for process
prefer 'process.env.NODE_ENV': JSON.stringify('production')
over process: { env: { NODE_ENV: JSON.stringify('production') } }
. Using the latter will overwrite the process
object which can break compatibility with some modules that expect other values on the process object to be defined.
Note that because the plugin does a direct text replacement, the value given to it must include actual quotes inside of the string itself. Typically, this is done either with alternate quotes, such as '"production"'
, or by using JSON.stringify('production')
.
After passing through Rspack with no minification results in:
and then after a minification pass results in:
Enable/disable features in production/development build using feature flags.
Use a different service URL in production/development builds: