有没有办法将 CoffeeScript 发送到客户端浏览器并*在那里*将其编译为 JavaScript?
有没有办法将 CoffeeScript 发送到客户端的浏览器并将其编译为 JavaScript??
<script type="text/coffeescript">
square = (x) -> x * x
list = [1, 2, 3, 4, 5]
squares = (square num for num in list)
</script>
CoffeeScript编译器是用JavaScript编写的,那么我可以将其发送给客户端以在客户端的浏览器中编译/运行此代码吗?
Is there a way to send CoffeeScript to the client's browser and have it compiled to JavaScript there?
<script type="text/coffeescript">
square = (x) -> x * x
list = [1, 2, 3, 4, 5]
squares = (square num for num in list)
</script>
The CoffeeScript compiler is written in JavaScript, so can I send it to the client to compile/run this code in the client's browser?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Jeremy 已经有了这个,但让我添加一些重要的细节和注意事项:
coffee-script.js
是一个大文件;因此,除非您实际上让用户运行他们自己的 CoffeeScript,否则您真的不应该在生产中使用它。squares
在脚本之外将不可见。相反,您需要将其更改为window.squares = ...
。coffee-script.js
在文档准备好之前不会读取您的XMLHTTPRequest
加载,这意味着它们必须托管在与您的网站相同的域中(某些浏览器(至少是 Chrome)也存在执行的问题。 XMLHTTPRequest 位于
file://
路径上。)请 如果您正在为 Ruby 或 Python 服务器进行开发,我已尝试在 http://github.com/jashkenas/coffee-script/wiki/Web-framework-plugins。
如果您正在开发没有后端的网站,我强烈建议您使用 Middleman 工具,它可以让您在开发过程中使用 CoffeeScript(如果需要,还可以使用 Haml 和 Sass),然后编译并缩小它以进行生产部署。
Jeremy already has this one, but let me add some important details and caveats:
coffee-script.js
is a big file; so unless you're actually letting your users run their own CoffeeScript, you really shouldn't use it in production.squares
wouldn't be visible outside of the script. Instead, you'd want to change it towindow.squares = ...
.coffee-script.js
doesn't read your<script type="text/coffeescript>
tags until after the document is ready, by which time your JavaScripts have already run.XMLHTTPRequest
, which means that they must be hosted on the same domain as your site. (Certain browsers—Chrome, at least—also have a problem with doingXMLHTTPRequest
s onfile://
paths.)So, you might want to look at some alternatives for serving CoffeeScript as compiled JavaScript instead. If you're developing for a Ruby or Python server, there are plugins available. I've tried to list them all at http://github.com/jashkenas/coffee-script/wiki/Web-framework-plugins.
If you're developing a site without a backend, a tool I highly recommend looking at is Middleman, which lets you work with CoffeeScript (as well as Haml and Sass, if you want) during development, then compile and minify it for production deployment.
也许您正在寻找这个?
Perhaps you're looking for this?
答案是肯定的。我不会重复@Trevor的出色答案,而只是提供您正在思考的示例:
http://forgivingworm.wordpress.com/2010/09/27/running-coffeescript-in-browser/
基本上你
编译器将按顺序评估并编译所有的 CoffeeScript)
下面的示例 HTML
The answer is yes. I won't repeat @Trevor's excellent answer, but rather just provide an example of what you're thinking about:
http://forgivingworm.wordpress.com/2010/09/27/running-coffeescript-in-browser/
Basically you
compiler will evaluate and compile all coffeescript in order)
Sample HTML below
src
,您必须能够通过XMLHTTPRequest
访问该文件,特别是在使用file://
的浏览器上会失败。src
you must be able to access the file viaXMLHTTPRequest
, in particular it fails on browsers withfile://
.