2

I'm using Rails 6 and the webpacker 5.4.3 gem. I just recently upgraded several node packages and added one which broke hot reloading and can't determine why; these are the packages I've upgraded/added:

  • @rails/webpacker. from 5.1.1 to 5.4.3
  • webpack from 4.39.4 to 4.46.0
  • webpack-dev-server from 3.8.0 to 4.1.1
  • webpack-cli 4.9.1 (added)

TLDR; it looks like webpack-dev-server is watching for changes but not acting as a proxy to refresh the browser for some reason after I updated some node packages, in fact, webpacker may not even be reading config/webpacker.yml at all as it's not using the indicated host and port..

What I've noticed so far, is that if I run the following in 2 terminals...

$ ./bin/webpack-dev-server and $ rails s -b 0.0.0.0 -p 3333

...and change one of my react components (some text for example), I can see that the terminal running the webpack-dev-server (wds) does the following:

  1. Picks up on the changes.
  2. Generates a new Hash.
  3. Recompiles.

I also noticed at this point my public/packs/manifest.json file gets updated properly, but none of the js files that were supposedly recompiled according to the wds terminal output are present in the js folder, for example: js/application-b46eee3e623901e06c18.js js/application-6c52adcc65de5c6db860.hot-update.js js/application-b46eee3e623901e06c18.js.map js/application-6c52adcc65de5c6db860.hot-update.js.map

That being said, the browser never refreshes the view like it used to. When I refresh the browser to see the changes, I can see that the terminal running the rails server indicates that webpacker recompiles everything all over again. I can see the changes, this is just a recompile, not hot loading and takes forever.

Here are my relevant files:

package.json

{
  "name": "estat",
  "private": true,
  "scripts": {
    "build:test": "NODE_ENV=test webpack --config webpack.config.js",
    "build:production": "NODE_ENV=production yarn install && NODE_ENV=production webpack --config webpack.config.js",
    "build:development": "NODE_ENV=development webpack -w --config webpack.config.js",
    "lint:eslint": "eslint ./app/javascript/components/**/*.{js,jsx};exit 0",
    "lint:eslint-fix": "eslint --fix ./app/javascript/components/**/*.{js,jsx};exit 0"
  },
  "dependencies": {
    "@babel/core": "^7.6.0",
    "@babel/plugin-proposal-class-properties": "^7.5.5",
    "@babel/plugin-transform-runtime": "^7.6.0",
    "@babel/preset-env": "^7.6.0",
    "@babel/preset-react": "^7.0.0",
    "@fortawesome/fontawesome-free": "^5.15.0",
    "@rails/webpacker": "^5.4.3",
    "babel-loader": "^8.0.6",
    "babel-plugin-transform-react-remove-prop-types": "^0.4.24",
    "babel-preset-react": "^6.24.1",
    "case-sensitive-paths-webpack-plugin": "^2.2.0",
    "elliptic": "^6.5.3",
    "es5-shim": "^4.5.13",
    "eslint-plugin-ramda": "^2.5.1",
    "http-proxy": "^1.18.1",
    "imports-loader": "^0.8.0",
    "jquery": "*",
    "jquery-ui-dist": "^1.12.1",
    "jstree": "^3.3.10",
    "mark.js": "^8.11.1",
    "moment": "^2.24.0",
    "node-forge": "^0.10.0",
    "node-sass": "^4.12.0",
    "prop-types": "^15.7.2",
    "ramda": "^0.27.0",
    "rc-year-calendar": "^1.0.2",
    "react": "^16.9.0",
    "react-big-calendar": "^0.33.2",
    "react-datepicker": "2.14.1",
    "react-dom": "^16.9.0",
    "react-modal": "^3.10.1",
    "react-redux": "^7.2.6",
    "react-select": "^3.0.4",
    "react_ujs": "^2.6.0",
    "redux": "^4.0.4",
    "redux-saga": "^1.0.5",
    "serialize-javascript": "^3.1.0",
    "webpack": "^4.46.0",
    "webpack-cli": "^4.9.1",
    "websocket-extensions": "^0.1.4",
    "yargs-parser": "13.1.2"
  },
  "version": "1.3.2",
  "devDependencies": {
    "eslint": "^7.32.0",
    "eslint-plugin-react": "^7.14.3",
    "webpack-dev-server": "4.1.1",
    "webpack-manifest-plugin": "^4.0.2"
  }
}

config/webpacker.yml

# Note: You must restart bin/webpack-dev-server for changes to take effect

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_root_path: public
  public_output_path: packs
  cache_path: tmp/cache/webpacker
  check_yarn_integrity: false
  webpack_compile_output: true

  # 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

  # Extract and emit a css file
  extract_css: false

  static_assets_extensions:
    - .jpg
    - .jpeg
    - .png
    - .gif
    - .tiff
    - .ico
    - .svg
    - .eot
    - .otf
    - .ttf
    - .woff
    - .woff2

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

development:
  <<: *default
  compile: true

  # Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules
  check_yarn_integrity: false

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: localhost
    port: 3035
    public: localhost:3035
    hmr: false
    # 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
    pretty: false
    headers:
      'Access-Control-Allow-Origin': '*'
    static:
      watch:
         ignored: '**/node_modules/**'

test:
  <<: *default
  compile: true

  # Compile test packs to a separate directory
  public_output_path: packs-test

prod_shared: &prod_shared
  <<: *default

  # This environment depends on precompilation of packs prior to booting for performance.
  compile: false

  # Extract and emit a css file
  extract_css: true

  # Cache manifest.json for performance
  cache_manifest: true

devprod:
  <<: *prod_shared

preview:
  <<: *prod_shared

staging:
  <<: *prod_shared

staging-01 :
  <<: *prod_shared

production:
  <<: *prod_shared

config/webpack/development.js

process.env.NODE_ENV = process.env.NODE_ENV || 'development'

const environment = require('./environment')

config = environment.toWebpackConfig()
config.output.filename = "js/[name]-[hash].js"

module.exports = config

Any help would be appreciated.

gangelo
  • 3,034
  • 4
  • 29
  • 43
  • I have the same problem, have you managed to resolve it? – Kate Wilkins Nov 04 '21 at 14:36
  • 2
    Yes, downgrade we pack-cli to 3.x and we pack-dev-server to 3.x – gangelo Nov 04 '21 at 14:39
  • Do we have any other option without downgrading it ?. Currently, we found a CVE for which we need to upgrade webpack-dev-server. Just wondering is any other solution without downgrading it? – Sam Jan 25 '22 at 12:28

3 Answers3

1

I was able to solve it by doing the below development.js

const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');

    const devServer = {
      port: 3035,
      client: { overlay: false },
      liveReload: false,
    };
    
    //plugins
    environment.plugins.append(
      'ReactRefreshWebpackPlugin',
      new ReactRefreshWebpackPlugin({
        overlay: {
          sockPort: devServer.port
        }
      })
    )
Sam
  • 1,040
  • 1
  • 7
  • 11
0

Webpack allows you to benefit both from Live Reload, and from Hot Module Reload. Hot Module Reload is a more advanced tool. Here is how I implement it more:

devServer: {
   port: 8080,
   hot: "only",
   static: {
     directory: path.join(__dirname, './'),
     serveIndex: true,
   },
 },
Maksym Dudyk
  • 1,082
  • 14
  • 16
0

./bin/webpack-dev-server uses default port 8080. In your webpacker.yml you have

  dev_server:
    https: false
    host: localhost
    port: 3035
    public: localhost:3035

Change this to

  dev_server:
    https: false
    host: localhost
    port: 8080
    public: localhost:8080

And then hot reloading should work. You should see

[webpack-dev-server] Server started: Hot Module Replacement enabled, Live Reloading enabled, Progress disabled, Overlay enabled.
[HMR] Waiting for update signal from WDS...

In your browser's JavaScript console.

HoTell
  • 1
  • 1