更新时间:2022-06-22 23:13:10
Jeremy已经有这个,但是让我添加一些重要的细节和注意事项:
Jeremy already has this one, but let me add some important details and caveats:
coffee-script.js
大文件因此,除非你实际上允许用户运行自己的CoffeeScript,否则你实际上不应该在生产环境中使用它。正方形
在脚本外部不可见。 $ 所有CoffeeScript代码,无论是外部还是内联,都会改为 window.squares = ...
,将在网页上的所有JavaScript代码之后运行。这是因为 coffee-script.js
不会读取您的< script type =text / coffeescript>
XMLHTTPRequest
加载的,这意味着它们(某些浏览器 - 至少在文件上执行 XMLHTTPRequest
时出现问题) //
路径。)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 to window.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 doing XMLHTTPRequest
s on file://
paths.)所以,你可能想看看一些替代服务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.