3

I'm wondering if I can use CoffeeScript to include other standard JS files (as a simple way to do some combining of files).

I have a client-side minification tool I'm using (an app called Live Reload) which is working just fine.

<!-- Some jQuery plugins I'm using. -->
<script src="/js/libs/some-plugin.js"></script>
<script src="/js/libs/another-plugin.js"></script>

<!-- The output of my /js/script.coffee file: -->
<script src="/js/script.js"></script> 

What I'd like to do, is just combine those plugins into output of my coffeescript file. I've looked high and low and I've only seen articles on server methods for this as well as a lot of articles on things like requirejs.org. I'm not trying to do anything that complex- I just want to get rid of a couple round trips for js files I know I'm never going to touch.

Does CoffeeScript have an "include" function to speak of?

Jeremy Ricketts
  • 2,601
  • 4
  • 29
  • 28
  • possible duplicate of [Is there a way to include file in coffee script?](http://stackoverflow.com/questions/7718121/is-there-a-way-to-include-file-in-coffee-script) – Matt Ball Mar 12 '12 at 01:10
  • From coffeescript-concat: "coffeescript concat require a node.js installation, CoffeeScript, and underscore.js." As I'm already outputting coffeescript to js, I'm not in need of another external tool to combine .coffee files. I'm trying to include/combine other .js files INTO a .coffee file using CoffeeScript itself. – Jeremy Ricketts Mar 12 '12 at 01:17
  • CoffeeScript is not a superset of JavaScript, so if you just `#include` (you get the idea) JS into a `.coffee` file, that won't be syntactically valid CoffeeScript. You'd have to first turn the JS into CoffeeScript, then include it... then turn it all back into JS. I don't really see the point. – Matt Ball Mar 12 '12 at 01:19

2 Answers2

1

There are ways you can achieve this by creating a more complex Cakefile, in which you will read the contents of js-files and append them with CS compiler output than write it into the single target js file. You can even create a fake global require function which will mimic its behaviour in the bundled file.

If you were looking for a standard tool or at least an approach to that problem, unfortunately, since CS is very young, there's none yet. There are some attempts though: https://github.com/jashkenas/coffeescript/wiki/%5BIntegrations%5D-Build-Tools.

I'm currently working on such a tool myself and am planning to publish it within a month. I'll post back then.

Саша Черных
  • 2,561
  • 4
  • 25
  • 71
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
  • hi Nikita, I'm looking for such a tool as you described, do you have any progress on that? – teleme.io Jan 04 '14 at 05:20
  • I don't remember whether it approaches the described issues, since I've been out of coffeescripting for quite some time, but it might be close :https://github.com/nikita-volkov/Robusta – Nikita Volkov Jan 04 '14 at 11:29
0

Basically, the answer seems to be no. This is not something CoffeeScript is capable of.

Jeremy Ricketts
  • 2,601
  • 4
  • 29
  • 28