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

Idea: ERB helper to easy enabling HMR #17

Open
renchap opened this issue Feb 26, 2017 · 7 comments
Open

Idea: ERB helper to easy enabling HMR #17

renchap opened this issue Feb 26, 2017 · 7 comments

Comments

@renchap
Copy link
Owner

renchap commented Feb 26, 2017

I have been thinking on how to improve our HMR support and have a way to toggle it globally while reducing the boilerplate.

The best idea I have is to provide a Ruby helper and have people use packs/application.js.erb.

The interface would look like:

# packs/application.js.erb
<%=  register_react_components {
  Hello: 'components/Hello',
  App: 'components/App',
} %>

Which will generate the following JS code:

import Hello from 'components/Hello'

WebpackerReact.register(Hello)
if (module.hot) {
  module.hot.accept(
    'components/Hello',
    () => WebpackerReact.renderOnHMR(Hello)
  )
}

import App from 'components/App'

WebpackerReact.register(App)
if (module.hot) {
  module.hot.accept(
    'components/App',
    () => WebpackerReact.renderOnHMR(App)
  )
}

We can then have a Rails config flag to enable / disable HMR, as well as a register_react_components param to override it. This will also allow to only enable HMR (and lot relevant code) depending on Rails environment.

Webpacker enables ERB processing in Webpack config by default, so this should work out of the box. I dont fully like the need to have the users move their packs to ERB, but it looks like much more flexible than any other solutions I thought about.

@sevos @mfazekas @daninfpj any feedback?

@sevos
Copy link
Contributor

sevos commented Feb 26, 2017

Again, why this javascript can't be part of the JS library?

@sevos
Copy link
Contributor

sevos commented Feb 26, 2017

Is this to solve the following warning?

@renchap
Copy link
Owner Author

renchap commented Feb 26, 2017

Yes, you can find more context here: renchap/webpacker-react-example#2 (comment)

We havent been able to find documentation on the internals for HMR in Webpack 2. I would have preferred a 100% JS solution where you only have to register your component, but it does not seem possible due to the way HMR works.

@mfazekas
Copy link
Contributor

@renchap i like the idea, alternative would be a babel plugin, but the .erb solution is just much simpler.

👍

@renchap
Copy link
Owner Author

renchap commented Mar 5, 2017

@sevos do you think we can have a simpler pure-JS solution? I tried again to look at how to make this work with Webpack & hot-reloading, without any results.

I will work on implementing the ERB otherwise.

@sevos
Copy link
Contributor

sevos commented Mar 5, 2017

First, what do you think if we define the requirements explicitly? Like a real ticket at a software product dev. What is the exact challenge?

@renchap
Copy link
Owner Author

renchap commented Mar 6, 2017

Sure. I have several goals:

  • avoid the need for boilerplate to be added for HMR (in the common cases at least)
  • be able to easily enable / disable HMR depending on a Rails config option. Ideally I would like for new users to try it out just by flipping this option and running ./bin/webpack-dev-server --hot in place of webpack-watcher
  • completely disable all HMR-related code in production (smaller pack)

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

3 participants