Require Tree in Asset Pipeline

Require tree in asset pipeline

In your app/assets/javascripts/application.js file, try putting:

//= require_tree ../Typefaces

See more:
http://guides.rubyonrails.org/asset_pipeline.html

Let me know if that works.

what's the reason behind the default require_tree in asset pipeline?

require_tree . will result in you having a single file (application.js in this case) holding all your scripts that is there in the folder. And the fact that browsers will only pull that file once from your web server (unless you do a Ctrl + R refresh or there is a change in file cache property), does make the apps behave faster for subsequent requests.

Unless of course you have an application that have quite varying and huge scripts and a typical user is not expected to move around much that he wouldn't need majority of those. Which obviously is not very common case.

for additional and detailed information. look here
http://guides.rubyonrails.org/asset_pipeline.html

Does require_tree require files inside 'vendor/assets' and 'lib/assets' ? - Rails

No, require_tree . will only load the assets in the local directory, hence the dot after require_tree, which specifies only the directory where the application asset file exists. If you want to include files in vendor/assets and lib/assets, you should do something like this (or similar for stylesheets):

//= require_tree ../../../vendor/assets/javascripts/.
//= require_tree ../../../lib/assets/javascripts/.

(From this question.)

require_tree argument must be a directory in a Rails 5 upgraded app

I finally figured it out. So because I am doing the upgrade, RailsDiff didn't tell me that I was missing something.

So the error message wasn't incorrect, however, what I forgot to do was to create an empty directory.

In my app/assets/javascripts/cable.js, I had the following:

//= require_tree ./channels

However, I forgot to actually create that folder.

So to fix this, all I had to do was create an empty folder within app/assets/javascripts called channels. Also, because git ignores empty directories, within that newly created folder, I also had to create an empty file called .keep.

So once I did the following, everything worked like a charm:

  • Create folder: app/assets/javascripts/channels
  • Create empty file within that folder: app/assets/javascripts/channels/.keep

Everything works perfectly now.

Why is require_tree before require_self in default application.css?

The change was made in order to allow styles defined in application.css to override previously included styles.

See Rails issue #11639 and this commit which changed the order of the require instructions.

Understanding require_tree load order

The reason for this is require_tree loads the files or folders alphabetically whether it is a file or folder. One a way of solving your problem is individually loading the file without using require_tree. Another way you can achieve your goal is by adding _ underscore before the file you want to load first. Because require_tree will load the file prefixed with underscore before the other files. So in your case your directory structure would like following

Updated dir structure

Asset Pipeline: excluding an admin.css file

Similar question was asked earlier and you should check that one.

Sprockets uses manifest files to determine which assets to include and serve. These manifest files contain directives — instructions that tell Sprockets which files to require in order to build a single CSS or JavaScript file. With these directives, Sprockets loads the files specified, processes them if necessary, concatenates them into one single file and then compresses them (if Rails.application.config.assets.compress is true). By serving one file rather than many, the load time of pages can be greatly reduced because the browser makes fewer requests.

You can have as many manifest files as you need. For example the admin.css and admin.js manifest could contain the JS and CSS files that are used for the admin section of the application.

In particular, you can specify individual files and they are compiled in the order specified.

Example and more details can be found in this guide.

Thus, your new application.css would become:

/*
*= require styles
*= require layout
*/

/* Other styles */

Rails 3 Should require_tree be used?

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 Topics



Leave a reply



Submit