Fix Webpack 4 DefinePlugin config #1286
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What?
I've found that the usage of
webpack.DefinePlugin
at Cornerstone's Webpack configuration is wrong:According to official DefinePlugin documentation, if the definition value is a string, it will be used as a code fragment.
The plugin does a direct text replacement. The recommended way, broadly used, and also referred in official documentation, is to wrap the string in a
JSON.stringify
call:This way, in code,
process.env.NODE_ENV
will be properly substituted by the string"development"
, instead of the literal code snippetdevelopment
, which results in a'development' is not defined
error becausedevelopment
is considered an undefined variable.I've stumbled upon this issue when adding Vue.js and vue-loader as dependencies. When bundling Vue.js, Webpack substitutes
process.env.NODE_ENV
in Vue.js code. Due to the misconfiguration, Vue.js fails with adevelopment is not defined
error.Tickets / Documentation
Post Scriptum
In fact, this whole
DefinePlugin
usage in Cornerstone config seems obsolete, now that Webpack 4 automatically uses themode
configuration value forprocess.env.NODE_ENV
replacements.I have completely removed the whole
DefinePlugin
usage from Webpack config locally, and everything worked. If we were setting other definitions, or if we wanted to overridemode
value inprocess.env.NODE_ENV
, then it would make sense to keep usingDefinePlugin
.