Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I embed dynamic Ruby into webpacker.yml configuration file for setting the server host?

I need to test the app from a mobile phone, and file is checked into Git where other developers access it. How do I dynamically set the host name to the IP address of the server? I tried

webpacker.yml
development:
  dev_server:
    host: <%= Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }.ip_address %>

It gave the error

Error: getaddrinfo ENOTFOUND <%= Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }.ip_address %>

I tried renaming the file to webpackager.yml.erb but it gave the error

Webpack dev_server configuration not found in .../config/webpacker.yml.
like image 252
Chloe Avatar asked Jan 22 '26 04:01

Chloe


1 Answers

I ran into the same issue and embedded Ruby within webpacker.yml doesn't appear to be possible.

However, in development mode, you can override webpack-dev-server configuration values via environmental variables. Example:

WEBPACKER_DEV_SERVER_HOST=example.com ./bin/webpack-dev-server

If you're using Foreman, add the command to the relevant Procfile, such as:

webpack: WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
web:    rails s -b 0.0.0.0

More specifically, for your case, something like:

WEBPACK_DEV_SERVER_HOST=$(ruby -e "require 'socket'; Socket.ip_address_list.detect{|intf| intf.ipv4_private?}.inspect_sockaddr") ./bin/webpack-dev-server

References:

  • Dynamic port for webpacker-dev-server
  • Allow overriding dev server settings using env variables
  • Update README.md for ENV vars with dev server
  • Documentation
  • Code
like image 64
user3006381 Avatar answered Jan 23 '26 17:01

user3006381