是否可以在 html 文件的 script 标签中编译 Coffeescript 代码?
有没有一种简单的方法来编译里面的 Coffeescript html 中的 标签,或者您通常将所有 Coffeescript 放在单独的文件中吗?
Possible Duplicate:
Is there a way to send CoffeeScript to the client's browser and have it compiled to JavaScript there?
Is there a simple way to compile Coffeescript that is inside <script>
tags inside html, or do you usually have all Coffeescript in separate files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确实有。 这里有一篇关于它的帖子。
该文章的摘要如下:
text/coffeescript
类型的脚本在页面头部包含此行:
请注意,不鼓励这样做。
Indeed there is. There's a post about it here.
The summary of that article is this:
text/coffeescript
Include in the head of the page this line:
Beware that doing this isn't encouraged.
回应 dvcolgan 的澄清评论:
因此,您希望在服务器上有一个带有内联 CoffeeScript 的 HTML 文件,该文件通过内联 JavaScript 作为 HTML 提供。 CoffeeScript 编译器不直接支持此功能,但您可以使用
coffee-script
库和 jsdom 进行 HTML 解析。您想要如何实现这一点取决于您正在使用的 Web 框架。您可能不希望 CoffeeScript 编译器在每个请求上运行(它非常快,但它仍然会减少服务器每秒可以处理的请求数);相反,您需要编译一次 HTML,然后从缓存中提供编译后的版本。再说一次,我不知道有任何现有工具可以做到这一点,但编写自己的工具应该不会太难。
Responding to dvcolgan's clarifying comment:
So, you want to have an HTML file on the server with inline CoffeeScript that gets served as HTML with inline JavaScript. The CoffeeScript compiler doesn't support this directly, but you could write a Node script to do it fairly easily, using the
coffee-script
library and jsdom to do the HTML parsing.Exactly how you want to implement this would depend on the web framework you're using. You probably don't want the CoffeeScript compiler to run on every request (it's pretty fast, but it's still going to reduce the number of requests/second your server can handle); instead, you'd want to compile your HTML once and then serve the compiled version from a cache. Again, I don't know of any existing tools that do this, but it shouldn't be too hard to write your own.