有没有办法发送CoffeeScript到客户端的浏览器,并将它编译为JavaScript * there *? [英] Is there a way to send CoffeeScript to the client's browser and have it compiled to JavaScript *there*?

查看:101
本文介绍了有没有办法发送CoffeeScript到客户端的浏览器,并将它编译为JavaScript * there *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有方法可以将CoffeeScript传送到客户的浏览器并将其编译成

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>

CoffeeScript编译器是用JavaScript编写的,因此我可以将其发送到客户端来编译/运行代码

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?

推荐答案

Jeremy已经有这个,但是让我添加一些重要的细节和注意事项:

Jeremy already has this one, but let me add some important details and caveats:


  1. 在39k gzipped(比较jQuery在29k), coffee-script.js 大文件因此,除非你实际上允许用户运行自己的CoffeeScript,否则你实际上不应该在生产环境中使用它。

  2. 如文档中所述,每个CoffeeScript代码段都有自己的匿名关闭。因此,您的示例代码段不会做任何事情 - 正方形在脚本外部不可见。 $ 所有CoffeeScript代码,无论是外部还是内联,都会改为 window.squares = ... ,将在网页上的所有JavaScript代码之后运行。这是因为 coffee-script.js 不会读取您的< script type =text / coffeescript>

  3. 远程CoffeeScript是通过 XMLHTTPRequest 加载的,这意味着它们(某些浏览器 - 至少在文件上执行 XMLHTTPRequest 时出现问题) // 路径。)

  4. 目前,不能保证不同的远程CoffeeScript运行的顺序,我提交了一个补丁,但它不是官方的一部分的CoffeeScript。请参见此拉取请求

  1. At 39k gzipped (compare to jQuery at 29k), 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.
  2. As mentioned in the documentation, each CoffeeScript snippet will be in its own anonymous closure. So your example snippet wouldn't do anything—squares wouldn't be visible outside of the script. Instead, you'd want to change it to window.squares = ....
  3. All CoffeeScript code, whether external or inline, will run after all JavaScript code on the page. That's because 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.
  4. Remote CoffeeScripts are loaded via 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 doing XMLHTTPRequests on file:// paths.)
  5. Currently, the order in which different remote CoffeeScripts run is not guaranteed. I submitted a patch for this, but it's not officially a part of CoffeeScript yet. See this pull request.

所以,你可能想看看一些替代服务CoffeeScript作为编译的JavaScript。如果你正在开发一个Ruby或Python服务器,有插件可用,我试图请在 http://github.com/jashkenas/coffee-script/wiki/Web-框架插件

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.

如果您开发的网站没有后端,我强烈建议您查看的工具是中间人,它允许您在开发过程中使用CoffeeScript(以及Haml和Sass,如果需要),然后为生产部署进行编译和缩小。

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.

这篇关于有没有办法发送CoffeeScript到客户端的浏览器,并将它编译为JavaScript * there *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆