2

I have a coffee script user.js.coffee, that is only used in certain views. I achieved this by using the following answer:

https://stackoverflow.com/a/6795533/784318

Now I have excluded the script from the application.js. I also removed the //= require_tree . entry.

So my file is available here: http://localhost:3000/assets/user.js however, when I deploy this to the server the assets will be combined in one application.js so how can I make sure that the user.js will be available on production like so: http://myserver.com/assets/user.js?

Community
  • 1
  • 1
Besi
  • 22,579
  • 24
  • 131
  • 223

2 Answers2

2

In environments/production.rb (or the environment you need precompile to occur) uncomment or add this file to the precompile array:

# environments/production.rb

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

Other entries might be already present, just add any other file that you need to access separately.

This file will not get compiled in one big application.js file and will be accessible separately as user.js

Oktav
  • 2,143
  • 2
  • 20
  • 33
  • This looks promising. I will test that out on production. Do you know if I have to use `user.js.coffee` in my case? – Besi Apr 30 '13 at 13:11
  • `user.js` should work fine. At least it works in my case. It should pickup the `.coffee` by itself – Oktav Apr 30 '13 at 13:24
  • @Oktav but the compiled file will have the name with fingerprint like ```user-e55bd5e3eb82fa4e8034af2883dc37e8.js``` so we can't definitely know the path to it. – freemanoid Oct 31 '13 at 10:09
  • @freemanoid You need to include that file via `javascript_include_tag "user"` in your view and it should include the file with the correct fingerprint. Also the fingerprint will change only if the content of the js file changes. – Oktav Nov 04 '13 at 09:20
  • @Oktav but what if I need to post the link to this file on 3rd party resource? I need to know the link to do that. – freemanoid Nov 04 '13 at 12:47
  • @freemanoid If you need to use the same js file for other applications then I wouldn't use the asset pipeline for that file. Just write it as plain js, put it in public/javascripts/ and include it statically. Something like `javascript_include_tag "javascripts/user.js"` – Oktav Nov 04 '13 at 13:03
  • @Oktav but I still want to use coffee for it – freemanoid Nov 04 '13 at 15:43
0

You can try putting your user.js file to the public folder of your application directly and configure your asset pipeline to exclude it from the "zipping" process.

Lazarus Lazaridis
  • 5,803
  • 2
  • 21
  • 35
  • Well it is not a `.js` file but a `.js.coffee` file. So it will at least have to processed by the coffee-script compiler. – Besi Apr 30 '13 at 11:18
  • Check this http://scottwb.com/blog/2012/06/30/compile-a-single-coffeescript-file-from-your-rails-project – Lazarus Lazaridis Apr 30 '13 at 11:21