Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1 strategy for pre-compiling controller specific JS assets

Tags:

In order to keep controller specific JavaScript logic out of the standard application.js and only have it included by the relevant controller, I'm putting it in its own .js file and including it based on the controller name from the layout like such:

<%= javascript_include_tag "application", params[:controller] %>

That works just fine, but when I deploy the app to production (I'm using Capistrano and have a pre-compile task set up), the asset pipeline doesn't precompile any of the controller specific JS files. I presume this is because my actual JavaScript file isn't referenced by require directives in application.js.

How do I deal with this without moving my controller specific JS back to application.js, or explicitly referencing it from application.js?

Is there some way to tell the asset pipeline to pre-compile an additional list files? How could I manually pre-compile a specific file on production?

Update

As it turns out, you can specify individual files here in your config/environments/production.rb:

config.assets.precompile += %w( achievements.js )

...or I just went ahead and capriciously added it for every JavaScript file:

config.assets.precompile += %w( *.js )
like image 880
Joost Schuur Avatar asked Sep 15 '11 04:09

Joost Schuur


People also ask

How do you Precompile an asset?

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.

Where should you put images JavaScript and CSS so that they get processed by the asset pipeline?

In previous versions of Rails, all assets were located in subdirectories of public such as images , javascripts and stylesheets . With the asset pipeline, the preferred location for these assets is now the app/assets directory.

What does rake assets Clean do?

The clean it removes the old versions of the precompiled assets while leaving the new assets in place. Show activity on this post. rake assets:clean removes compiled assets. It is run by cap deploy:assets:clean to remove compiled assets, generally from a remote server.

What are sprockets Rails?

Sprockets is a Ruby library for compiling and serving web assets. Sprockets allows to organize an application's JavaScript files into smaller more manageable chunks that can be distributed over a number of directories and files.


2 Answers

If you want to precompile the js|css only found in the root of assets/javascripts and assets/stylesheets directories (and not their tree hierarchy), you can put this in environment files :

  Dir.chdir "#{Rails.root}/app/assets/javascripts"
  a = Dir.glob("*.{js,coffee,erb}")
  Dir.chdir "#{Rails.root}/app/assets/stylesheets"
  b = Dir.glob("*.{css,erb}")
  config.assets.precompile +=  a.concat(b)
  Dir.chdir Rails.root
like image 183
Alain Beauvois Avatar answered Oct 30 '22 19:10

Alain Beauvois


I think you and james_schorr are not really talking about the same thing. You need to add the files other than application.js to config.assets.precompile. His answer was more about the directory structure you could/should adopt, if I'm not mistaken.

If I wanted to have controller specific, I would do:

/assets
    /javascripts
        /users
            login.js
            profile.js
        /blogs
        /posts
        users.js
        blogs.js
        posts.js

And for instance, users.js would be:

*= require_tree ./users

That way, you can stay organized (have a lot of js files per controller), but in prod, they will all be included in one file.

Still need that in your config:

config.assets.precompile += %w( *.js )
like image 41
Robin Avatar answered Oct 30 '22 19:10

Robin