-
-
Notifications
You must be signed in to change notification settings - Fork 3.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
HMR guide rewrite #1439
HMR guide rewrite #1439
Conversation
Initial Commit
align examples to previous guides
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 for tackling this @sbaidon, this is a great start! Sorry for the delayed review -- I've been busy the last few days. I added a few comments and I'd like to let @TheDutchCoder take a look as well.
contentBase: path.resolve(__dirname, 'dist'), | ||
publicPath: '/' | ||
filename: '[name].bundle.js', | ||
path: path.resolve(__dirname, 'dist') | ||
} | ||
}; |
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 like the indentation is a bit off in the bottom section (i.e. from the closing plugins
array bracket through the final closing bracket of the whole configuration object). Note that for all our guide diffs we use 2 space indentation as a base so we can squeeze the +
and -
signs in.
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.
Yeah just use 2 spaces for indentation and 2 spaces at the start of each line in a diff block for the +
and -
.
Not too bad, huh? Let's test it out using `module.hot.accept`... | ||
You can also use the CLI to modify the [webpack-dev-server](https://github.com/webpack/webpack-dev-server) configuration with the following command: `webpack-dev-server --hotOnly`. | ||
|
||
Now let's update the `index.js` file so that when a change inside `print.js` is detected we tell webpack to accept the updated module. |
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.
I think you need to mention that the reader should npm start
their webpack-dev-server
before altering the print.js
module.
return element; | ||
} | ||
|
||
document.body.appendChild(component()); | ||
``` |
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.
You might be able to just leave out this whole section as nothing has changed from the development
guide.
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.
I did add the module.hot
part, or do you mean doing something like this:
import _ from 'lodash';
import printMe from './print.js';
if (module.hot) {
module.hot.accept('./print.js', function() {
console.log('Accepting the updated printMe module!');
printMe();
})
}
...
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.
No what you did was fine IMO, just make it a full diff ;)
Hot Module Replacement can be tricky. For example, let's say I have the following class: | ||
Hot Module Replacement can be tricky. To show this, let's go back to our working example. If you go ahead and click the button on the example page, you will realize the console is printing the old `printMe` function. | ||
|
||
This is happening because the button's `onclick` event handler is still bind to the original `printMe` function. |
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.
/s/still bind/still bound
module.hot.accept('./print.js', function() { | ||
console.log('Accepting the updated printMe module!'); | ||
document.body.removeChild(element); | ||
element = component(); // update binding now that new printMe module has been acepted |
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.
I would rewrite the comment as:
Re-render the "component" to update the click handler.
import printMe from './print.js'; | ||
|
||
if (module.hot) { | ||
module.hot.accept('./print.js', function() { |
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 line is indented two tabs instead of one. However, I think this whole block should be changed to a diff
and indented two extra lines.
``` | ||
|
||
|
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.
The extra line break is intentional -- we usually try to keep some spacing between sections (especially for h2
s).
``` | ||
|
||
This is just one example, but there are many others that can easily trip people up. Luckily, there are a lot of loaders out there, some mentioned below, that will make using this process much easier. | ||
|
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.
Add an extra line break between sections (similar to my other h2
comment above).
|
||
Hot Module Replacement with CSS is actually fairly straightforward with the help of the `style-loader`. This loader uses `module.hot.accept` behind the scenes to patch `<style>` tags when CSS dependencies are updated. | ||
|
||
Now let's update the configuration file to make use of the loader. |
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.
You should probably add a quick installation line before the config changes, e.g.
npm install --save-dev style-loader css-loader
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.
Also below the installation line you should put the name of the file that's being edited for consistency with the other guides e.g.
webpack.config.js
...
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.
I actually thought about this, but I noticed that on the previous examples style-loader
and css-loader
are already added as dev-dependencies, should I write this just as a reminder in case they haven't been installed?
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.
I think it's only mentioned in asset-management
which is then cleaned up at the end. I would mention it again just as a reminder.
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.
I agree, it's also best to make sure that people who don't follow all the guides in succession are still able to fairly easily follow along with this particular guide.
I'd rather be a bit too verbose in these cases.
@@ -152,7 +222,6 @@ body { | |||
|
|||
Change the style on `body` to `background: red;` and you should immediately see the page's background color change without a full refresh. | |||
|
|||
|
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.
Same thing re line breaks.
Thanks for the review will submit changes ASAP! |
|
||
__index.js__ | ||
|
||
``` js | ||
import Library from './library'; |
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.
make this a diff and use the +
and -
for consistency :)
@@ -18,125 +19,194 @@ related: | |||
url: /api/hot-module-replacement | |||
--- | |||
|
|||
T> This guide extends on code examples found in the [`Development`](/guides/development) guide. |
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.
Can you remove the backticks from "[Development
]"? We use them for code, but decided to not use them for links in the guides.
document.body.appendChild(element); | ||
``` | ||
|
||
This is just one example, but there are many others that can easily trip people up. Luckily, there are a lot of loaders out there, some mentioned below, that will make using this process much easier. |
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.
I would suggest to change this slightly for a nicer sentence:
This is just one example, but there are many others that can easily trip people up. Luckily, there are a lot of loaders out there (some of which are mentioned below) that will make hot module replacement much easier.
filename: '[name].bundle.js', | ||
path: path.resolve(__dirname, 'dist') | ||
} | ||
}; | ||
``` | ||
|
||
hot loading stylesheets is a breeze... |
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.
I would change this slightly to keep the tone a little more friendly, maybe:
Hot loading stylesheets is as easy as importing them into a module:
filename: '[name].bundle.js', | ||
path: path.resolve(__dirname, 'dist') | ||
} | ||
}; | ||
``` | ||
|
||
hot loading stylesheets is a breeze... | ||
|
||
__index.js__ | ||
|
||
``` js |
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.
Make this a diff and include the full file for consistency (I know it's a bit verbose in this case, but we can always later on decide to only include the actual diff-ed sections).
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.
Can you also add in a diff for the project structure (you're adding a new file, right?), and put the new file contents in a code block as well. Unless the file is already present, then a diff is sufficient (but I think it's not there anymore, as one of the guides does a cleanup at the end).
@@ -152,7 +222,6 @@ body { | |||
|
|||
Change the style on `body` to `background: red;` and you should immediately see the page's background color change without a full refresh. |
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.
Would also include a diff here for the stylesheet instead of describing what to to.
E.g.
body {
- background: green;
+ background: red;
}
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 really good so far, thanks for all the hard work!
I just added some additional comments to check out.
Don't worry if not all of them make sense, I'm always willing to commit a few changes in the end as well!
Just pushed the changes Hope I didn't miss anything! |
I today opened a PR to request ehnancements to this documentation, @TheDutchCoder just kindly redirected me here. Glad to see this rewrite in progress. Feedback on current live documentationProgressing through the tutorial so far, each tute lead on nicely to the next. Basically the convention of the green lines beginning with + signs is not present in this file, breaking the flow of the tutorial at this point. |
@evolve2k Yeah we're doing a major guides overhaul. Some of them are "done" and in place, but a couple of them are still WIPs and don't follow the same "flow" as the ones that are done. It's our goal to make each guide smoothly follow up on a previous one. If you find any of them lacking, please let us know (and even open a PR if you like), we're always happy with contributions! |
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.
Just one small change, rest is looking really good so far! 💯
``` | ||
|
||
__styles.css__ | ||
|
||
``` css | ||
body { | ||
background: blue; | ||
background: red; |
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 should be background: blue;
to make it line up with the diff further down ;)
@evolve2k yeah you can check out this comment on #1258 for more information on the current guides work. I've been keeping it updated based on the work and PRs submitted. |
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 is looking great! Added a few last minor comments... once those and @TheDutchCoder's are addressed this should be good to go 👍
} | ||
}; | ||
``` | ||
To get it up and running let's do `npm start` from the command line. |
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.
I would rephrase "let's do..." => "let's run..."
class Logger { | ||
log(text) { | ||
console.log('Logging some text: ', text) | ||
To make this work with HMR we need to update that binding to the new `printMe` function using `module.hot.accept` |
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.
You should probably to add some punctuation to the end of this sentence (e.g. :
).
|
||
Hot Module Replacement with CSS is actually fairly straightforward with the help of the `style-loader`. This loader uses `module.hot.accept` behind the scenes to patch `<style>` tags when CSS dependencies are updated. | ||
|
||
First let's install both loaders with the following command: `npm install --save-dev style-loader css-loader` |
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.
You should break the command to a new line e.g.
First let's install both loaders with the following command:
``` bash
npm install --save-dev style-loader css-loader
```
Just made the changes, hope everything is alright now 👍 |
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.
💥 Awesome!
@TheDutchCoder I'll leave this to you to "Squash and Merge" once the build checks finish. |
Yes sir, will do! |
@skipjack was that a better "Squash & Merge"? :) |
I just noticed I was missing a lodash import in a diff block, I opened up a new PR #1456 is that ok? |
@TheDutchCoder beautiful 🚀 🎉 😆 ... feel free to also drop the bullet points from the body, e.g. docs(guides): rewrite of the HMR guide (#1439)
Rewrite of the hot-module-replacement guide. @sbaidon that's perfect, thanks for following up! |
Yeah no problem!
On Jul 26, 2017 12:07 PM, "Sergio Emir Baidon Carrillo" < [email protected]> wrote:
I just noticed I was missing a lodash import in a diff block, I opened up a
new PR #1456 <#1456> is that
ok?
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#1439 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABl0bwg1_RTe2-2YHvY2rhlqSL45fK4sks5sR2Q4gaJpZM4OaHID>
.
|
This PR contains some changes to the
HMR
guide:Development
guide