我在 lib/tasks/foo.rake 中有这个:
Rake::Task["assets:precompile"].enhance do
print ">>>>>>>> hello from precompile"
end
Rake::Task["assets:precompile:nondigest"].enhance do
print ">>>>>>>> hello from precompile:nondigest"
end
当我在本地运行 rake assets:precompile
时,两条消息都会被打印出来。
当我推送到 heroku 时,只打印非摘要消息。然而,according to the buildpack ,推送正在执行与我在本地完全相同的命令。
为什么对基础 Assets 的增强:预编译案例在 heroku 上不起作用但在本地起作用?
最佳答案
我一直在研究这个问题,我发现 assets:precompile
的行为取决于 RAILS_ENV
和 RAILS_GROUPS
是否都已设置或未在本地查看。
# This works
→ bundle exec rake assets:precompile RAILS_ENV=production
>>>>>>>> hello from precompile:nondigest
>>>>>>>> hello from precompile
# This works
→ bundle exec rake assets:precompile RAILS_GROUPS=assets
>>>>>>>> hello from precompile:nondigest
>>>>>>>> hello from precompile
→
# This does not work :'(
→ bundle exec rake assets:precompile RAILS_ENV=production RAILS_GROUPS=assets
>>>>>>>> hello from precompile:nondigest
→
问题出自https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/sprockets/assets.rake在 invoke_or_reboot_rake_task
方法中,如果您将 Rake::Task[task].invoke
行替换为 ruby_rake_task task
那么它的工作方式就像您期望的那样.我一直在研究这是为什么,但没有找到原因。
由于这两个变量都在 Heroku 构建包中设置,因此您可以创建自定义构建包而无需同时设置 GROUP 和 ENV 设置,但我认为这有点过分了。在这种情况下,您应该能够增强 assets:precompile:primary
或 assets:precompile:all
并获得与您期望的意图类似的结果。
关于ruby-on-rails - 为什么 Rake 任务增强在我的本地环境和部署到 Heroku Cedar 时有所不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9966408/