0

Using Docker and Docker Compose, I'm unable to get the webpacker service up and running after upgrading to webpacker 3.3.0. The output after docker-compose up is:

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

I've run webpacker:install - it made changes but the output is the same. I've also run rails new test --webpacker and compared the configuration files. So far the only notable difference between the working generated app and my own is the Node version. On my local machine in a typical RVM environment the node version is v9.4.0 and in the docker images it's v8.9.3. Yarn in both environments is 1.3.2.

My config/webpacker.yml is almost exactly the same as generated. The only difference is a few added extensions:

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

Any ideas as to why it's not finding the dev_server configuration? I don't believe the node version is the reason.

Archonic
  • 5,207
  • 5
  • 39
  • 55
  • Might wanna post your config files like `webpacker.yml` and `webpack/environment.js`/`webpack/development.js`. – t56k Mar 07 '18 at 00:46
  • I've posted a link to my webpacker.yml. I'll update shortly with webpack/environment.js etc. – Archonic Mar 07 '18 at 00:49
  • And you've definitely got the `webpacker` gem installed like with a `bundle install`? – t56k Mar 07 '18 at 01:00
  • Yes. It's looking like a recent version of webpacker doesn't install correctly out of the box: https://github.com/rails/webpacker/issues/1295 – Archonic Mar 07 '18 at 01:03
  • Looks like it, my app that runs this config has this: `"webpack-dev-server": "^2.9.7"`. – t56k Mar 07 '18 at 01:07

1 Answers1

0

As mentioned in their changelog, Webpacker 3.1.0 and 3.1.1 are broken and should be avoided.

I narrowed the problem down to this block within 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

I'm not sure why there's no fallback for NODE_ENV within the gem, but this code relies on NODE_ENV being populated and matching the key that has your dev_server key in config/webpacker.yml. They're relying on a binstub that sets the NODE_ENV to RAILS_ENV. You can generate them with bundle exec rails webpacker:binstubs.

Here's the example 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)

I generated binstubs a few times, but I believe I was having cache issues with docker that was blocking the fix. docker volume -rm webpacker may have been what fixed that - not sure.

Archonic
  • 5,207
  • 5
  • 39
  • 55
  • ` 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` this is from the gem itself correct? – Lucian Tarna Oct 22 '19 at 14:47
  • @LucianTarna Yes, that was within the webpacker gem. Webpacker has changed a lot in the last year so I don't know how relevant this answer is anymore. – Archonic Nov 12 '19 at 18:58