开发者

How do I get TextMate to see "require" expressions in CoffeeScript when compiling? [closed]

开发者 https://www.devze.com 2023-03-21 02:57 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its curre开发者_如何学运维nt for
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its curre开发者_如何学运维nt form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

When writing CoffeeScript in TextMate and wanting to use a 3rd party Javascript library like jQuery or Raphael there is a need to add a "require" like so:

$ = require 'jquery'

This works fine when using coffee -c myfile.coffee from the commandline, but fails in the TextMate bundle when trying to compile with this error:

Error: Cannot find module 'jquery'
at Function._resolveFilename (module.js:317:11)
at Function._load (module.js:262:25)
at require (module.js:346:19)
at Object. (.:3:7)
at Object. (.:4:4)
at Module._compile (module.js:402:26)
at Object.run (/usr/lib/node_modules/coffee-script/lib/coffee-script.js:62:19)
at /usr/lib/node_modules/coffee-script/lib/command.js:120:29
at Socket. (/usr/lib/node_modules/coffee-script/lib/command.js:154:14)
at Socket.emit (events.js:61:17)

In this example, the code is all in the same directory at the root of the project.

The same occurs when specifying:

$ = require 'jquery.js'

How are other people compiling in CoffeeScript with the TextMate bundle? This would seem to be a showstopper to me for anything but the most trivial code. Apart from syntax highlighting, surely this is one of the most important parts of this bundle?


You've hit the "Run" command (⌘R), equivalent to coffee myfile.coffee.

You want "Compile and Display JS" (⌘B), equivalent to coffee -c --print --bare myfile.coffee. This should open up a window with the compiled output

var $;
$ = require('jquery');

As to require-ing libraries like jQuery and Raphael, I hope you understand that require is just a runtime function for loading modules; it has no compile-time meaning. You may be coming from a language like C++ or Java where there's a "linker." In CoffeeScript, on the other hand, code is compiled to JavaScript one file at a time, and JavaScript files are loaded individually into the runtime environment (be it a browser or a framework like Node.js).

0

精彩评论

暂无评论...
验证码 换一张
取 消