-
Notifications
You must be signed in to change notification settings - Fork 10.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
fix(gatsby): fix some css HMR edge cases #29839
Conversation
…related to css HMR
c5afaf5
to
5cfc027
Compare
|
||
apply(compiler: Compiler): void { | ||
compiler.hooks.thisCompilation.tap(this.name, compilation => { | ||
compilation.hooks.fullHash.tap(this.name, () => { |
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.
this entire hack is largely based on checking what HMR internal webpack plugin does ( https://github.com/webpack/webpack/blob/04a7d052b5c04628257ec23244f234622c7f394c/lib/HotModuleReplacementPlugin.js#L323-L411 ) and "tricking it" into thinking that blank.css
file we import (
gatsby/packages/gatsby/cache-dir/app.js
Line 21 in 40e54e8
import "./blank.css" |
a) there is new css module or hash of already existing one changed (we set
anyCssChanged
to true
)b) when css module that was in previous compilation is no longer there (that's combo of
seenCssInThisCompilation
and this.previouslySeenCss
)
/** | ||
* This is total hack that is meant to handle: | ||
* - https://github.com/webpack-contrib/mini-css-extract-plugin/issues/706 | ||
* - https://github.com/webpack-contrib/mini-css-extract-plugin/issues/708 | ||
* The way it works it is looking up what HotModuleReplacementPlugin checks internally | ||
* and tricks it by checking up if any modules that uses mini-css-extract-plugin | ||
* changed or was newly added and then modifying blank.css hash. | ||
* blank.css is css module that is used by all pages and is there from the start | ||
* so changing hash of that _should_ ensure that: | ||
* - when new css is imported it will reload css | ||
* - when css imported by not loaded (by runtime) page template changes it will reload css | ||
*/ |
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.
All of this does also rely on fact that we extract css to single commons.css
file. To be fair - fact that we do that is also part of the problem (particularly issue about not visited modules that import css)
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.
Looks good, added 2 small comments
packages/gatsby/src/utils/webpack/force-css-hmr-for-edge-cases.ts
Outdated
Show resolved
Hide resolved
packages/gatsby/src/utils/webpack/force-css-hmr-for-edge-cases.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Ward Peeters <ward@coding-tech.com>
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.
Thanks, this looks wonderful! 👍
* test(e2e-development-runtime): add test cases for various edge cases related to css HMR * hackity hack * Update packages/gatsby/src/utils/webpack/force-css-hmr-for-edge-cases.ts Co-authored-by: Ward Peeters <ward@coding-tech.com> * add some comments with explanation Co-authored-by: Ward Peeters <ward@coding-tech.com> (cherry picked from commit 52facaf)
* test(e2e-development-runtime): add test cases for various edge cases related to css HMR * hackity hack * Update packages/gatsby/src/utils/webpack/force-css-hmr-for-edge-cases.ts Co-authored-by: Ward Peeters <ward@coding-tech.com> * add some comments with explanation Co-authored-by: Ward Peeters <ward@coding-tech.com> (cherry picked from commit 52facaf) Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
Description
Add test cases for issues described in:
splitChunks
that extracts all css to single file webpack-contrib/mini-css-extract-plugin#708Current status (tests for removing imports only pass because adding imports doesn't do anything :) ):
With this veeeeeery hacky
fixworkaround:Related Issues
[ch25603]
[ch25604]