是否可以在 html 文件的 script 标签中编译 Coffeescript 代码?

发布于 2024-12-11 06:26:21 字数 377 浏览 0 评论 0原文

可能的重复:
有吗一种将 CoffeeScript 发送到客户端浏览器并将其编译为 JavaScript 的方法?

有没有一种简单的方法来编译里面的 Coffeescript html 中的

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

注定孤独终老 2024-12-18 06:26:21

确实有。 这里有一篇关于它的帖子

该文章的摘要如下:

  1. 在您的 HTML 文档中包含 text/coffeescript 类型的脚本
  2. 在页面头部包含此行:

请注意,不鼓励这样做。

<html>

<head>
  <title>In-Browser test</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript">
  </script>
  <script src="https://raw.githubusercontent.com/jashkenas/coffeescript/master/lib/coffee-script/coffee-script.js" type="text/javascript">
  </script>

</head>

<body>
  <script type="text/coffeescript">
    $ -> $('#header').css 'background-color', 'green'
  </script>
  <h1 style="color:white" id="header">CoffeeScript is alive!</h1>


</body>

</html>

Indeed there is. There's a post about it here.

The summary of that article is this:

  1. Include in your HTML document your script with type text/coffeescript
  2. Include in the head of the page this line:

    <script type="text/javascript" src="https://raw.githubusercontent.com/jashkenas/coffeescript/master/lib/coffee-script/coffee-script.js"></script>
    

Beware that doing this isn't encouraged.

<html>

<head>
  <title>In-Browser test</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript">
  </script>
  <script src="https://raw.githubusercontent.com/jashkenas/coffeescript/master/lib/coffee-script/coffee-script.js" type="text/javascript">
  </script>

</head>

<body>
  <script type="text/coffeescript">
    $ -> $('#header').css 'background-color', 'green'
  </script>
  <h1 style="color:white" id="header">CoffeeScript is alive!</h1>


</body>

</html>

丶视觉 2024-12-18 06:26:21

回应 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文