Let me preface this by saying that everything I have set up works, this is just a question that's nagging me that I would love to get an answer for. I'm using the react-hot-boilerplate project (https://github.com/gaearon/react-hot-boilerplate). However, in webpack.config.js
, this setting is confusing me to no end:
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/static/'
},
In this config, it looks like the output file should go into the dist
folder in root of the project. Even if I manually create the dist
folder (which I know I shouldn't have to do), no file gets outputted. Yet everything works totally fine; the app loads and will hot-reload if I change something in the components. Where is this output file actually going?
Output is a property that tells webpack where to emit / save the bundles it creates and how to name these bundled files. By default the main output file gets stored in ./dist/main.
Hot reloading allows you to see the changes that you have made in the code without reloading your entire app. Whenever you make any changes, all you need to do is save your code. As soon as you save your code, React Native tracks which files have changed since your last saved, and only reload those file for you.
Understanding how React Webpack works? Like create-React-app, React Webpack is also a command-line tool used to create a bundle of assets (files and code). It doesn't run on the browser or the server. It takes all the JS files and other assets, transforming them into one large file.
All of the heavy lifting for react-hot-boilerplate (in terms of hot reloading files) is done by the webpack-dev-server dependency.
webpack-dev-server in turn uses webpack-dev-middleware to handle the serving of the files (from express).
This bit of documentation about Webpack Dev Server should give you a good overview about how the mechanism works:
Using this config webpack-dev-server will serve the static files in your build folder and watch your source files for changes. When changes are made the bundle will be recompiled. This modified bundle is served from memory at the relative path specified in publicPath (see API). It will not be written to your configured output directory. Where a bundle already exists at the same url path the bundle in memory will take precedence.
For example with the configuration above your bundle will be available at localhost:8080/assets/bundle.js
And this is a good bit from the webpack-dev-middleware docs:
The webpack-dev-middleware is a small middleware for a connect-based middleware stack. It uses webpack to compile assets in-memory and serve them. When a compilation is running every request to the served webpack assets is blocked until we have a stable bundle.
So, inside the dev server, the files are written to an in memory-file system and then served. The assets endpoint serves files from the in-memory location as well as creates a web socket connection to push down further changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With