【问题标题】:React Rails with Webpacker not hot reloading用 Webpacker 反应 Rails 不热重载
【发布时间】:2019-07-08 21:28:31
【问题描述】:

我已将react-rails 添加到我们正在缓慢迁移以做出反应的现有项目中。

当前的 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/

我在一个终端启动 rails 应用程序,在另一个终端启动 ./bin/webpack-dev-server,我可以看到编译中出现的热门模块:

[./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]

问题是当我运行./bin/webpack-dev-server 时,每次更新我的反应文件(而不是刚刚更新的组件)时都会刷新整页。整页刷新仅在开发服务器运行时发生。此外,在整页刷新之前,我没有看到组件更新。

所以问题是,为什么 webpack-dev-server 会发出浏览器页面刷新的信号,以及为什么组件没有热重载?

【问题讨论】:

    标签: ruby-on-rails webpack-dev-server webpacker react-rails hot-module-replacement


    【解决方案1】:

    这就是 Webpack HMR 的工作原理,如果你想在你的 react 模块上启用 HMR,你可以尝试 React Hot LoaderWebpack plugin

    。安装react-hot-loaderyarn

    。编辑.babelrc 文件并将react-hot-loader/babel 添加到插件列表中。

    。让你的 React 组件“热”。

    import React from 'react'
    import { hot } from 'react-hot-loader'
    
    class Example extends React.Component {
    }
    
    export default hot(module)(Example);
    

    【讨论】:

    • 更新:解决方案被认为是Old API。对于react-hot-loader 4.5.4+ 版本,现在是export default hot(Example)
    • 这个解决方案对我来说真的不起作用......你有一个可以链接作为示例的存储库吗?
    • @rstojano 我让它工作了!这是一个示例 repo:github.com/edelgado/react-rails-hmr
    • 谢谢@rstojano!我刚刚提交了一个 PR,您的示例已更新到 Rails 6 - fork 在这里:github.com/Leap-Forward/react-rails-hmr
    猜你喜欢
    • 2018-04-05
    • 2018-10-16
    • 2017-06-24
    • 2016-09-27
    • 2018-04-30
    • 2018-09-15
    • 2019-09-21
    • 2019-11-27
    • 1970-01-01
    相关资源
    最近更新 更多