new webpack.DefinePlugin({ PRODUCTION: JSON.stringify(true), VERSION: JSON.stringify("5fa3b9"), BROWSER_SUPPORTS_HTML5: true, TWO: "1+1", "typeof window": JSON.stringify("object") })
https://github.com/webpack/docs/wiki/list-of-plugins#defineplugin
This seems very unusual, unnecessary and "dev-error-prone".
Is it type-checking concerns?
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.
The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
Oct 29, 2021. Webpack's DefinePlugin() function lets you replace a given token in the compiled code with another token. A common use case is using it to define environment variables when you cannot use an . env file directly.
The answer is below the example:
- If the value is a string it will be used as a code fragment.
- If the value isn't a string, it will be stringified (including functions).
I.e. the value of a string is inserted into the source code verbatim.
Passing JSON.stringify(true)
or passing true
directly is the same, since non-string values are converted to strings.
However, there is a big difference between JSON.stringify('5fa3b9')
and "5fa3b9"
:
Assuming your code was
if (version === VERSION)
then VERSION: JSON.stringify('5fa3b9')
would result in
if (version === "5fa3b9")
but VERSION: "5fa3b9"
would result in
if (version === 5fa3b9)
which is invalid code.
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 either alternate quotes, such as '"production"', or by using JSON.stringify('production').
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With