Closed sandhose closed 2 years ago
I see where you're going with this, and I think it makes sense to allow devs to run the production build as well. However, I am reluctant to add additional dependencies like nodemon, as well as having longer and potentially harder to understand run scripts like webpack-dev-server --content-base-target http://localhost:8081/ --hot --inline --quiet --config=webpack.local.config.js
There currently exists a comment in build/index.html
that allows includes the production bundle of the application. What we can do further is serve that file as a static from express.js, and conditionally start the webpack dev server (maybe depending on an environmental variable?).
I'm all for adding this functionality, but I'm also wary of adding additional dependencies and configurations. There exist many react starter kits out there that do that and much more, but I'm trying to keep Essential React as minimal as possible while providing the necessary functionality to start productive development using React.
With that said, if you could refactor the pull request to not change the package.json file and have express serve the production build depending on an env variable or something, that would be a good start :)
I am reluctant to add additional dependencies like nodemon...
You're right, I'll remove it
... as well as having longer and potentially harder to understand run scripts...
In fact, I think we could do something equivalent in an external file (like hot-loader-server.js
, or dev-proxy.js
?) which could be run directly with node, or via npm scripts. The file would look like this:
var WebpackDevServer = require('webpack-dev-server'),
webpack = require('webpack'),
config = require('./webpack.local.config');
new WebpackDevServer(config, {
hot: true,
inline: true,
quiet: true,
contentBase: {
target: 'http://localhost:' + (process.env.PORT || 8080)
}
}).listen(process.env.DEV_PORT || 8081, function(err, result) {
if(err) throw err;
});
The advantages of this solution are that:
The npm scripts could be
dev-proxy
: node dev-proxy
, which will be equivalent to the actual webpack-dev-server
scriptserver
: node server
dev
: npm run server & npm run dev-proxy
So if the user want to use nodemon (assuming the user has nodemon globally installed) to reload the server, he just has to run nodemon server.js --ignore src/ --ignore build/
and npm run dev-proxy
side by side. For that reason, I think it is important to have the webpack-dev-server and the server separated
I did something similar, though with more of a net deletion.
This allows the
server.js
to be independent from webpack-dev-server. Since it allows to reload the server without breaking the hot-loader, I also addednodemon
, in order to automatically reload the server when a file changes.Technically, the
server.js
listens to the8081
port, and thewebpack-dev-server
(which is listening to8080
) creates a proxy to the server (using thecontent-base-taget
option)It also allows to have the same
index.html
file in dev and production environment, because the dev-server and the production-server are listening to the same portTo launch the server with webpack-dev-server and nodemon, use the
dev
npm script.PS: Sorry if I did any spelling error in the README file or in this PR, I'm French :no_mouth: