Skip to content
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

Replace module.makeHot with a decorator #108

Closed
gaearon opened this issue Mar 25, 2015 · 4 comments
Closed

Replace module.makeHot with a decorator #108

gaearon opened this issue Mar 25, 2015 · 4 comments
Milestone

Comments

@gaearon
Copy link
Owner

gaearon commented Mar 25, 2015

Currently RHL only knows about the exported classes.
If you export a function, you need to use module.makeHot API provided by the hot loader:

export default function makeComponent(whatever) {
  class Something {
    ...
  }

  if (module.makeHot) { // false in production
    Something = module.makeHot(Something);
  }

  return Something;
}

This API is overly verbose and I don't like it.
Instead, we can do some magic to make this work:

import { hotReload } from 'react-hot-loader';

export default function makeComponent(whatever) {
  class Something {
    ...
  }
  return hotReload(Something);
}

This would be an identity function in production, and RHL patcher in development. RHL would emit additional code to keep track of current module so withHotReload knows which module to attach to.

Finally, with ES7 decorators (next version of Babel will support them), we can write this as

import { hotReload } from 'react-hot-loader';

export default function makeComponent(whatever) {
  return @hotReload class Something {
    ...
  };
}

Now that we talk about Babel, there's another, much more fun option. If Babel had a transform that wraps each class declaration into a specified function, we would not need this trickery at all. What do you think @sebmck?

@sebmck
Copy link

sebmck commented Mar 25, 2015

Finally, with ES7 annotations

Just to be pedantic they're decorators and are very different from annotations. 😜

If Babel had a transform that wraps each class declaration into a specified function, we would not need this trickery at all.

This would be absolutely doable with the plugin API coming in the next major.

@gaearon
Copy link
Owner Author

gaearon commented Mar 25, 2015

Just to be pedantic they're decorators and are very different from annotations.

Yeah. Some recent “decorators or annotations” argument caused a brainfart in my head.

@gaearon gaearon changed the title Consider replacing module.makeHot API with something better Replace module.makeHot with a decorator Apr 21, 2015
@gaearon gaearon added this to the v2.0 milestone Apr 21, 2015
@gaearon
Copy link
Owner Author

gaearon commented Apr 21, 2015

Just wrote some thoughts about what I want 2.0 to become: https://medium.com/@dan_abramov/the-death-of-react-hot-loader-765fa791d7c4

cc @sebmck @milankinen @caspervonb

@gaearon gaearon closed this as completed Aug 22, 2015
@gaearon
Copy link
Owner Author

gaearon commented Aug 23, 2015

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

No branches or pull requests

2 participants