Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1 asset pipeline and manually ordered Javascript requires

I am trying to convert an existing app to the new 3.1 asset pipeline layout, and want to include a lot of vendor files that have to be in a specific order, (underscore.js and backbone being one pair). As such, I can't just use a = require_tree . to pull in my vendor files, (without renaming each file with a prefix. Yuck).

The following is within my app/assets/javascripts/application.js file:

 //= require modernizr-1.7 //= require jquery-1.6.1 //= require underscore-1.1.5 //= require backbone-0.3.3 //= require_tree . 

I have tried every combination of with/out extensions, with/out the require_tree and with/out the relative paths, and nothing works. All of my vendor files are in /vendor/assets/javascripts/.

I feel like I am being stupid because this seems like such an obvious use case, (including specific files by name in an order is common with JS, no?) that I must be doing something idiotic?

like image 974
beseku Avatar asked May 27 '11 08:05

beseku


2 Answers

You have two possible structure : the first one and the second one. With both following examples, you expose a package at /assets/externals.js. You can javascript_include_tag this package, but you can also require it in your application.js file.

The first one

vendor/ ├── assets │   ├── javascripts │   │   ├── externals.js │   │   ├── modernizr-1.7.js │   │   └── underscore-1.1.6.js │   └── stylesheets └── plugins 

The file externals.js contains :

//= require ./underscore-1.1.6.js //= require ./modernizr-1.7.js 

The second one

vendor/ ├── assets │   ├── javascripts │   │   └── externals │   │       ├── index.js │   │       ├── modernizr-1.7.js │   │       └── underscore-1.1.6.js │   └── stylesheets └── plugins 

The file index.js contains :

//= require ./underscore-1.1.6.js //= require ./modernizr-1.7.js 
like image 166
Romain Tribes Avatar answered Sep 30 '22 09:09

Romain Tribes


You can require each file in particular order and then add:

//= require_self 

instead of:

//= require_tree . 
like image 26
martinciu Avatar answered Sep 30 '22 07:09

martinciu