rails / webpacker

Use Webpack to manage app-like JavaScript modules in Rails
MIT License
5.3k stars 1.47k forks source link

running webpack when NODE_ENV=test #1429

Closed ciampam9 closed 6 years ago

ciampam9 commented 6 years ago

👋 Hey all!

Is there a reason why we're not allowing webpack to run environment/test.js? It seems as though when running NODE_ENV=test ./bin/webpack, the nodeEnv in package/env.js is being overridden to production.

Currently, if the node env isn't a part of the whitelist (package/env.js):

const NODE_ENVIRONMENTS = ['development', 'production']
const DEFAULT = 'production'

it defaults to production (package/env.js):

module.exports = {
  railsEnv: railsEnv && railsEnv.match(regex) ? railsEnv : DEFAULT,
  nodeEnv: nodeEnv && NODE_ENVIRONMENTS.includes(nodeEnv) ? nodeEnv : DEFAULT
}

Would it be possible to whitelist test as well? If the package has a specific test environment we should be allowed to use it by setting the node env, imo.

I realize this is outlined in the docs but why would we still have a package/environments/test.js file if you cannot run it?

gauravtiwari commented 6 years ago

@ciampam9 Apologies for late reply. Good question - basically I wanted to remove it in this PR #1359 but totally forgot in the end. If you check out that PR I have explained the reasons for only having development and production for NODE_ENV.

In earlier versions of webpacker we have used Rails env as fallback env, which isn't ideal because both NODE_ENV and RAILS_ENV has different meaning for different environments.

For example: setting NODE_ENV=production means the webpacker will compile assets in production mode however at same time RAILS_ENV can be staging. Webpack and node libraries only understands development and production environments. However, it's normal to have many custom RAILS_ENV.

For your use case you could do, since now RAILS_ENV is used for loading configurations:

# NODE_ENV is development by default
RAILS_ENV=test ./bin/webpack

or you could use

RAILS_ENV=test bundle exec rails webpacker:compile
RAILS_ENV=test bundle exec rails assets:precompile
DougPuchalski commented 6 years ago

@gauravtiwari Not sure if this is the same issue but even when I set RAILS_ENV=test I get output files in my packs and not packs-test as configured in my webpacker.yml, so it seems to be using NODE_ENV which is overridden to production when selecting the config

gauravtiwari commented 6 years ago

@aceofspades What version of webpacker are you using?

DougPuchalski commented 6 years ago

@gauravtiwari Found my issue, looks like I wasn't in sync with the gem & node package. Interesting, would be nice if this condition were detected, it caused quite a bit of debug time