-
Notifications
You must be signed in to change notification settings - Fork 289
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
Why are Webpack-related packages required unconditionally in server.js? #66
Comments
@simevidas Nope, that's not a stupid question at all. :) The requirement of these packages doesn't mean that are going to run. The actual invocation happens bellow at lines #15, #16 and still those lines aren't causing webpack to run. Later below, the You could add another bootstrap file for production if you like where webpack wont being required. |
Hm, if I understand correctly, Example 1: let foo = require('foo');
if (development) {
foo();
} Example 2: if (development) {
let foo = require('foo');
foo();
} I’m not sure why Webpack-related modules should be parsed and compiled in production. It just wastes server time and memory. (Correct me if I’m wrong.) For comparison, this is my own setup and it works fine for HMR: if (process.env.NODE_ENV === 'development') {
let webpack = require('webpack');
let config = require('./webpack.config');
let compiler = webpack(config);
app.use(require('webpack-dev-middleware')(compiler));
app.use(require('webpack-hot-middleware')(compiler));
} |
Indeed they are useless since they are never going to be used in production.
Well yes but it's only loading the source code, which is really negligible. Actually server processing and usage waste happens when you actually tell webpack to do something. Like I said, I would separate the But either way, your code works just fine! :) |
I got curious so I run a test on my code - I’ve measured the time and if (process.env.NODE_ENV === 'development') {
console.log(`Memory before: ${JSON.stringify(process.memoryUsage())}`);
console.time('webpack');
let webpack = require('webpack');
let webpackDevMiddleware = require('webpack-dev-middleware');
let webpackHotMiddleware = require('webpack-hot-middleware');
console.timeEnd('webpack');
console.log(`Memory after: ${JSON.stringify(process.memoryUsage())}`);
let config = require('./webpack.config');
let compiler = webpack(config);
app.use(webpackDevMiddleware(compiler));
app.use(webpackHotMiddleware(compiler));
} The results are consistent on my machine. Here is one result:
It looks like the Webpack modules take up ~16 MB, in addition to slowing down server boot (my app is not ready for deployment, so I can’t test on my actual web server yet). I recommend optimizing the boilerplate, so that server resources aren’t wasted. This seems like a good idea for the default setup. |
I would love to throw a PR. @christianalfoni what do you think? |
I apologize if this is a stupid question. I’m just about to start using Webpack for my Express web app.
In server.js, I see that
webpack
,webpack-dev-middleware
, andwebpack-hot-middleware
are required at the top of the file, unconditionally.Shouldn’t these statements be inside the
if (isDeveloping)
branch? Otherwise, Webpack runs on the actual web server in production, if I understand correctly.The text was updated successfully, but these errors were encountered: