Skip to content

Fix hot module replacement for RN 0.12 #103

Description

@elliottsj

New in react-native@0.12: when using the Chrome debugger, the app bundle is now run inside a web worker: react/react-native#1632, react/react-native@8db35d4

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 different postMessage API. webpack-hot-middleware is an alternative which combines the functionality of both webpack/hot/only-dev-server and webpack-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: elliottsj/webpack@743a00a.

    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 (-‸ლ)

    screenshot 2015-10-11 15 07 53

    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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions