-
-
Notifications
You must be signed in to change notification settings - Fork 473
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(index): enable HMR in case locals
(css-modules
) are unchanged
#298
Changes from 1 commit
9d96c43
3bb5ae5
19ba925
1007d40
b516fc0
0d52dee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,13 +23,34 @@ module.exports.pitch = function (request) { | |
"// Hot Module Replacement", | ||
"if(module.hot) {", | ||
" // When the styles change, update the <style> tags", | ||
" if(!content.locals) {", | ||
" module.hot.accept(" + loaderUtils.stringifyRequest(this, "!!" + request) + ", function() {", | ||
" var newContent = require(" + loaderUtils.stringifyRequest(this, "!!" + request) + ");", | ||
" if(typeof newContent === 'string') newContent = [[module.id, newContent, '']];", | ||
" module.hot.accept(" + loaderUtils.stringifyRequest(this, "!!" + request) + ", function() {", | ||
" var newContent = require(" + loaderUtils.stringifyRequest(this, "!!" + request) + ");", | ||
" if(typeof newContent === 'string') newContent = [[module.id, newContent, '']];", | ||
" var localsSame = true;", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess. But isn’t that a worse name? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic checks for identity of the locals (it's in the code), so if the name // Do we have (new) locals => no HMR
if (!locals) throw new Error('..') There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your comment is wrong.
In other words:
|
||
" if(!content.locals && newContent.locals) {", | ||
" localsSame = false;", | ||
" } else if(content.locals && !newContent.locals) {", | ||
" localsSame = false;", | ||
" } else if(content.locals && newContent.locals) {", | ||
" var oldKeys = Object.keys(content.locals);", | ||
" var newKeys = Object.keys(newContent.locals);", | ||
" if(oldKeys.length !== newKeys.length) {", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const length = Object.keys(content.locals).length - Object.keys(newContent.locals).length
if (length === 0) {
...
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? I find this harder to understand. And I use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, sry I meant if (!length) {
...
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in the latest commit. |
||
" localsSame = false;", | ||
" } else {", | ||
" localsSame = oldKeys.every(function(oldKey) {", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Object.keys(content.locals).every(function (key) {
return content.locals[key] === newContent.locals[key]
}) |
||
" var oldValue = content.locals[oldKey];", | ||
" var newValue = newContent.locals[oldKey];", | ||
" return oldValue === newValue;", | ||
" });", | ||
" }", | ||
" }", | ||
" if(localsSame) {", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comparison between old and new locals is the gist of this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (!locals) throw new Error('...') |
||
" update(newContent);", | ||
" });", | ||
" }", | ||
" } else {", | ||
" // This error is caught and not shown and causes a full reload.", | ||
" throw new Error('Aborting CSS HMR due to changed css-modules locals.');", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked through https://webpack.js.org/api/hot-module-replacement/ and tried to read the HMR source code in the webpack repo, and could not find any other way of aborting an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will take a look into this, I think it's fine |
||
" }", | ||
" });", | ||
" // When the module is disposed, remove the <style> tags", | ||
" module.hot.dispose(function() { update(); });", | ||
"}" | ||
|
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 I removed this check, I dedented the code so the diff looks a little bit larger than it is.