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

Allow overriding dev server settings using env variables #843

Merged
merged 1 commit into from
Sep 24, 2017

Conversation

gauravtiwari
Copy link
Member

@gauravtiwari gauravtiwari commented Sep 21, 2017

This PR removes cli args support for webpack dev server and adds support for env variables within Webpacker to override dev server settings.

For overriding dev server settings, simply set env variable before calling webpacker.

WEBPACKER_DEV_SERVER_HMR=true
WEBPACKER_DEV_SERVER_INLINE=true
WEBPACKER_DEV_SERVER_DISABLEHOSTCHECK=false

All settings for dev server inside config/webpacker.yml can be overriden, just make sure they are in caps.

@javan
Copy link
Contributor

javan commented Sep 21, 2017

Do we need to support dotenv with this change? I'm hesitant to add another dependency, and would prefer to start by just reading ENV (Ruby) / process.env (Node) directly.

@gauravtiwari
Copy link
Member Author

Yeah, I was too but later felt since we are encapsulating configs inside a node module it would make sense to provide env support out of the box. Obviously, we are adding another dependency but I guess the usefulness of this is greater and most of the apps will find this useful. What do you think?

@javan
Copy link
Contributor

javan commented Sep 21, 2017

I think people using dotenv (or any env manager for that matter) should just ensure their env vars are set before calling into webpacker.

@gauravtiwari
Copy link
Member Author

Yeah, totally but it's not that trivial to setup. Here is our doc about supplying env variable to Webpacker: https://github.com/rails/webpacker/blob/master/docs/env.md (which basically requires overriding Environment plugin).

@guilleiguaran
Copy link
Member

Warning: using Foreman/Invoker and npm dotenv at the same time can result in confusing behavior, in that Foreman/Invoker variables take precedence over npm dotenv variables.

I think this warning explain why we shouldn't add a default env manager and leave it to the users.

@gauravtiwari gauravtiwari changed the title Add support for env variables Allow overriding dev server settings using env variables Sep 21, 2017
@gauravtiwari
Copy link
Member Author

Makes sense 👍

@gauravtiwari gauravtiwari force-pushed the configurable-dev-server-options branch 2 times, most recently from ab15354 to 8416ca0 Compare September 21, 2017 19:25
@gauravtiwari
Copy link
Member Author

@guilleiguaran Could you please review and merge this one? I have dropped dotenv

@timkrins
Copy link

timkrins commented Sep 7, 2018

I can understand using ENV variables, but why specifically remove support for ARGV also?

Unable to use https certificates that I would usually expect to pass to webpack-dev-server like so:

webpack-dev-server --https --cert=/home/user/selfsigned.crt --key=/home/user/selfsigned.key

@gauravtiwari
Copy link
Member Author

Hey @timkrins

The previous implementation was limited. Webpacker uses dev server options (webpacker.yml or ENV) for an internal rake middleware and it was a bit complex to allow three sources for overriding dev server configuration - cli, webpacker.yml and ENV

This could be unified but haven't had time to look into this properly. For now, you could supply certs through config/webpack/development.js

For example:

// development.js
environment.config.merge({
   devServer: {
    https: {
      key: fs.readFileSync('/path/to/server.key'),
      cert: fs.readFileSync('/path/to/server.crt'),
      ca: fs.readFileSync('/path/to/ca.pem'),
    }
  }
})

@timkrins
Copy link

timkrins commented Sep 7, 2018

Hi @gauravtiwari ,

Thanks for the explanation.

I think as the webpacker webpack binstub does accept ARGV I was not expecting the webpack-dev-server binstub to ignore them.

I am aware I can do what you recommended, however, it is just a bit annoying not to be able to have access to do this via the commandline, when I know that webpack-dev-server supports it.

Is there a potential conflict adding ARGV to the execute_cmd function in dev_server_runner.rb? (just like in webpack_runner.rb)
ie.

    cmd = [
      "#{@node_modules_path}/.bin/webpack-dev-server",
      "--config", @webpack_config
    ] + @argv

@timkrins
Copy link

timkrins commented Sep 7, 2018

Ah, I can see I'm not the only one confused by this - just found #1672

@tbtalbottjr
Copy link

Can someone explain how to set the port for the webpacker dev server so that it is used by both the server and the client using a .env file or similar (as requested in #767)? I'm missing something simple, I'm sure, but I've been able to change the devServer port, but the client continues to use the port out of the webpacker.yml file. The only thing that has worked so far is to physically change the webpacker.yml file. I want to avoid that because I want to be able to run two versions of the app server at the same time and don't want to be changing a file that gets checked into the repository

Rails 5.1.7
Webpacker 4.02

Running app server through puma_dev.

Thanks.

@minimul
Copy link

minimul commented Aug 1, 2020

Same here.

ENV var overrides such as:

WEBPACK_DEV_SERVER_PORT=3011 bin/webpack-dev-server
WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server

are not working.

Rails 6.0.3.2
Webpacker 5.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants