The asset_pipeline guide mentions the use of require_tree for JavaScript and style-sheet files to recursively load every file of the respective files types, but doesn't mention any advantages or disadvantages of doing so. For instance are there any benefits to me having require_tree in my application.js file? In one case it prevents me from manually having to include individual javascript files for various pages. On the other hand theres been many occasions where I forget that I'm using the require_tree directive and accidentally loading libraries more than once.
Are there any performance-related issues involved in loading javascript/stylesheet files for pages that don't even make use of them (as would be the case in using require_tree)?
If you are using require_tree, there is absolutely no reason to manually require your own files. You shouldn't do so, but it won't hurt if you do since Rails is smart enough to not require the same file twice.
However, you should require files that are "vendorized" or in a gem. "require_tree" won't require those files automatically. It only requires files that are in app/assets/{type}.
Related
What advantages does compiling the LESS files have over just git cloning the Bootstrap Repo and copy/pasting various bootstrap.css files from there?
With LESS compiled Bootstrap you can get a customized and minified CSS file exclusive for your requirements (classes and colors). It is just performance, it reduce file size and petitions to the web server, principally.
Take a look to Bootstrap online LESS compiler (http://getbootstrap.com/customize/) to view all the variables you can choose.
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.
I came to notice that there is one folder called assets in the root folder.To know more about it,I went through this link.Now I want to know adding some css in these files is good or shall I add css to to the main.css file inside css folder.
The asset folder is automatically generated by Yii based upon your environment so best avoid putting your CSS, images etc inside here. It also best to not commit these folders and files into SVN as they are automatically generated and folder names will differ from your qa/staging/live site to your local site.
There are some good reasons to use Yii's assets.
it prevents naming conflicts in css and js files
it allows you to keep CSS and JS files under your document-root but outside of your web-root (for easier version control)
it allows to easily switch between sets of CSS & JS files, rather than having to deal with each file individually (suppose the system admin needs to revert back to a previous version).
it allows you to publish assets (images, JS & CS) to several websites hosted on the same server.
Please check here or there for more details.
Well, when i started my first Yii project, i also put my CSS and JS files in assets. It works but then i found that its not just the right way. Its better to make a separate directory for your CSS file(s). Also there are some auto generated files in assets, so to avoid mix-up with those and your i prefer to make it separate. Hope you got the point.
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 introduces a new way of organizing both JS and CSS with the introduction of manifest files. For example, application.js might look like this:
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require_tree .
This will grab various bits of Jquery, all of your own JS, concatenate them together and serve it as a single file to clients. Simple enough.
Unfortunately the picture is not so clear to me with SASS. SASS already has concatenation built in using #import.
Should I change all of my partials into full SASS files and then concatenate them using the manifest file or continue using #import? Why?
Sprockets converts all imports to CSS before concatenating, so it can't be used to share mixins and variables across files. I'm guessing this is going to stay that way just because you can import SASS, LESS and CSS files via that method.
So here's how I do it:
If I have ERB to include (mostly for asset_path() calls), I put them in my main file, application.css.scss.erb
If I have vendored CSS I want to include, I require it via Sprockets, e.g. //=require jquerymobile
In that same file, I use the SASS #import command to explicitly load all files. None of the #import'ed files may be .erb though.
load the basic stuff (e.g. reset) and imports with mixins
declare variables
import the specific styles
Here's how my app.css looks at the moment. Don't forget the ";" and the quotes:
// Using SASS import is required for variables and mixins to carry over between files.
#import "reset.css.scss";
#import "mixins.css.scss";
$color_base: #9b2d31;
$color_background: #c64e21;
// Using asset_path is important for browsers to use versioned url for the asset.
// This lets us do aggressive caching.
$logo-url: url(<%= asset_path("logo.png") %>);
#import "application/layout.css.scss";
#import "application/sidebar.css.scss";
#import "application/videos.css.scss";
#import "application/pages.css.scss";
...
Note that I'm still exploring the Rails 3.1 asset pipeline, so your mileage may vary. I'll try to come back & update if I find anything else interesting.
The best way to solve this is to use the native #import directive as explained here: https://github.com/rails/sass-rails#important-note
This question was already answered here : how to use sprockets imports with sass
Hope this helps! :)
The sass-rails gem explicitly states not use the require syntax with SASS files - use SASS's #import statements instead.