0

I have read and tried the Assets Pipeline guide here: http://guides.rubyonrails.org/asset_pipeline.html

... which shows how to include specific files in a manually created and updated list, --OR-- the Proc which includes a directory (or directories) but then excludes all the other files which Rails ordinarily includes.

I want to += my folder of files to the normally included files.

I have tried the answers:

Rails config.assets.precompile setting to process all CSS and JS files in app/assets

What is the purpose of config.assets.precompile?

rails config.assets.precompile for specific sub folder

... the last of which appears to show a solution:

config.assets.precompile += ["*external/calendars*"]

which I changed to:

    config.assets.precompile += %w["*javascript*"]

or config.assets.precompile += ["javascript"] (and about 20 other variations.)

... to get my assets/javascript folder. But the directory is not included, as evidenced by the error "...isn't precompiled."

The third method, is to give it

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

... which works, but leads to a very, very long compile, I would assume finding every JS file it can discover, anywhere.

Needless to say, adding files to a manually updated list is not suitable for an in-progress application - and losing whatever unknown things Rails precompiles with an exclusionary Proc won't cut it either (yet those are the only two examples in the docs).

Is there not a simple wildcard solution to "+-=" a folder - or perhaps to just turn this 'feature' off, specify my JS per view, and still have it work on Heroku?

----EDIT - It gets more irrational the deeper I look. Essentially, the solution is, "Load all the things Rails finds A-OK in Development Mode." And yet such an option does not exist?

The production.rb file, referring to the precompile line, says:

  # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)

... and application.js has:

//= require_tree .

... so that should load all the files under that directory - but it doesn't. Why? The deeper I dig, the less sense this makes.

Community
  • 1
  • 1
JosephK
  • 668
  • 10
  • 18

1 Answers1

1

A good practice when dealing with multiple CSS/JS files to add to the asset pipeline is to simply create a new manifest for those files:

Let's say you have some JS files under lib/assets/javascripts/external/calendars and you want to load them through the asset pipeline.

You want to create an index.js manifest file with the following content:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require_tree .

This way all JS files you add into the external/calendars directory will be included by default thanks to the require_tree . directive.

Now, in your app/assets/javascripts/application.js file add the following line:

//= require calendars

This should find your "calendars' manifest index file" and load all dependent JS files. No need to add anything into the asset pipeline, it will just work.

Pierre-Louis Gottfrois
  • 17,561
  • 8
  • 47
  • 71
  • I am only trying to include the files in app/assets/javascripts/ - no subdirectories at this point. I added the 'index.js' file into 'app/assets/javascripts/' folder (was that the correct location?), and put '//= require javascripts' into the 'app/assets/javascripts/application.js' file. I also commented out the 'config.assets.precompile' line in the 'production.rb' file. Now when I run 'bundle exec rake assets:precompile' I get the error "couldn't find file 'javascripts'". – JosephK Aug 16 '13 at 08:06
  • OK, I see that one MUST use a subdirectory to get this method to work. I created one, put my files there, and precompile created the /public/assets/blah files. Note that one's JS files are included within the minified application.js file there - not in their own js files, as they were using some other methods. Unfortunately, when I pushed to Heroku, the files are still not working - 'blah.js isn't precompiled' error - to spite no errors when the slug precompiled in x seconds. – JosephK Aug 16 '13 at 08:31
  • Added back the config.assets.precompile line in production.rb as+= %w( index.js ) – JosephK Aug 16 '13 at 08:38
  • Same error ... Ahh, because the file is referenced in the view, and now it doesn't exist as its own file anymore, but is mixed into application.js - sigh. Time to rework all my views to take out any references to my independent js files. Did that for a couple to test and they work now. Thanks for a pointer in the right direction Pierre. – JosephK Aug 16 '13 at 08:53
  • You welcome :) Glad it helped you. Working with the asset pipeline is not that easy :( – Pierre-Louis Gottfrois Aug 16 '13 at 08:55