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

问题描述

我在 docker 中有一个带有 Ruby on Rails 6、Webpacker 和 react-rails 的容器,当我重新加载页面时,它的编译延迟超过 20 秒,所以我想为我的项目添加热重新加载,但它只是不起作用. webpack-dev-server 运行良好,但页面没有更新更改。

我正在使用 foreman 运行 bin/webpack-dev-server 和 rails server,我正在使用 react-refresh-webpack-plugin 来刷新 react。当我更改代码时,即使我重新加载页面页面也不会收到更改。

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()

控制台输出

enter image description here

解决方法

我可以解决它,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