Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1, exclude JS files from asset pipeline

I know there are a million questions already on this, but I can't get this.

I want to include most of my JS files in the asset pipeline, but I have a few I want to load conditionally (or only on certain pages). These are big, complicated files and will never, ever be used by 95% of the users, so I'd rather not have them loaded for every user. One set of JS files is for a calendar, placed in:

app/assets/javascripts/calendar

So my manifest is set up to include only the top directory (and exclude the calendar subdirectory):

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_directory .

My config/environments/production.rb:

# Compress JavaScripts and CSS
config.assets.compress = true

# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false

# Generate digests for assets URLs.
config.assets.digest = true

# This following config is left over from previous Rails app,
# so not sure if it's now unnecessary ...
# Disable Rails's static asset server
# In production, Apache or nginx will already do this
config.serve_static_assets = false

In the view, I'm using Ryan Bates' nifty_layout to manually include the calendar files:

javascript "calendar/date.js", "calendar/jquery.weekcalendar.js", "calendar/custom.js"

I've tried precompiling in both development and production -- the docs aren't clear where I'm supposed to do this, but it looks like production.

And when I run the page, I get this:

ActionView::Template::Error (calendar/date.js isn't precompiled)

I don't want it precompiled. I want it loaded manually. (Actually, it would be OK to precompile in a file other than the main application.js that is created, but I don't know how to do that.)

What's the solution?

Thanks!

like image 975
dwayne Avatar asked Dec 19 '11 15:12

dwayne


People also ask

How do you Precompile rails assets?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

What is assets Precompile?

rails assets:precompile is the task that does the compilation (concatenation, minification, and preprocessing). When the task is run, Rails first looks at the files in the config.assets.precompile array. By default, this array includes application.js and application.css .

What is Require_tree?

The require_tree directive tells Sprockets to recursively include all JavaScript files in the specified directory into the output. These paths must be specified relative to the manifest file.

What does rake assets Clean do?

rake assets:clean Only removes old assets (keeps the most recent 3 copies) from public/assets . Useful when doing rolling deploys that may still be serving old assets while the new ones are being compiled.


1 Answers

OK, I didn't realize this was how it works, but I think I figured it out.

Add the files to be manually loaded to config/environments/production.rb like so:

config.assets.precompile += %w( calendar/*.js jquery_calendar/*.css )

I thought this just folded them into the application.js and application.css, but apparently not -- it compiles them as individual files.

Then, you can call the files as you traditionally would (in this case, using nifty_layout):

javascript "calendar/date.js", "calendar/jquery.weekcalendar.js", "calendar/custom.js"
like image 127
dwayne Avatar answered Oct 29 '22 07:10

dwayne