dwayne
dwayne

Reputation: 171

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!

Upvotes: 8

Views: 4105

Answers (1)

dwayne
dwayne

Reputation: 171

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"

Upvotes: 4

Related Questions