5
votes

I've added react-rails to an existing project that we're slowly migrating over to react.

Current webpacker.yml

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_output_path: packs
  cache_path: tmp/cache/webpacker

  # Additional paths webpack should lookup modules
  # ['app/assets', 'engine/foo/app/assets']
  resolved_paths: []

  # Reload manifest.json on all requests so we reload latest compiled packs
  cache_manifest: false

  extensions:
    - .jsx
    - .js
    - .sass
    - .scss
    - .css
    - .module.sass
    - .module.scss
    - .module.css
    - .png
    - .svg
    - .gif
    - .jpeg
    - .jpg
    - .tsx
    - .ts

development:
  <<: *default
  compile: true

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: localhost
    port: 3035
    public: localhost:3035
    hmr: true
    # Inline should be set to true if using HMR
    inline: true
    overlay: true
    compress: true
    disable_host_check: true
    use_local_ip: false
    quiet: false
    headers:
      'Access-Control-Allow-Origin': '*'
    watch_options:
      ignored: /node_modules/

I boot up the rails app in one terminal and ./bin/webpack-dev-server in another and I can see the hot modules appearing in the compilation:

[./node_modules/webpack-dev-server/client/index.js?http://localhost:3035] (webpack)-dev-server/client?http://localhost:3035 7.93 kB {0} {1} [built]
[./node_modules/webpack-dev-server/client/overlay.js] (webpack)-dev-server/client/overlay.js 3.67 kB {0} {1} [built]
[./node_modules/webpack/hot/dev-server.js] (webpack)/hot/dev-server.js 1.61 kB {0} {1} [built]
[./node_modules/webpack/hot/emitter.js] (webpack)/hot/emitter.js 77 bytes {0} {1} [built]
[./node_modules/webpack/hot/log-apply-result.js] (webpack)/hot/log-apply-result.js 1.31 kB {0} {1} [built]
[./node_modules/webpack/hot/log.js] (webpack)/hot/log.js 1.04 kB {0} {1} [built]
   [0] multi (webpack)-dev-server/client?http://localhost:3035 webpack/hot/dev-server ./app/javascript/packs/application.js 52 bytes {1} [built]
   [1] multi (webpack)-dev-server/client?http://localhost:3035 webpack/hot/dev-server ./app/javascript/packs/server_rendering.js 52 bytes {0} [built]

The problem is when I'm running ./bin/webpack-dev-server I get a full page refresh every time I update my react files (instead of the components just being updated). The full page refresh only happens while dev server is running. Also, before the full page refresh I don't see component update.

So the question is, why is webpack-dev-server signaling a browser page refresh and why are the components not hot-reloading?

1

1 Answers

2
votes

This is how Webpack HMR works, if you want to enable HMR on your react modules you can try React Hot Loader with the Webpack plugin

. Install react-hot-loader with yarn

. Edit the .babelrc file and add react-hot-loader/babel to the list of plugins.

. Make your React Component 'hot'.

import React from 'react'
import { hot } from 'react-hot-loader'

class Example extends React.Component {
}

export default hot(module)(Example);