Rails 5.2.0.rc1 + Webpacker 3.3.0 找不到dev_server配置


使用 Docker

和 Docker Compose,我无法在升级到 webpacker 3.3.0 后启动并运行 webpacker 服务。docker-compose up后的输出为:

webpack dev_server configuration not found in /app/config/webpacker.yml.
Please run bundle exec rails webpacker:install to install Webpacker

我已经运行了webpacker:install - 它进行了更改,但输出是相同的。我还运行了rails new test --webpacker并比较了配置文件。到目前为止,工作生成的应用程序和我自己的应用程序之间唯一显着的区别是 Node 版本。在我的本地计算机上,在典型的 RVM 环境中,节点版本是v9.4.0的,在 docker 映像中它是v8.9.3的。两种环境中的纱线都是1.3.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_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:
    - .coffee
    - .erb
    - .js
    - .jsx
    - .ts
    - .vue
    - .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
    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
    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
  # Cache manifest.json for performance
  cache_manifest: true

config/webpack/environment.js:

const { environment } = require('@rails/webpacker')
module.exports = environment

config/webpack/development.js:

const environment = require('./environment')
module.exports = environment.toWebpackConfig()

关于为什么找不到dev_server配置的任何想法?我不相信节点版本是原因。

正如他们的更新日志中提到的,Webpacker 3.1.0 和 3.1.1 被破坏了,应该避免。

我将问题缩小到webpacker中的这个块:

def load_config
  @config_file = File.join(@app_path, "config/webpacker.yml")
  dev_server = YAML.load_file(@config_file)[ENV["NODE_ENV"]]["dev_server"]
  @hostname          = dev_server["host"]
  @port              = dev_server["port"]
  @pretty            = dev_server.fetch("pretty", true)
rescue Errno::ENOENT, NoMethodError
  $stdout.puts "webpack dev_server configuration not found in #{@config_file}."
  $stdout.puts "Please run bundle exec rails webpacker:install to install Webpacker"
  exit!
end

我不确定为什么 gem 中没有NODE_ENV的回退,但这段代码依赖于填充NODE_ENV并匹配在 config/webpacker.yml 中具有您的dev_server键的键。他们依靠将NODE_ENV设置为RAILS_ENV的垃圾桶存根。您可以使用 bundle exec rails webpacker:binstubs .

下面是示例 bin/webpack-dev-server:

#!/usr/bin/env ruby
ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
ENV["NODE_ENV"] ||= ENV["RAILS_ENV"]
require "pathname"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
  Pathname.new(__FILE__).realpath)
require "rubygems"
require "bundler/setup"
require "webpacker"
require "webpacker/dev_server_runner"
Webpacker::DevServerRunner.run(ARGV)

我生成了几次垃圾桶存根,但我相信我在 docker 上遇到了缓存问题,阻止了修复程序。 docker volume -rm webpacker可能是解决这个问题的原因 - 不确定。

最新更新