Fix gatsby-plugin-image
webpack issue with global
#30658
Closed
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.
Overview
The
gatsby-plugin-image
plugin doesn't work nicely with plugins that rely on theglobal
variable.This pull request attempts to address it.
Problem
The plugin adds the following
webpack
configuration:gatsby/packages/gatsby-plugin-image/src/gatsby-node.ts
Lines 22 to 42 in a217661
This causes the following error
Solution
I think that the
[global.GATSBY___IMAGE]
key is causing the issue.I've looked at other official Gatsby plugins that use the
plugins.define()
method to define global variables,and haven't come across a key that matches this pattern.
Essentially,
[global.GATSBY___IMAGE]
is changed toGATSBY_IMAGE
.Testing
With the changes made, the tests are unaffected,
i.e., the same tests that failed prior to the changes, fail now as well:
Full Test Log
I am not sure how to build the plugin and use it with my site with these new changes.
Hence, I haven't tested if the changes actually fix the issue.
Conclusion
I think that since the changes made to the plugin
keep it consistent with other official plugins that require global variables,
it should fix the issue.
A few changes to the codebase have swapped
global
forwindow
.This might cause a few issues, and require review.
I have not done any rigorous testing other than running the tests before and after the changes.
Any suggestions are welcome!
I would love some feedback about this fix and where it could be improved.
Related Issues
global
is not defined #30093]: [gatsby-plugin-image] causes Uncaught ReferenceError: global is not definedglobal
is not defined