-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
docs(guides): Add middleware section to dev guide #1481
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
Conversation
This PR adds the `webpack-dev-middleware` and `webpack-hot-middleware` to the Development Guide.
@skipjack this adds quite a bit to the guide, and I hope it's sufficient. Didn't want to into more detail and overload this guide. It was quite the challenge getting this to work, but I'm getting the hang of it ;) Please check for consistency, spelling, formatting, and other things as usual. I probably looked over some stuff ;) Will reference this PR in the stale PR that previously tried to add this section 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.
Left some minor comments in the first section. For the most part, it looks 🎉 . Thank you for tackling this and closing the other PR. By any chance did you add a demo to webpack-guides-code-examples
repo? I'd be interested in playing around with it if you did.
As for the second section on HMR I would recommend dropping it from here (see my comment). Sorry, if I had realized you were planning to add that as well I would have said something earlier.
I think webpack-dev-middleware
and webpack-hot-middleware
are primarily for advanced usage so I think we should avoid digging too deep into them. Also, from what I just read in their readmes, the docs within those repos are actually pretty good so there's no need to repeat all of it.
content/guides/development.md
Outdated
@@ -240,7 +240,301 @@ T> Now that your server is working, you might want to give [Hot Module Replaceme | |||
|
|||
### Using webpack-dev-middleware | |||
|
|||
?> Familiar with `webpack-dev-middleware`? We need your help! Please submit a PR to fill in the missing instructions and example here. Make sure to keep it simple as this guide is intended for beginners. | |||
`webpack-dev-middleware` is a wrapper that will emit files processed by webpack to a server. It's actually used in the `webpack-dev-server` itself, but if you want to use it with a different configuration (e.g. with `express`), you can use the middleqware. |
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/middleqware/middleware
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.
Though maybe the whole thing can just be rephrased slightly...
webpack-dev-middleware
is a wrapper that will emit files processed by webpack to a server. This is used bywebpack-dev-server
internally, however it's available as a separate package to allow more custom setups if desired. We'll take a look at an example that combineswebpack-dev-middleware
with anexpress
server.
content/guides/development.md
Outdated
__webpack.config.js__ | ||
|
||
``` diff | ||
var path = require('path'); |
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 we typically use const
for node imports.
content/guides/development.md
Outdated
|
||
``` diff | ||
var path = require('path'); | ||
const htmlWebpackPlugin = require('html-webpack-plugin'); |
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.
Not sure what the rest of the instances in this guide look like but in other guides we've capitalized plugin names (e.g. HtmlWebpackPlugin
) and that seems to be the standard within the community for plugin imports. Personally, I actually capitalize all imports to delineate them from normal variables but that's a change I can PR later on if anything.
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.
Completely right, typo on my end (rest was capitalized)
content/guides/development.md
Outdated
devtool: 'inline-source-map', | ||
plugins: [ | ||
new CleanWebpackPlugin(['dist']), | ||
new htmlWebpackPlugin({ |
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 sure to update this too.
content/guides/development.md
Outdated
output: { | ||
filename: '[name].bundle.js', | ||
- path: path.resolve(__dirname, 'dist') | ||
+ path: '/', |
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 will cause npm run build
not to emit files to /dist
anymore. webpack-dev-middleware
manages output in memory as opposed to actually emitting files to output.path
which is why when you run webpack-dev-server
nothing in /dist
is ever built or updated. I think what you want is something more along the lines of:
output: {
filename: '[name].bundle.js'
path: path.resolve(__dirname, 'dist'),
publicPath: '/' // To serve from `localhost:3000` or '/app' to serve from `localhost:3000/app`
}
I'm not 100% on this as I haven't used webpack-dev-middleware
much, aside from behind the scenes in webpack-dev-server
so I may have to test a bit. However, I do think that changing path
will have unintended side effects for the rest of the npm scripts
.
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'll need to test this, good point though!
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 were completely right, will push the changes!
content/guides/development.md
Outdated
|- /src | ||
|- index.js | ||
|- print.js | ||
+ |- server.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.
You might want to actually place this outside of /src
as it's not really part of the actual web application that's being deployed. Typically I've seen these kinds of scripts either in the root directory, or a separate /scripts
directory though I think the root would be best here.
content/guides/development.md
Outdated
"watch": "webpack --progress --watch", | ||
"start": "webpack-dev-server --open", | ||
+ "server": "node src/server.js", | ||
"build": "webpack" |
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.
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, remember to update the server
script to "node server.js"
if you make the change I suggested above.
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 looks like tabs, will correct
content/guides/development.md
Outdated
Now fire up your browser and go to `http://localhost:3000/app`, you should see your webpack app running and functioning! | ||
|
||
|
||
#### Adding Hot Module Replacement |
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 drop this whole section -- if anything just make a note of the webpack-hot-middleware
package at the end of the section above and link to it. Or, even better, just add a small tip to the Hot Module Replacement guide that notes and links to it, maybe something like:
T> If you took the route of using
webpack-dev-middleware
instead ofwebpack-dev-server
, please use thewebpack-hot-middleware
package to enable HMR on your custom server or application.
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.
Okay no problem, it was good practice! 😄
Thanks @skipjack I will go through your feedback and make the appropriate changes hopefully this week! 👍 |
@skipjack I think I resolved all issues/feedback, another look would be appreciated! |
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.
A few minor things, aside from that this looks great!
content/guides/development.md
Outdated
output: { | ||
filename: '[name].bundle.js', | ||
path: path.resolve(__dirname, 'dist'), | ||
+ publicPath: 'http://localhost/' |
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 try just publicPath: '/'
but if that didn't work then I guess this is fine. Frankly, I still find publicPath
a bit confusing -- it almost seems like for fully qualified urls the first section, e.g. http://localhost
is just ignored for generated bundles. I think maybe people use fully qualified URLs like https://cdn.example.com/
so that if something not generated by webpack returns a 404, the dev-server will fall back to actual location.
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 that works, so we should be good with that change!
content/guides/development.md
Outdated
|
||
// Serve the files on port 3000. | ||
app.listen(3000, function () { | ||
console.log('Example app listening on port 3000!'); |
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 throw a line break at the end of the statement so there's a little space between your and webpack's, e.g.:
console.log('Example app listening on port 3000!\n');
to generate below:
Example app listening on port 3000!
webpack built 27b137af6d9d8668c373 in 1198ms
Hash: 27b137af6d9d8668c373
...
(somewhat nitpicky, I know -- feel free to ignore 😆 )
content/guides/development.md
Outdated
|
||
Now fire up your browser and go to `http://localhost:3000`, you should see your webpack app running and functioning! | ||
|
||
T> If you took the route of using `webpack-dev-middleware` instead of `webpack-dev-server`, please use the [`webpack-hot-middleware`](https://github.com/glenjamin/webpack-hot-middleware) package to enable HMR on your custom server or application. |
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 meant add this somewhere in guides/hot-module-replacement.md
... just pick a place in there that seems appropriate.
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 want that tip to be added to the HMR guide instead and take it out of this one?
I can do that, just wanted to be sure I understood correctly ;)
@skipjack This should cover 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.
Looks great, thanks for making the changes! I made one change to the paragraph after publicPath
to clarify but aside from that this was perfect. As soon as the build passes I'll merge.
🎉 |
Ok I'm going to merge as the build errors are just stemming from
If any other issues come up from merging this (e.g. linting) then we can resolve in a separate PR. |
This PR adds the
webpack-dev-middleware
andwebpack-hot-middleware
to the Development Guide.