-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warning: Introduce SCRIPT_DEBUG
to make the package compatible with webpack 5
#50122
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,5 @@ interface Process { | |
env: Environment; | ||
} | ||
declare var process: Process; | ||
|
||
declare var SCRIPT_DEBUG: boolean; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though the
process
polyfill is no longer present, it shouldn't matter forprocess.env.NODE_ENV
, because it's a special constant defined and replaced by theDefinePlugin
. This plugin will expandor
into
eliminating the
process
reference from the output.But the
DefinePlugin
doesn't undestand whatprocess
orprocess.env
is and will leave it in the output.Therefore, the easiest fix for the bug is to use:
Here
DefinePlugin
can do the expansion and keeps the logic where:process
orprocess.env
doesn't exist,isDev
is alwaysfalse
.isDev
istrue
iffNODE_ENV
is notproduction
.Adding support for
SCRIPT_DEBUG
is nice, but I think it's a mistake to assume thatprocess.env.NODE_ENV
is bad and no longer supported.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but it only fixes the bug for the
@wordpress/warning
usage with the integrated Babel plugin. It doesn’t help with the case where someone wants to useprocess.env.NODE_ENV
in other packages to guard the usage of code. They would have to use something like( process?.env?.NODE_ENV ?? ‘production’ ) !== ‘production’
to avoid errors when theDefinePlugin
is not replacing the entry. Therefore I think thatSCRIPT_DEBUG
is way simpler to use. Folks outside of WordPress core and the Gutenberg plugin can use the longer version withDefinePlugin
, if they prefer it overSCRIPT_DEBUG
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When would such an error happen? It won't happen in Node.js, and it won't happen in webpack (if
process.env.NODE_ENV
is written in such a way thatDefinePlugin
can fully replace it). It can happen in a browser, when the@wordpress/warning
package is imported with the native ES module loader. Is that the case we try to guard against here?It's increasingly common that packages ship separate ES modules for Node and for browsers. Specified in the
exports
field. If we decided to do that, too, presence ofprocess.env.NODE_ENV
would be on of the differences.When testing this PR, I see one odd thing: the built script for the
warning
package, inbuild/warning/index.js
, theSCRIPT_DEBUG
variable is replaced away, and in production version thewarning
function is empty.SCRIPT_DEBUG
global will never enable it again.So, do we really want to specify
SCRIPT_DEBUG
withDefinePlugin
? Wouldn't it be better to keep the references to the global in the built script?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that is the expected behavior with this plugin, yes. It‘s a global that‘s compiled away during build
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the bug that was filed initially in #44950. The current webpack configuration doesn't do anything with
process.env.NODE_ENV
.Yes, that is expected as it aligns with how WordPress core loads files. See a more detailed explanation in #50122 (comment).