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

Consistently use NODE_ENV and support non-standard env #1304

Merged
merged 1 commit into from
Mar 3, 2018

Conversation

gauravtiwari
Copy link
Member

@gauravtiwari gauravtiwari commented Feb 27, 2018

Closes #1265

This PR fixes issue for non-standard environments

  • Adds separate env class for gem
  • Consistent gem and npm package behaviour
  • Use NODE_ENV for consistency
  • Removes deprecated set from ConfigList type
  • Adds tests

cc// @rmehner @IgorDmitriev @doits @hiromi2424 do you wanna try out this branch please?

@gauravtiwari gauravtiwari requested a review from javan February 27, 2018 22:48
lib/webpacker.rb Outdated
@@ -2,6 +2,9 @@
require "active_support/logger"
require "active_support/tagged_logging"

ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
ENV["NODE_ENV"] ||= ENV["RAILS_ENV"]
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably don't want to set any ENV vars here since this file gets required by applications. It was OK to do in bin/* files since those are separate one-off processes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ahh right, I was thinking setting here will set it in all environments (like in rails console) but I guess that's not ideal.

@javan
Copy link
Contributor

javan commented Feb 27, 2018

Does this allow RAILS_ENV and NODE_ENV to be set separately / independently? I imagine there are cases where you want NODE_ENV to be production and RAILS_ENV to be something else. Like deploying your Rails app to a staging environment.

@gauravtiwari
Copy link
Member Author

Yep it was possible before as well except that we were falling back to RAILS_ENV if there was no NODE_ENV set. I removed that case for clarity so it always checks for NODE_ENV first and then falls back to 'production'. What do you think?

@javan
Copy link
Contributor

javan commented Feb 27, 2018

What happens when there's no config/webpack/[environment].js file matching the env? I can't tell at a glance.

@gauravtiwari
Copy link
Member Author

So if the config file is missing then binstub will raise an error (same as Rails env config):

screen shot 2018-02-28 at 08 10 34

If the config file is present matching environment but the settings isn't present inside webpacker.yml for that environment:

screen shot 2018-02-28 at 08 11 00

If both config file and env is defined in webpacker.yml then:

screen shot 2018-02-28 at 08 13 38

@gauravtiwari
Copy link
Member Author

gauravtiwari commented Feb 28, 2018

The one thing I am unsure is:

screen shot 2018-02-28 at 08 16 11

If we open rails console without NODE_ENV and try to access Webpacker instance then it raises an error. I am not sure if we should use RAILS_ENV as fallback or don't raise at all? The reason for removing RAILS_ENV was to keep it consistent with JS ecosystem - so it's either NODE_ENV or production fallback to make things clear.

That's why I moved the env declaration to module itself from binstubs so NODE_ENV is consistently set but I guess that's isn't ideal?

@gauravtiwari gauravtiwari mentioned this pull request Feb 28, 2018
@hiromi2424
Copy link

My Circle CI test was failed with following message:

Webpacker is installed 🎉 🍰
Using /******/config/webpacker.yml file for setting up webpack paths
rails aborted!
Using webpacker requires that you set NODE_ENV environment variables. Valid values are default, development, test, staging, production

We expect webpacker to fallback RAILS_ENV, that ours is 'test' 😢

If this is breaking change please update minor(or major) version..

@gauravtiwari
Copy link
Member Author

@hiromi2424 Ahh I see, what is NODE_ENV set to test?

@hiromi2424
Copy link

hiromi2424 commented Mar 1, 2018

@gauravtiwari It passed with NODE_ENV: test .

RAILS_ENV fallback also allow us to separate webpacker env and application env, e.g.:

LET

RAILS_ENV = staging
webpacker.yml contains:

  • development
  • test
  • production

ACTS

It fallbacks production config for webpacker.
And when NODE_ENV=test specified, webpacker runs with test config, but rails side it runs on staging environment.
yay!


I think raising error is not comfortable decision since webpacker is for rails application that requires RAILS_ENV but not NODE_ENV.

@sunnyrjuneja
Copy link
Contributor

I highly endorse the idea of using RAILS_ENV as a fallback for NODE_ENV.

@gauravtiwari
Copy link
Member Author

alright makes sense, will push an update 👍

@gauravtiwari gauravtiwari force-pushed the non-standard-env branch 2 times, most recently from 1a5b921 to 76d5f16 Compare March 3, 2018 14:07
@gauravtiwari gauravtiwari merged commit 3923768 into master Mar 3, 2018
@gauravtiwari gauravtiwari deleted the non-standard-env branch March 3, 2018 15:36
@hiromi2424
Copy link

Huge thanks! @gauravtiwari
Now CI and staging build of my project has been succeeded!!!

tricknotes added a commit to tricknotes/webpacker that referenced this pull request Mar 14, 2018
The Rails defined custom env is allowed by rails#1304.
However Webpacker custom env is not allowed for now.

```
/Users/tricknotes/dummy-app/node_modules/@rails/webpacker/package/config.js:16
    delete defaults.extensions
           ^

TypeError: Cannot convert undefined or null to object
```

This commit allows using user defined env for Webpacker.
tricknotes added a commit to tricknotes/webpacker that referenced this pull request Mar 14, 2018
The Rails defined custom env is allowed by rails#1304.
However Webpacker custom env is not allowed for now.

```
/Users/tricknotes/dummy-app/node_modules/@rails/webpacker/package/config.js:16
    delete defaults.extensions
           ^

TypeError: Cannot convert undefined or null to object
```

This commit allows using user defined env for Webpacker.
tricknotes added a commit to tricknotes/webpacker that referenced this pull request Mar 14, 2018
The Rails defined custom env is allowed by rails#1304.
However Webpacker custom env is not allowed for now.

```
/Users/tricknotes/dummy-app/node_modules/@rails/webpacker/package/config.js:16
    delete defaults.extensions
           ^

TypeError: Cannot convert undefined or null to object
```

This commit allows using user defined env for Webpacker.
@Systho
Copy link
Contributor

Systho commented Mar 15, 2018

@gauravtiwari
We still can't use NODE_ENV=staging RAILS_ENV=staging (Or just RAILS_ENV=staging)?

What would be the expected way to provide a different webpacker config in custom environment ?

The typical use case would be using different JS API keys.

Currently I'm using different sections in webpacker.yml to set the values then the DefinePlugin like this in config/environment.js

environment.plugins.append('Define', new webpack.DefinePlugin({
  "GA_PROPERTY_ID":        JSON.stringify(config.ga_property_id),
  ....
}));

@gauravtiwari
Copy link
Member Author

@Systho Sorry about that, I am working on a PR to fix this.

@Systho
Copy link
Contributor

Systho commented Mar 15, 2018

No problem, It's great that you're aware of the problem.
Since this issue is closed I was mostly wondering whether I was doing something wrong. That's why I provided my use case :)

Keep up the good work and thank you !

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.

5 participants