Skip to content
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

Merged
merged 9 commits into from
Jul 26, 2017
Merged

HMR guide rewrite #1439

merged 9 commits into from
Jul 26, 2017

Conversation

sbaidon
Copy link
Contributor

@sbaidon sbaidon commented Jul 17, 2017

This PR contains some changes to the HMR guide:

  • Update examples to be consistent with Development guide

sbaidon added 2 commits July 17, 2017 09:39
align examples to previous guides
@skipjack skipjack requested a review from TheDutchCoder July 24, 2017 22:09
Copy link
Collaborator

@skipjack skipjack left a 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')
}
};
Copy link
Collaborator

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.

Copy link
Collaborator

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.
Copy link
Collaborator

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());
```
Copy link
Collaborator

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.

Copy link
Contributor Author

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();
  })
}
...

Copy link
Collaborator

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.
Copy link
Collaborator

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
Copy link
Collaborator

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() {
Copy link
Collaborator

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.

```


Copy link
Collaborator

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 h2s).

```

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.

Copy link
Collaborator

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.
Copy link
Collaborator

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

Copy link
Collaborator

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

...

Copy link
Contributor Author

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?

Copy link
Collaborator

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.

Copy link
Collaborator

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.


Copy link
Collaborator

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.

@sbaidon
Copy link
Contributor Author

sbaidon commented Jul 24, 2017

Thanks for the review will submit changes ASAP!


__index.js__

``` js
import Library from './library';
Copy link
Collaborator

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.
Copy link
Collaborator

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.
Copy link
Collaborator

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...
Copy link
Collaborator

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
Copy link
Collaborator

@TheDutchCoder TheDutchCoder Jul 24, 2017

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).

Copy link
Collaborator

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.
Copy link
Collaborator

@TheDutchCoder TheDutchCoder Jul 24, 2017

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;
  }

Copy link
Collaborator

@TheDutchCoder TheDutchCoder left a 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!

@sbaidon
Copy link
Contributor Author

sbaidon commented Jul 25, 2017

Just pushed the changes

Hope I didn't miss anything!

@evolve2k
Copy link

I today opened a PR to request ehnancements to this documentation, @TheDutchCoder just kindly redirected me here. Glad to see this rewrite in progress.
I've had a quick look over the commit history but it's hard to tell at a glance if my thoughts have been addressed.. So here's what I said earlier. If they are already addressed that's great! If not please feel free to account for them.

Feedback on current live documentation

Progressing through the tutorial so far, each tute lead on nicely to the next.
This one however breaks the flow and provided no clues as to which lines to add like previous files do.

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.

@TheDutchCoder
Copy link
Collaborator

@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!

Copy link
Collaborator

@TheDutchCoder TheDutchCoder left a 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;
Copy link
Collaborator

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 ;)

@skipjack
Copy link
Collaborator

@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.

Copy link
Collaborator

@skipjack skipjack left a 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.
Copy link
Collaborator

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`
Copy link
Collaborator

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`
Copy link
Collaborator

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
```

@sbaidon
Copy link
Contributor Author

sbaidon commented Jul 26, 2017

Just made the changes, hope everything is alright now 👍

Copy link
Collaborator

@TheDutchCoder TheDutchCoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💥 Awesome!

@skipjack
Copy link
Collaborator

@TheDutchCoder I'll leave this to you to "Squash and Merge" once the build checks finish.

@TheDutchCoder
Copy link
Collaborator

Yes sir, will do!

@TheDutchCoder TheDutchCoder reopened this Jul 26, 2017
@TheDutchCoder TheDutchCoder merged commit 595e0ac into webpack:master Jul 26, 2017
@TheDutchCoder
Copy link
Collaborator

@skipjack was that a better "Squash & Merge"? :)

@sbaidon
Copy link
Contributor Author

sbaidon commented Jul 26, 2017

I just noticed I was missing a lodash import in a diff block, I opened up a new PR #1456 is that ok?

@skipjack
Copy link
Collaborator

@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!

@TheDutchCoder
Copy link
Collaborator

TheDutchCoder commented Jul 26, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants