-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Compile is always using production env? #1558
Comments
@cmcfadden Are you using the latest version? https://github.com/rails/webpacker/blob/master/lib/webpacker.rb#L16 & https://github.com/rails/webpacker/blob/3-x-stable/lib/webpacker.rb#L16 (3.5.3) |
We're using 3.5.3. The change to with_node_env you point to doesn't seem to matter in this case because the calling line is https://github.com/rails/webpacker/blob/master/lib/tasks/webpacker/compile.rake#L24 which hardcodes |
Any update on this? It seems odd that you can only use the production environment to compile assets. Edit: I'm happy to do a merge request for this. I just want to make sure there isn't a reason we don't want to use the specified environment. |
@davekerber Sorry about the delay. The idea behind this was to have the same behavior as we have in But I see the point, maybe |
@gauravtiwari also sorry for the delay. :) I agree, it should default to production, and should allow you to specify the environment. We regularly have staging or demo environments that have different settings from production. Should it be based on RAILS_ENV or NODE_ENV? |
@gauravtiwari Yeah this is killing me too. I'm trying to precompile a test build on my CI server so that the parallel test runners can just pick up and run with the build. If I don't precompile, and just let Webpacker compile on the first integration spec that happens, it throws off the spec timings, which makes it impossible to load balance my test runners based on test time (as random tests will always take I'd like to prebuild with It doesn't make a lot of sense to me to force it to As mentioned above, if Also as a suggestion, just put a line of output to |
@davekerber happy to help with a PR if you've got too much on your plate! |
For the time being, I added a custom Rake task to my Rails project to unblock me until this issue gets closed. Here's the patch if anyone else wants it: # lib/tasks/webpacker.rake
namespace :ci do
namespace :webpacker do
task :compile do
require 'webpacker'
# Until https://github.com/rails/webpacker/issues/1558
# is resolved, patch Webpacker to allow custom NODE_ENV in builds.
module AllowNodeEnvInWebpackerCompile
def with_node_env(*)
puts "Compiling pack with NODE_ENV=#{ENV['NODE_ENV'] || 'production'}"
yield
end
end
Webpacker.extend(AllowNodeEnvInWebpackerCompile)
Rake::Task['webpacker:compile'].invoke
end
end
end |
This appears to continue to be a problem with webpacker 4.0.2. Our workaround is to do two separate precompilations:
Only the second invocation creates the expected files in |
In my anecdotal usage, |
Yes, of course. Could you please tell us your use case? As explained in different threads, setting For example: You are deploying to That's why we kept it like so. |
I want to precompile my JS on my CI server with I wouldn't deviate from Edit: not precompiling my JS means that each of my parallel test runners has to spend 3 minutes compiling, as well as time pulling in build caches. |
@gauravtiwari thank you for that output; I should have tested with a fresh Rails app 😊. Mine is indeed different (I've removed some unrelated warnings and the long list of assets):
I'll have to investigate what the difference could be; my current hunch is some poor interaction with React on Rails. @dbalatero describes well the end goal here — we have multiple parallel CI jobs. Running the first test with on-demand compilation actually causes our first test to fail due to long compilation times. We precompile the test assets once to:
|
How long is "long"? You might just need some optimization, how many packs are you packing? |
I've opened shakacode/react_on_rails#1202; it does appear that RoR monkeys with webpackers tasks; apologies for the confusion!
I forget the exact numbers now, but that's a bit off topic for this issue. You can see my earlier comment where I mention that the precompilation takes 2 minutes. We are lazy at the moment and only have one massive pack. It's not ideal, but we've made that tradeoff for now and aren't eager to change that yet. Either way, performing precompilation once before the parallel test machines spin up saves us actual money by not wasting CPU time. |
I would use some other env variable for this, which would be more reliable vs depending on |
That's certainly a workaround but it's still not my preference. Consider that:
If there's a desire to keep the compile task focused on production builds, I can buy that - but I don't really buy that setting node env to production is the safest assumption when wanting to build a precompiled test or dev pack, given the Node ecosystem doesn't necessarily work that way across all libs. In any case the patch I have above "works" but I'd rather not patch things if it's possible to avoid. |
I only asked because you said your tests would "fail due to long compilation times". Depending on what you use for a CI, there are tricks you can use to save your webpack cache (
I personally would use production packs on a CI, but in general, TDD/unit-testing would seem to prefer dev or test, while e2e/integration testing would prefer test or production packs. I agree that On the other side of that coin, there have been users who follow the docs and install their testing framework like e.g: yarn add --dev karma They may find that their It just really depends on seeing what works best for the tools & tests you are using. At the end of the day, there does not seem to be one unifying opinion, even within testing libs: |
Thanks for the lively discussion. This issue was fixed in #2341 |
It seems like with the changes introduced in d8dd446, the webpack gem is always going to build in the production environment, regardless of the environment that's set via the environment variable.
Was this change intentional? Or am I missing something in terms of how to build non-production with this gem?
The text was updated successfully, but these errors were encountered: