1

We have a conf/environments/production.rb file that lists all the js files individually, e.g.

conf.assets.precompile += %w (highcharts.js)
conf.assets.precompile += %w (schedule.js)
conf.assets.precompile += %w (new_event.js)
conf.assets.precompile += %w (old_event.js)
conf.assets.precompile += %w (event_controls.js)
conf.assets.precompile += %w (other_stuff.js)
...

Every time I have a new js file I have to manually add an entry here.

How can I include them all without having to manually maintain this file?

Charles
  • 50,943
  • 13
  • 104
  • 142
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
  • 1
    This has been answered before: http://stackoverflow.com/questions/7278029/rails-3-1-asset-precompilation - though I agree that your title is much more descriptive :) – emrass Sep 17 '12 at 16:05

1 Answers1

1

You are technically approaching the problem from the wrong direction. Your config.assets.precompile should reference only a few central manifest type files (for example, application.js), and the manifest files then reference the js files as necessary.

For example, your application.js might look like:

//= require highcharts.js
//= require schedule.js
//= require new_event.js
//= require old_event.js
//= require event_controls.js
//= require other_stuff.js

Set up like this, all the listed files will be included in the precompiled version of application.js, and application.js is the only file you will need to include in your layout.

To have new files added automatically, you can use instead

//= require_tree .

which will include every .js file in the same directory as application.js, plus those in all the child directories.

In practice, one application.js file that includes every single js file in the application is a bit much. You can break your files down into a collection of central manifests. For example, charts.js, events.js, misc.js. Add these files to your config.assets.precompile. Then when a new file is required, update the manifest files and not the production.rb file.

Dave Isaacs
  • 4,499
  • 6
  • 31
  • 44
  • It is not necessary wrong to add js files manually. Since you do not always want to have alls js files compiled into one. It might be needed to load js files asynchronously. – dc10 Feb 27 '13 at 22:36