Skip to content

Idea: ERB helper to easy enabling HMR #17

@renchap

Description

@renchap

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?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions