Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asset precompile missing standalone javascript / css

I have that sneaking feeling I'm missing something obvious:

I ran

RAILS_ENV=production bundle exec rake assets:precompile

to precompile my assets before pushing to Heroku. Looking in /public/assets shows that application.js and application.css manifests successfully compiled, but none of my standalone files precompiled:

admin.js.coffee
blog.js.coffee.erb
[ ... several more similarly named ... ]
twitter.js.coffee.erb

and

admin.css.less
home.css.less
public.css.less

are all missing from /public/assets.

I thought that Rails would precompile the application.js/.css files, plus anything else that doesn't end in js/css:

The default matcher for compiling files includes application.js, application.css and all files that do not end in js or css:

[ /\w+\.(?!js|css).+/, /application.(css|js)$/ ]

from: http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets

I don't want to have to manually update config.assets.precompile every time an asset file name changes. Am I missing something that will make Rails precompile these assets?

Update

Based on @Richard Hulse's answer below, I tested it out by created a separate manifest file for a standalone asset (i.e. I renamed twitter.js.coffee.erb to twitter-include.js.coffee.erb and added twitter.js with a single //= require pulling in the renamed original). This seems to work.

There must be a cleaner way than this, and it seems to contradict the Rails guide quoted above. The guide says the only files that won't be compiled are .js or .css files not named application. What I'm see is only .js or .css being directly compiled (i.e. not via a manifest) - nothing else.

like image 320
Dave W. Avatar asked Dec 30 '11 21:12

Dave W.


1 Answers

Two things:

If these files are included in your application manifests, then they are included in the site's application files.

There should be a line in both application manifests: require_tree, that will pick up all the assets automatically for your. Is that in these files?

Edit in reply to edit:

The way I would structure this is have two sets of manifests. The standard ones (application.css/.js) are for public. The admin set are for admin pages. Include all the stuff you want in admin.js/.css manifests and add those files to the precompile array:

config.assets.precompile += ['admin.js', 'admin.css']

This would allow you to share code between the two groups. For example you can include jquery in both, but jquery_ujs in admin only. In your admin section layout just include the admin manifests instead of the application manifests.

In practice you will then add new files to the application or admin manifests as you develop the site, and you won't have to change the precompile configuration.

Once you get to the point of adding lots of assets, an admin section and so on, it is expected that things will get more complex and that you have to be explicit about what is included in manifests and the order (as opposed to require_tree).

like image 150
Richard Hulse Avatar answered Nov 30 '22 21:11

Richard Hulse