Asset Pipeline Pre-Compile all asset folder sub di

2019-07-12 09:48发布

问题:

I like to keep my assets highly organized, however, I am running into an issue with precompile. It is not picking up the sub directories. Here are the paths:

assets
>fonts
>images
>>backgrounds
>>home
>>icons

Everything works on dev, but prod = no go. In my prod environment I already do:

 config.assets.enabled = true
  config.assets.compile = true
  config.assets.precompile += ['*.js', '*.ico', '*.png', '*.jpg']
  config.assets.initialize_on_precompile = true

And I am currently running the following function in my application.rb file:

   # Add All Asset Pipeline Sub Folders
    Dir.glob("#{Rails.root}/app/assets/images/**/").each do |path|
      config.assets.paths << path
    end

This is what I have been working with no, with not much luck. I was hoping to dynamically pick up all directories created/changed etc with few lines of code so I do not have to add them one by one.....

  # Add All Asset Pipeline Sub Folders
    Dir.glob("#{Rails.root}/app/assets/images/**/").each do |path|
      config.assets.paths << path
      config.assets.precompile += ['#{path}/*']
    end

Thoughts?

Updated Solution:

I ended up going with the following in my production rb:

  config.assets.precompile += ['*.js', '*.ico', '*.png', '*.jpg']
  config.assets.precompile += [ Dir["#{Rails.root}/app/assets/images/**/*"].reject {|fn| File.directory?(fn) } ]

回答1:

# Add All Asset Pipeline Sub Folders
Dir.glob("#{Rails.root}/app/assets/images/**/").each do |path|
   ..  # seems you include /assets/images sub directories, expand the path

Ruby command:

Dir.glob("#{Rails.root}/app/assets/images/**/") 

returns directories, located inside

/images

directory. I assume, that you push to precompiler images assets only. Has a sense? Replace it with:

 Dir.glob("#{Rails.root}/app/assets/**/")