HRM 无法在 Rails 上使用 Webpacker 和 Docker 在 Ruby 上工作

HRM is not working on Ruby on Rails with Webpacker and Docker

我在 docker 中有一个容器,在 Rails 6 上有 Ruby,Webpacker 和 react-rails,当我重新加载页面时,它延迟了 20 多秒进行编译,所以我想将热重载添加到我的项目中,但它无法正常工作。 webpack-dev-server 运行良好 运行 但页面未更新更改。

我正在使用 运行 bin/webpack-dev-server 和 rails 服务器的工头,我正在使用 react-refresh-webpack-plugin 来刷新反应。当我更改代码时,即使我重新加载页面,页面也没有收到更改。

docker-compose.yml

version: '3.3'
services:    
    mysql:
        image: mysql:latest
        restart: always    
        command: mysqld --default-authentication-plugin=mysql_native_password    
        ports:
            - 3307:3306        
        environment: 
            MYSQL_ROOT_PASSWORD: password
            MYSQL_DATABASE: development 
    ruby:        
        build: .               
        image: alexisnoe27/ubuntu_rails:master        
        command: bash -c ' 
            cd rails_app && 
            bundle update &&
            yarn &&
            rm -f tmp/pids/server.pid &&   
            bundle exec foreman start -f Procfile.dev'             
        environment: 
            MYSQL_USER: root
            MYSQL_PASSWORD: password
            MYSQL_PORT: 3306
            MYSQL_HOST: mysql
            MYSQL_DATABASE: development
            RAILS_ENV: development
            NODE_ENV: development
            WEBPACKER_DEV_SERVER_HOST: 0.0.0.0
        expose:
            - 3000       
            - 3035     
        ports: 
            - 3000:3000            
            - 3035:3035
        depends_on: 
            - mysql
        links: 
            - mysql
        volumes: 
            - ./:/rails_app               
        tty: true
        stdin_open: true

Procfile.dev

webpack: bin/webpack-dev-server
web: bundle exec rails s -b 0.0.0.0 -p 3000

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  
  webpack_compile_output: true

  # Additional paths webpack should lookup modules
  # ['app/assets', 'engine/foo/app/assets']
  additional_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

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: localhost
    hmr: true
    port: 3035
    public: localhost:3035
    # 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': '*'
    watch_options:
      ignored: '**/node_modules/**'


test:
  <<: *default
  compile: true

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

production:
  <<: *default

  # Production 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

development.js

process.env.NODE_ENV = process.env.NODE_ENV || 'development'
const environment = require('./environment')

const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const isWebpackDevServer = process.env.WEBPACK_DEV_SERVER;

if (isWebpackDevServer) {
    console.log('true')
    environment.plugins.append(
        'ReactRefreshWebpackPlugin',
        new ReactRefreshWebpackPlugin({
            overlay: {
                sockPort: 3035
            }
        })
    );
}

module.exports = environment.toWebpackConfig()

控制台输出

我可以解决它,webpack 没有监视我的文件,我在 webpacker.yml 的 watch_options 中添加了 aggregateTimeout: 300 和 poll: 500,它起作用了。

# 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  
  webpack_compile_output: true

  # Additional paths webpack should lookup modules
  # ['app/assets', 'engine/foo/app/assets']
  additional_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

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: localhost
    hmr: true
    port: 3035
    public: localhost:3035
    # 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': '*'
    watch_options:
      ignored: '**/node_modules/**'
      aggregateTimeout: 300
      poll: 500


test:
  <<: *default
  compile: true

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

production:
  <<: *default

  # Production 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