react-native-webpack-server
react-native-webpack-server copied to clipboard
Fix hot module replacement for RN 0.12
New in [email protected]: when using the Chrome debugger, the app bundle is now run inside a web worker: https://github.com/facebook/react-native/pull/1632, https://github.com/facebook/react-native/commit/8db35d492b846f51a758e8ee7e5e402c6bad3785
This means that an app bundle built with webpack's hot module replacement enabled will raise errors such as #99, and hot module replacement will fail.
I'm able to get HMR working on the rn-0.12-hmr branch by doing the following:
-
Use webpack-dev-middleware + webpack-hot-middleware instead of webpack/hot/only-dev-server + webpack-dev-server/client:
webpack-dev-server depends on
window.postMessage
to broadcast "webpackHotUpdate" events to any loaded scripts (usually just'webpack/hot/dev-server'
or'webpack/hot/only-dev-server'
), which does not work in the context of a web worker since web workers have a differentpostMessage
API. webpack-hot-middleware is an alternative which combines the functionality of bothwebpack/hot/only-dev-server
andwebpack-dev-server/client
. -
Patch webpack so it uses
importScripts
inside a web worker instead of the default behaviour of appending a<script>
with the new hot-reloaded module: https://github.com/elliottsj/webpack/commit/743a00a8bc491f1900ce9f49150229183f874da1.~~Known issue: this inexplicable error is thrown every time a module is hot reloaded:~~ Edit: turns out this was caused by adding
webpack.HotModuleReplacementPlugin()
to the config twice instead of once (-‸ლ)~~Not sure why this happens; the original error message & stack gets lost somewhere.~~
Try out rn-0.12-hmr/Examples/BabelES6 for a working example.
My patch to webpack seems quite hacky; I think the proper solution is to make something like 'WebWorkerMainTemplate.runtime.js', but I don't know enough about webpack internals to know what to do here.
Does anyone have any ideas on the proper way to do HMR from inside the web worker? cc @gaearon @sokra