rails / webpacker

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

Cache does not care about dependent environment variables #3248

Closed Tolsee closed 2 years ago

Tolsee commented 2 years ago

Problem statement

When we use environment variables like:

function urlPath() {
  if (process.env.DEPLOYMENT_ENV === 'production') {
    return '/prod';
  }

  return '/staging';
}

Builds with these commands are actually different.

DEPLOYMENT_ENV=production bin/rails webpacker:compile 
DEPLOYMENT_ENV=development bin/rails webpacker:compile 

But, right now webpacker will say "Everything's up-to-date. Nothing to do"

Possible solution

When calculating digest, add dependent environment variables as well. https://github.com/rails/webpacker/blob/275fbb56e6b0c837ae4b214a276891558147d861/lib/webpacker/compiler.rb#L54-L61

justin808 commented 2 years ago

Caching of the bundle creation (i.e., determining if we need to re-run bin/webpack) should really be moved outside of the main development flow, and there should be a way to add additional values to the cache key.

@Tolsee another option could be to have your deployment dirs based a different Rails ENV for production and production_staging, and then configure that in webpacker.yml.

justin808 commented 2 years ago

Consider not having automatic compilation for production/staging. You should have a build script.