This repository has been archived by the owner on May 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 512
feat: support hot module replacement (HMR) #457
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module.exports = function(compilationHash, publicPath, outputFilename) { | ||
if (document) { | ||
var origin = document.location.protocol + '//' + document.location.hostname + (document.location.port ? ':' + document.location.port: ''); | ||
var styleSheets = document.getElementsByTagName('link'); | ||
for (var i = 0; i < styleSheets.length; i++) { | ||
if (styleSheets[i].href) { | ||
var hrefUrl = styleSheets[i].href.split('?'); | ||
var href = hrefUrl[0]; | ||
var hash = hrefUrl[1]; | ||
if (hash !== compilationHash && href === origin + publicPath + outputFilename) { | ||
var url = href + '?' + compilationHash; | ||
styleSheets[i].href = url; | ||
console.log('[HMR]', 'Reload css: ', url); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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.
Without moving to an earlier hook, the
%%extracted-file%%
, etc. replacements wouldn't take the first time around.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.
Please add a comment and ideally a test to cover that.
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.
Comment added.
How would I do the test? It looks like there aren't any existing tests that check the contents of the JS file from which we're extracting. I could probably add the entire built file to the
expected
directory, but then it would break every time webpack updates the prologue code.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.
Maybe the question is what's worth testing. It could be possible there is no good way just yet.
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 change is the source of problem I mentioned in #457 (comment)
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.
@cletusw also, because of changing
additional-assets
tobefore-chunk-assests
webpack does not produce source maps anymore :(I am not sure how to fix this..
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.
@mieszko4 Yeah, I don't know enough about webpack or this code to know why one hook works and not the other, I just tried it on a whim and it fixed HMR. Since the maintainers are not planning on merging this PR I'm not inclined to work on it any further. Feel free to make a PR to my fork if you can come up with a good solution and I'd be happy to look at it!
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.
@cletusw I would help but unfortunately I do not know enough about webpack either..
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.
@cletusw by the way I was able to get
"additional-assets"
to work so one hook could be used: https://github.com/faceyspacey/extract-css-chunks-webpack-plugin/blob/master/index.js#L331To begin with I did it exactly as you did with both
"before-chunk-assets"
callback and"additional-assets"
. In my case it was worse because I ended up duplicating a lot of code between the 2 callbacks.Eventually however I went back and give it a try with the original callback and it worked. To be honest I'm not even sure what was different. My guess is it had to do with this line:
https://github.com/faceyspacey/extract-css-chunks-webpack-plugin/blob/master/index.js#L377
where I essentially overrode the
_cachedSource
source, thereby hopping over any limitations in what you can do in each callback. I think likely once the source is cached it's sealed to future callbacks, and what I did to stripe out css injection (so that one of my 2 types of javascript chunks doesnt waste bytes having css in it) overrode that.Basically what I did is likely a hack and not the way the designers of webpack would do this, which is why it's imperative they chime, examine the state of affairs, and give direction before merging things back into ETWP and risking incompatibility and other errors to its large userbase. And to be honest, I also think the plugin should be re-written from the ground-up. No offense to anyone, but it's impossible to understand what this code is doing without hours and hours of research (and I did just that; days and days of playing with it; I'm ashamed to say 2 weeks, 4 hours a day). The code is procedural with huge methods. No pure functions--and by that I mean these functions are sharing state/variables all over the place. It needs to be broken into many smaller well-named pure functions so anyone has any hope of understanding it. I also don't understand why no line breaks are used--there are no logical groupings of related code. To be sure, half the problem is that it was my first time delving into the in-memory structure representing your bundle, but no punches were pulled to make it readable to anyone without in-depth knowledge of Webpack. I'm not sure if the whole codebase is like this, but I'm seeing it's written in plain js with no transpiler. Not that transpiling is the save-all solution, but basically the code could stand to being upgraded to learning from idiomatic best practices over the last 2 years. I love webpack, but after looking at what was going on underneath the hood in a few of these plugins, I'm basically shocked. That it all works is a miracle. I think just what we're hearing in this issue shows how scared people are of dealing with its codebase. Like no other popular codebase in Reactlandia, everyone in the back of their head intuits that only @sokra 's knows how to deal with it. That's why this past year when other people stepped up it was so apparent that it was a big deal, i.e. something that needed to happen that was hard to happen. I assume they still have growing pains for days. A huge codebase was built by one person in an antiquated procedural style that does everything including the ceiling fan. There's no other way to put it. Hats off to @sokra for gluing this all together and making it work. But Webpack 3.0 is gonna need a major refactoring if it doesn't want to get beaten out by a team that takes all its lessons and does it more extensibly before it's 4.0. That all said, I haven't examined 95% of the code. I'm mainly going off a few plugins. So if they are anomalies, great! I don't know what else to say. I only share this because I think it needs to be heard. Maybe it's said a bunch elsewhere--I dunno, I try to stay focused in application code as much as possible vs. building/bundling. Everybody knows bundling/building is not sexy and fun to do--thats why I have nothing but profound appreciation that @sokra ever built this. ...anyway this is just a plugin; forget saving the world; this particular plugin should be built from the ground-up. If it was, the challenge of getting our PRs merged wouldn't be the problem it is. At the very least the person that built it simply should write a bunch of comments in the code.
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.
@faceyspacey I hear you. I think even Tobias agrees that the current implementation of the plugin is a hack. Note that webpack-defaults gives a transpilation step and makes it possible to rewrite a lot of code in a newer style. There's still the problem of legacy as you definitely don't want to break anything but it's a step to the right direction at least.