When I run assets:precompile with the WEBPACKER_PRECOMPILE flag set to false, it should skip installing Yarn dependencies:
circleci@3386211f0f38:~/project$ WEBPACKER_PRECOMPILE=false bundle exec rake assets:precompile
yarn install v1.9.4
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.59s.
I'm splitting my Sprockets and Webpacker compiles into separate tasks and don't want this running unnecessarily.
Without setting WEBPACKER_PRECOMPILE, yarn:install will be called twice:
Setting WEBPACKER_PRECOMPILE only disables the Webpacker enhancement.
WEBPACKER_PRECOMPILE=false isn't meant to skip installing Yarn dependencies, but only to skip compiling webpack assets when calling bin/rails assets:precompile.
可以直接到 gems 里修改源码,因为已经强制为预编译资产,我的结局方法
/gems/webpacker-4.0.7/lib/tasks/webpacker/compile.rake
```ruby
def enhance_assets_precompile
# yarn:install was added in Rails 5.1
# deps = yarn_install_available? ? [] : ["webpacker:yarn_install"]
# Rake::Task["assets:precompile"].enhance(deps) do
# Rake::Task["webpacker:compile"].invoke
# end
end
WEBPACKER_PRECOMPILE=falseisn't meant to skip installing Yarn dependencies, but only to skip compiling webpack assets when callingbin/rails assets:precompile.
Let us decide ourselves if we need to install yarn dependencies. This makes both, our deployment and our CI builds slower. We want to handle this, or at least be able to handle this ourselves.
@mfittko, then you also need to go complain to Rails because it also adds yarn:install to assets:precompile.
Webpacker should only add yarn:install if you’re using Rails < 5.1, and only if WEBPACKER_PRECOMPILE=false is not set.
I think because Rails adds yarn:install to assets:precompile, first an env-var or something would need to be added there first, then Webpacker could use that env-var or whatever to handle the pre-Rails 5.1 situation.
I have the following yarn.rake file in my lib/tasks directory.
# frozen_string_literal: true
# Remove yarn install tasks as dependencies.
if Rake::Task.task_defined?('assets:precompile')
task = Rake::Task['assets:precompile']
task.prerequisites.delete('webpacker:yarn_install')
task.prerequisites.delete('yarn:install')
end
I'll try that out, thanks @jpickwell ! Already did something similar already :)
Okay, just to share this, I'm using following in my CI config now:
echo "Rake::Task['assets:precompile'].prerequisites.delete('webpacker:yarn_install')" > lib/tasks/yarn.rake
find vendor/bundle -name "yarn.rake" -delete -print | xargs touch
Unfortunately, yarn:install isn't a prerequisite and I saw no other way than clearing out the file from the rails install in my bundle.
Most helpful comment
I have the following
yarn.rakefile in mylib/tasksdirectory.