Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set dev server listen host and port via environment variables #793

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,15 @@ precedence over the ones already set in the configuration file.

By default, webpack dev server listens on `localhost` in development for security
but if you want your app to be available over local LAN IP or VM instance like vagrant
you can pass an additional config option `--listen-host`
you can set environment variable `WEBPACK_DEV_SERVER_LISTEN_HOST`
when running `./bin/webpack-dev-server` binstub:

```bash
./bin/webpack-dev-server --listen-host 0.0.0.0
WEBPACK_DEV_SERVER_LISTEN_HOST=0.0.0.0 ./bin/webpack-dev-server
```

Dev server listen port can be set either in `config/webpacker.yml` or via environment variable `WEBPACK_DEV_SERVER_PORT`. The later takes precedence.

**Note:** Don't forget to prefix `ruby` when running these binstubs on windows

## Integrations
Expand Down
12 changes: 5 additions & 7 deletions lib/install/bin/webpack-dev-server.tt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ begin
dev_server = YAML.load_file(CONFIG_FILE)[RAILS_ENV]["dev_server"]

HOSTNAME = args('--host') || dev_server["host"]
PORT = args('--port') || dev_server["port"]
PORT = ENV['WEBPACK_DEV_SERVER_PORT'] || dev_server["port"]
HTTPS = ARGV.include?('--https') || dev_server["https"]
DEV_SERVER_ADDR = "http#{"s" if HTTPS}://#{HOSTNAME}:#{PORT}"
LISTEN_HOST_ADDR = args('--listen-host') || DEFAULT_LISTEN_HOST_ADDR
LISTEN_HOST_ADDR = ENV['WEBPACK_DEV_SERVER_LISTEN_HOST'] || DEFAULT_LISTEN_HOST_ADDR

rescue Errno::ENOENT, NoMethodError
$stdout.puts "Webpack dev_server configuration not found in #{CONFIG_FILE}."
Expand All @@ -47,11 +47,9 @@ rescue Errno::EADDRINUSE
exit!
end

# Delete supplied host, port and listen-host CLI arguments
["--host", "--port", "--listen-host"].each do |arg|
ARGV.delete(args(arg))
ARGV.delete(arg)
end
# Delete supplied host CLI argument
ARGV.delete(args("--host"))
ARGV.delete("--host")

env = { "NODE_PATH" => NODE_MODULES_PATH.shellescape }

Expand Down
4 changes: 2 additions & 2 deletions lib/webpacker/dev_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def hot_module_replacing?
end

def host
fetch(:host)
ENV["WEBPACK_DEV_SERVER_LISTEN_HOST"] || fetch(:host)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should all match their method names, e.g. ENV["WEBPACK_DEV_SERVER_HOST"]

end

def port
fetch(:port)
ENV["WEBPACK_DEV_SERVER_PORT"] || fetch(:port)
end

def https?
Expand Down