Skip to content
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

How to exclude assets from gems/engines? Known approach looks tedious or like a hack #89

Open
tmaier opened this issue Apr 21, 2022 · 4 comments

Comments

@tmaier
Copy link

tmaier commented Apr 21, 2022

I want to exclude all assets I am not using from precompiling.
I understand that config.assets.excluded_paths exists and that it requires a full path.

In my case Rails.application.config.assets.paths returns

["/usr/src/app/app/assets/builds",
 "/usr/src/app/app/assets/images",
 "/usr/local/bundle/gems/view_component-2.53.0/app/assets/vendor",
 "/usr/local/bundle/gems/unobtrusive_flash-3.3.1/lib/assets/javascripts",
 "/usr/local/bundle/gems/unobtrusive_flash-3.3.1/lib/assets/stylesheets",
 "/usr/local/bundle/gems/turbo-rails-1.0.1/app/assets/javascripts",
 "/usr/local/bundle/gems/nested_form-0.3.2/vendor/assets/javascripts",
 "/usr/local/bundle/gems/heroicon-0.4.0/app/assets/images",
 "/usr/local/bundle/gems/actioncable-7.0.2.3/app/assets/javascripts",
 "/usr/local/bundle/gems/actionview-7.0.2.3/lib/assets/compiled"]

I would like to exclude most of them, e.g. "/usr/local/bundle/gems/nested_form-0.3.2/vendor/assets/javascripts" or "/usr/local/bundle/gems/actionview-7.0.2.3/lib/assets/compiled".

I have two reasons for this: It takes comparatively long (e.g. assets from heroicon), I don't like to waste space (I know, it is not too much) and I like to have a clean directory with only the files I really use.

This is the only way I found to do so:

Rails.application.config.assets.excluded_paths << File.join(Heroicon.root, 'app/assets/images')
# rubocop:disable Style/StringConcatenation
Rails.application.config.assets.excluded_paths << (Pathname.new(Gem.find_files_from_load_path('nested_form').first) + '../../vendor/assets/javascripts')
Rails.application.config.assets.excluded_paths << (Pathname.new(Gem.find_files_from_load_path('turbo-rails').first) + '../../app/assets/javascripts')
Rails.application.config.assets.excluded_paths << (Pathname.new(Gem.find_files_from_load_path('unobtrusive_flash').first) + '../../lib/assets/stylesheets')
Rails.application.config.assets.excluded_paths << (Pathname.new(Gem.find_files_from_load_path('unobtrusive_flash').first) + '../../lib/assets/javascripts')
Rails.application.config.assets.excluded_paths << (Pathname.new(Gem.find_files_from_load_path('view_component').first) + '../../app/assets/vendor')
# rubocop:enable Style/StringConcatenation

Note: I am unable to use this methodology to exclude actionview or actioncable.

Pathname.new(Gem.find_files_from_load_path('whaever').first) does not look like a good solution

What is the recommended way?
How can this be simplified?

@kevynlebouille
Copy link
Contributor

Hi @tmaier ,

I saw something in turbo-rail repo like that (put in your application.rb for instance)

config.after_initialize do
  config.assets.precompile -= Turbo::Engine::PRECOMPILE_ASSETS
end

You could use the same approach for anything come from another gem.

Kevyn

@tmaier
Copy link
Author

tmaier commented Apr 21, 2022

Thanks @kevynlebouille,

I think this does not work for Propshaft, as assets.precompile only exists for compatibility reasons. See

config.assets.precompile = []

Turbo::Engine::PRECOMPILE_ASSETS seems also just have the file names, not the path. See https://github.com/hotwired/turbo-rails/blob/6958e0e2742d4744da80128d178e7255989386a4/lib/turbo/engine.rb#L29

@haberbyte
Copy link

May not be ideal, but I used this workaround:

# config/initializers/assets.rb
Rails.application.config.after_initialize do |app|
    app.config.assets.paths = %w(
      app/assets/builds
      app/assets/config
      app/assets/images
    )
end

@brenogazzola
Copy link
Collaborator

We are currently taking a look at how we handle paths due to a caching bug and will take a look at this after we handle that.

monfresh pushed a commit to smcgov/SMC-Connect that referenced this issue Feb 10, 2024
Otherwise, propshaft will include all stylesheets in public/assets, even though they are already compiled inside of application.css. This is a known bug:

rails/propshaft#89
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants