How can I exclude several CSS files from precompilation? - ruby-on-rails-3

I hvae a Rails 3.2.x application that uses a 3rd party JS library which itself includes a few CSS files. When I precompile assets all of my JS goes into application.js and all the CSS into application.css.
I'd like for the CSS files, which I have put into vendor/assets/stylesheets to be excluded from precompilation but still available so the JS library can use them. Any suggestions on how I might do that?

Just Remove the following lines from application.css
in assets/stylesheets directory
*= require_self
*= require_tree .
These lines by default selects all the files in stylesheets directory for compilation..

Related

config.assets.precompile - include a folder of files? or kill the precompile 'feature' entirely?

I have read and tried the Assets Pipeline guide here:
http://guides.rubyonrails.org/asset_pipeline.html
... which shows how to include specific files in a manually created and updated list, --OR-- the Proc which includes a directory (or directories) but then excludes all the other files which Rails ordinarily includes.
I want to += my folder of files to the normally included files.
I have tried the answers:
Rails config.assets.precompile setting to process all CSS and JS files in app/assets
What is the purpose of config.assets.precompile?
rails config.assets.precompile for specific sub folder
... the last of which appears to show a solution:
config.assets.precompile += ["*external/calendars*"]
which I changed to:
config.assets.precompile += %w["*javascript*"]
or
config.assets.precompile += ["javascript"]
(and about 20 other variations.)
... to get my assets/javascript folder. But the directory is not included, as evidenced by the error "...isn't precompiled."
The third method, is to give it
config.assets.precompile += %w( *.js )
... which works, but leads to a very, very long compile, I would assume finding every JS file it can discover, anywhere.
Needless to say, adding files to a manually updated list is not suitable for an in-progress application - and losing whatever unknown things Rails precompiles with an exclusionary Proc won't cut it either (yet those are the only two examples in the docs).
Is there not a simple wildcard solution to "+-=" a folder - or perhaps to just turn this 'feature' off, specify my JS per view, and still have it work on Heroku?
----EDIT - It gets more irrational the deeper I look.
Essentially, the solution is, "Load all the things Rails finds A-OK in Development Mode." And yet such an option does not exist?
The production.rb file, referring to the precompile line, says:
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
... and application.js has:
//= require_tree .
... so that should load all the files under that directory - but it doesn't. Why? The deeper I dig, the less sense this makes.
A good practice when dealing with multiple CSS/JS files to add to the asset pipeline is to simply create a new manifest for those files:
Let's say you have some JS files under lib/assets/javascripts/external/calendars and you want to load them through the asset pipeline.
You want to create an index.js manifest file with the following content:
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require_tree .
This way all JS files you add into the external/calendars directory will be included by default thanks to the require_tree . directive.
Now, in your app/assets/javascripts/application.js file add the following line:
//= require calendars
This should find your "calendars' manifest index file" and load all dependent JS files. No need to add anything into the asset pipeline, it will just work.

How to configure Rails 3 for JavaScript development

I'm moving an app from Rails 2 to Rails 3. I have a bunch of JavaScript files in the app, most of which are for working with Google Maps. Previously, my JS files were in public/javascripts/*.js. I noticed that they were not being loaded by any of the pages that used javascript_include_tag, which writes a JavaScript tag that tries to load the JS file from /assets/*.js. (e.g. /assets/application.js).
I read that Rails 3 expects JS files to be located in app/assets/javascripts/*.js. So I moved my JS files there, but they still won't be accessible at /assets/application.js, unless I run bundle exec rake assets:precompile first.
I can't have my JS development cycle be:
Make change to JS
Run bundle exec rake assets:precompile
Reload browser
Thats just too long. My question is how can I configure my app so that the development cycle is as follows?
Make change to JS
Reload browser
So far I've tried:
Enabling the line that reads "Bundler.require(:default, :assets,
Rails.env)" in config/application.rb
Setting config.assets.enabled = false (and true) in config/application.rb
Thanks in advance.
The Rails asset pipeline should be used with a manifest file. This allows Rails to concatenate all of your javascript when in production. In order to do this, you should have an application.js file that looks something like.
//= require jquery
//= require jquery_ujs
//= require_tree .
The important part here is the require_tree part. This tells Rails to include all of the other javascript files in the same folder. This means that you want both the manifest version of application.js and all of your custom javascript files to live in app/assets/javascripts.
A good guide for transitioning to the asset pipeline from Rails 2 can be found in this RailsCast: http://railscasts.com/episodes/282-upgrading-to-rails-3-1
recently i have this issues
I change file config/environments/development.rb
config.assets.digest = false
then that working

Excluding files from assets:precompile in rails

I use codekit for writing less which it then converts to css automatically.
I don't want rails to convert my less files to css, I rather codekit do it.
if I precompile the assets via
rake assets:precompile
I get
rake aborted!
cannot load such file -- less
How do I exclude a specific folder/file types from precompiling? (all my less files are in app/assets/stylesheets/less and the css (which I do want to be precompiled) are in app/assets/stylesheets/css
update
deleting application.less solves this but how do I excluding it from processing in the first place?
From the Asset Pipeline guide:
The default matcher for compiling files includes application.js,
application.css and all non-JS/CSS files (i.e., .coffee and .scss
files are not automatically included as they compile to JS/CSS):
[ Proc.new{ |path| !File.extname(path).in?(['.js', '.css']) }, /application.(css|js)$/ ]
If you have other manifests or individual stylesheets and JavaScript
files to include, you can add them to the precompile array:
config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
So, I would say that your solution is to modify config.assets.precompile to exclude .less files. Maybe something like this (in a suitable environment file, like config/environments/production.rb):
config.assets.precompile = [ Proc.new{ |path| !File.extname(path).in?(['.js', '.css', '.less']) }, /application.(css|js)$/ ]
If your directory structure under the app/assets folder is so:
application.css
/css
(generated by code kit)
|...home.css
|...index.css
/less
|...home.less (assuming this is the extension)
|...index.less
Then, in your application.css file, there must be a directive that says *= require_tree . This tells rails to scan all the files/directories and try to compile all the files into one css file.
Change this to *= require_directory ./css and it will load the files under the css directory for compilation.

What is the purpose of config.assets.precompile?

In Rails 3.1, you must whitelist files that you want included in asset precompilation. You must open up config/environments/production.rb and explicitly include assets you want precompiled:
config.assets.precompile += ['somestylesheet.css']
If you don't do this this and you run rake assets:precompile, your asset will not be copied to public/assets, and your app with raise an exception(therefore causing a 500 error in production) when an asset is not found.
Why is this necessary? Why aren't all assets automatically precompiled?
This current approach creates extra code and stress when deploying. Wouldn't it be easier to blacklist/exclude assets so things worked right out of the box? Anyone else share these feelings?
Most assets are automatically included in asset precompilation. According to the RoR Guide on the Asset Pipeline:
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)$/ ]
You would use config.assets.precompile if you have additional assets to include:
config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
Or you could overwrite it.
I think it has to do with the pipeline/sprockets ability to require separate files.
For example, I have an admin.js file in my app/assets/javascripts folder. But all it does is require several other .js files.
//= require jquery
//= require jquery_ujs
//= require jquery.colorpicker.js
//= require jquery.wysiwyg.js
//= require wysiwyg.image.js
//= require jquery.fileupload.js
//= require jquery.fileupload-ui.js
//= require codemirror.js
//= require css.js
//= require admin_load
This is because (a) I'm using external js plugins and (b) I like to keep things like jQuery onload handlers in separate files.
If every .js file was precompiled, then it would precompile each one of these individual files–which is totally unnecessary. All I want/need is the single admin.js file precompiled.
Same goes for CSS files.
The assets precompile to me is cool so you dont end up deploying assets that you do not want. Dont also forget about the uglifer gem that helps compress your javascripts. Imaging all this are not existing and you just deploy your app and you find out that you have unused css files and uncompressed javascripts. how would you feel. this is just my own opinion and i say the asset pipeline is the coolest thing in rails.. Being able to manage all your assets properly.
And mind you if i am rails i would not want to compile assets that you would not want so you would say in your mind why did this guy compile these assets.. :)

rails 3.1 customize sprockets precompile assets

I am trying to customize what assets get precompiled. In production I only want application.js and applicaiton.css to be compiled and not all the subsequent files that are included.
For example giving the following files I only want one file (appliation.css) to be output when I run the precompile rake task
##application.css
//include components/form.elements
//include components/lists
The default is to precompile all assets in the assets directory and it is quite messy.
EDIT correction it is the default to precompile all non js and css files in the assets directory. If however you have a file like form.elements.js sprockets will precompile it thinking it is a non js/css file.
Check the sprockets documentation, it is quite extensive and well-written.
For your particular problem, the following application.css would only include the components/form_elements and components/lists directories, recursively:
/*
*= require_tree components/form_elements
*= require_tree components/lists
*/
If form_elements and lists are css files, just replace require_tree with require.
The default precompile options work as long as you don't have any files with extra periods in them. like (jquery.treeview.js)
I was able to to get my assets to compile the way I wanted by explicitly defining the individual files that I wanted compiled.
config.assets.precompile = [ 'application.css' ]
now in the public assets folder there is only one file that includes all the suplemental file content minimized.
EDIT
Rails default precomplie option has been changed to
[ Proc.new{ |path| !File.extname(path).in?(['.js', '.css']) }, /application.(css|js)$/ ]
so files with extra periods will compile properly now