svoop
svoop

Reputation: 3464

Precompiling assets issues when using wildcards in config.assets.precompile

I hava a few assets in a Rails 3.2rc1 app, here are some of them:

app/
  assets/
    stylesheets/
      application.css.scss
      users.css.scss
      projects.css.scss
      (...)
      parts/
        fonts.scss
        (...)

The application.css.scss is a mere manifest with lines such as:

@import 'parts/fonts';

And views/layouts/application.html.slim loads the precompiled stylesheets:

= stylesheet_link_tag "application", media: "all"
= stylesheet_link_tag params[:controller], media: "all"

So far, so good, works just fine for development. During deployment on production, rake assets:precompile is called and it worked fine for the application stylesheet. But the per-controller stylesheets were skipped and hence the app bombs.

To fix this, I've added this to config/environments/production.yml:

config.assets.precompile += %w(*.css *.js)

The per-controller stylesheets are compiled now, but the app still bombs with:

ActionView::Template::Error (application.css isn't precompiled):
    5:     = stylesheet_link_tag "application", media: "all"
  app/views/layouts/application.html.slim:5:in `_app_views_layouts_application_html_slim__4032169744877009540_23192340'

But when I look in public/assets/, the precompiled application-3218a93530ecb65582d5b345133dab49.css and it's compressed twin are there.

And if I replace the wildcards in config/environments/production.yml with a list of all per-controller stylesheets (in other words: all but application.css), everything works like a charm:

config.assets.precompile += %w(users.css projects.css)

Is the application stylesheet somewhat treated twice and thus causes the precompilation to not work as advertised?

Upvotes: 3

Views: 2778

Answers (1)

svoop
svoop

Reputation: 3464

Here's how I work around this for now:

files = Dir[Rails.root.join('app', 'assets', '{javascripts,stylesheets}', '**', '[^_]*.{js,css}*')]
files.map! {|file| file.sub(%r(#{Rails.root}/app/assets/(javascripts|stylesheets)/), '') }
files.map! {|file| file.sub(%r(\.(coffee|scss)), '') }
config.assets.precompile += files

There might be a better solution at some point: https://github.com/rails/rails/issues/3094

Upvotes: 5

Related Questions