如何从 .NET 编译 CoffeeScript?
我想编写一个 HttpHandler 来即时编译 CoffeeScript 代码并发送生成的 JavaScript 代码。我尝试过 MS [JScript][1] 和 IronJS,但没有成功。我不想使用 [Rhino][2],因为 Java 依赖性会使分发变得太困难。
如何从 .NET 编译 CoffeeScript?
I want to write an HttpHandler that compiles CoffeeScript code on-the-fly and sends the resulting JavaScript code. I have tried MS [JScript][1] and IronJS without success. I don't want to use [Rhino][2] because the Java dependency would make it too difficult to distribute.
How can CoffeeScript be compiled from .NET?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
CoffeeScript-dotnet
用于编译 CoffeeScript 的命令行工具。包括一个文件系统观察器,用于在 CoffeeScript 发生更改时自动重新编译它们。大致相当于linux/mac的coffee-script节点包。
CoffeeSharp
包括类似于 CoffeeScript-dotnet 的命令行工具以及当来自 asp.net 站点的请求时编译 CoffeeScripts 的 http 处理程序。
SassAndCoffeeScript
用于 Asp.net mvc 的库,可根据请求编译 sass 和 Coffeescript 文件。还支持缩小和组合。
手动编译使用IronJS
IronJS是一个.NET javascript解释器,可以成功加载CoffeeScript编译器并编译CoffeeScript。
使用 Node.js 手动编译
获取节点二进制文件并添加bin 目录到您的路径。编写 node.js 脚本来加载 CoffeeScript 编译器和 CoffeeScript 文件并保存编译后的javascript。
CoffeeScript-dotnet
Command line tool for compiling CoffeeScript. Includes a file system watcher to automatically recompile CoffeeScripts when they change. Roughly equivalent to the coffee-script node package for linux / mac.
CoffeeSharp
Includes a command line tool similar to CoffeeScript-dotnet as well as a http handler that compiles CoffeeScripts when requested from an asp.net site.
SassAndCoffeeScript
Library for Asp.net mvc that compiles sass and coffeescript files on request. Also supports minification and combination.
Manually Compile With IronJS
IronJS is a .NET javascript interpreter that can successfully load the CoffeeScript compiler and compile CoffeeScript.
Manually Compile With Node.js
Get the node binaries and add the bin directory to your path. Write a node.js script to load the CoffeeScript compiler and your CoffeeScript files and save the compiled javascript.
CoffeeScript 现在完全受 Chirpy 支持:
http://chirpy.codeplex.com/
CoffeeScript is now fully supported by Chirpy:
http://chirpy.codeplex.com/
您明确表示您想编写一个运行时编译器,因此这可能不完全是您正在寻找的,但如果主要目的是有一种方法来生成 javascript 结果,则 Mindscape Web Workbench 很有趣。它是 Visual Studio.NET 2010 的免费扩展,可在扩展管理器中使用。它提供智能感知、语法突出显示并在您编写时编译为 JS。我刚刚开始使用它,但看起来很有希望。 Scott Hanselman 在此处对此进行了讨论。它还支持 LESS 和 Sass。
You specifically said that you wanted to write a runtime compiler, so this may not be exactly what you are looking for, but if the main point is to have a way to generate the javascript result, the Mindscape Web Workbench is interesting. It is a free extension for Visual Studio.NET 2010 and available in the Extension Manager. It gives Intellisense, syntax highlighting and compiles to JS as you write. I am just getting started using it but looks promising. Scott Hanselman talks about it here. It also supports LESS and Sass.
我已经成功地使用 IKVM、jcoffeescript 和 Rhino 从 .NET 编译 CoffeeScript。这很简单,只是不带参数的 JCoffeeScriptCompiler 构造函数重载不起作用。它以 java.util.Collections.EMPTY_LIST 作为参数运行正常。
我就是这样做的:
下一步是创建构建任务和/或 HTTP 处理程序。
I've managed to compile CoffeeScript from .NET using IKVM, jcoffeescript and Rhino. It was straightforward, except that the JCoffeeScriptCompiler constructor overload without parameters didn't work. It ran OK with a java.util.Collections.EMPTY_LIST as parameter.
This is how I did it:
The next step would be to create a build task and/or an HTTP handler.
查看新的 coffeescript-dotnet 项目,该项目使用 Jurassic JavaScript 实现。
Check out the new coffeescript-dotnet project, which uses the Jurassic JavaScript implementation.
由于 CoffeeScript 编译器现在在 Internet Explorer 上运行,经过最近的几次调整,它也应该可以很好地运行在其他 MS 风格的 JavaScript 中。尝试包含最新版本中的
extras/coffee-script.js
,并且您应该可以很好地使用CoffeeScript.compile(code)
。Since the CoffeeScript compiler now runs on Internet Explorer, after a couple of recent tweaks, it should be good to go within other MS-flavors of JavaScript as well. Try including
extras/coffee-script.js
from the latest version, and you should be good to go withCoffeeScript.compile(code)
.我尝试通过基于 Windows 的脚本主机(或只是 wscript)运行捆绑的
extras/coffee-script.js
,但它没有报告任何问题。 添加了这一行:然后我在文件末尾
并再次通过
wscript
运行它,它正确打印了生成的 JavaScript。您使用 COM 对象吗?您能否分享更多负责初始化 MScript 对象引用的代码?I tried running the bundled
extras/coffee-script.js
through Windows Based Script Host (or just wscript) and it didn't report any issues. I then added this line:at the end of the file and run it through
wscript
again and it printed the resulting JavaScript correctly.Are you using COM objects? Can you share some more of the code responsible for initialising the MScript object reference?
Visual Studio 2010 中的 CoffeeScript
这是 Chirpy 的 fork(chirpy 是一个用于混合、压缩和验证 javascript、样式表和无点文件的工具)
“好吧,我想我在我的 fork 上得到了它,主要基于其他人的工作。检查一下出来:
http://chirpy.codeplex.com/SourceControl/network/Forks/Domenic/CoffeeScriptFixes ”
来自 http://chirpy.codeplex.com/workitem/48
CoffeeScript in Visual Studio 2010
It's Chirpy's fork (chirpy is a tool for mashing, minifing, and validating javascript, stylesheet, and dotless files)
"OK, I think I got it working on my fork, based mostly on other peoples' work. Check it out:
http://chirpy.codeplex.com/SourceControl/network/Forks/Domenic/CoffeeScriptFixes"
from http://chirpy.codeplex.com/workitem/48
我没有直接的答案(我希望你找到一个),但也许看看下面的内容,看看如何完成。
I don't have a direct answer, (I hope you find one), but maybe take a look at the following to see how it might be done.
IronJS 现在支持 CoffeeScript,并且通常比其他 .NET JS 引擎更快:
我在这里有一篇关于将两者连接在一起的博客文章:
http://otac0n .com/blog/2011/06/29/CoffeeDemo-A-Simple-Demo-Of-IronJS-Using-CoffeeScript.aspx
IronJS now supports CoffeeScript and is generally faster than the other .NET JS engines:
I have a blog post about wiring the two together here:
http://otac0n.com/blog/2011/06/29/CoffeeDemo-A-Simple-Demo-Of-IronJS-Using-CoffeeScript.aspx
我的主要编辑器是 VS 2010,我喜欢 WorkBench 扩展。每次你在 .coffee 文件上点击“保存”时,它都会自动编译为 js,这很好,还向你介绍了我读过但从未接触过的 SASS。
他们提供了一个付费版本,它也会自动缩小/缩小您的 js 和 css 文件,因为您的 .coffee 和 .scss 无论如何都是您的源文件。
我鼓励所有 VS 用户继续安装它,特别是如果您运行 VS 2010。
唯一的问题是,使用 .coffee 语法时,它不会像 html、js、c# 那样突出显示,请有人纠正我或启发我。代码是。这可能是因为我使用 http://studiostyl.es/ 中的配色方案并记录 http://studiostyl.es/schemes/coffee- 只是共享名称 Coffee 没有对 Coffeescript 的特殊语法突出显示支持我知道。但今天没有理由不开始使用工作台插件!
好吧,工作台网站声称:语法突出显示,所以也许这是我选择的 studiostyle.es。
My main editor is VS 2010 and I love the WorkBench extension. it's nice it auto compiles to js everytime you hit save on your .coffee file, also introduces you to SASS which I had read about but never got around.
They offer a pay version to that will autmaically shrink/minify your js and css files as well, since your.coffee and .scss are your source files anyway.
I'd encourage all VS users to go ahead and install this especially if you run VS 2010.
The only knock, and someone please correct me or enlighten me, is that with .coffee syntax it's not highlighted the way say html, js, c# code is. it might be because I am using a color scheme from http://studiostyl.es/ and for the record http://studiostyl.es/schemes/coffee- just shares the name coffee no special syntax highlight support for coffeescript that I am aware of. but no reason not to start using the workbench addin today!
Okay workbench website claims: syntax highlighting so again maybe it's the studiostyle.es i chose.
我知道这已经很旧了,但我来这里是为了回答一个非常相似的问题:如何使用 Visual Studio 2012 Express 编译我的 CoffeeScript?请注意,免费的 Express 版本不允许任何扩展,因此我无法继续使用 Mindscape Workbench
事实证明这很容易。只需使用 NuGet 安装 Jurassic-Coffee 包即可。
与 Mindscape Workbench 相比,使用此包的优点之一是您可以直接从 html 中的脚本标签引用您的咖啡。它会缩小并缓存已编译的 JS,因此您只有在请求的咖啡文件发生更改时才可以工作。
Mindscape 工作台允许您将不同的咖啡脚本文件捆绑在一起,这对于模块化您的咖啡脚本非常方便。您还可以使用 Jurassic Coffee 来执行此操作,方法是利用
#= require
语句来包含其他咖啡模块文件,例如:我认为咖啡中有
#= require
语句文件实际上比 Mindscape Workbench 方法更干净、更清晰,后者将所有这些隐藏在其界面后面,因此您很容易忘记自己拥有哪些依赖项。注意
有一个潜在的问题。 Nuget 安装程序会将 httphandler 条目放入 web.config 中,该条目可能与 IIS Express 集成托管管道模式不兼容。
因此,您可能会看到以下错误:
要解决此问题,只需删除如下所示的处理程序。
I know this is old but I came here to answer a very similar question: How do I get my CoffeeScript to compile using Visual Studio 2012 Express? Note that the free Express version does not allow any extensions so I could not continue to use the Mindscape Workbench extension that had served me well for quite some time.
It turns out to be very easy. Just use NuGet to install the Jurassic-Coffee package and off you go.
One advantage of using this package over mindscape workbench is that you can reference your coffee directly from the script tags in the html. It minifies and caches the compiled JS so you only do work if the requested coffee file has changed.
The mindscape workbench allows you to bundle together different coffescript files which is very handy for modularising your coffeescript. You can also do this using Jurassic Coffee by utilising the
#= require
statement to include other coffee module files, for example:I think having the
#= require
staement in the coffee file is actually cleaner and clearer than the mindscape workbench approach, which kind of hides all this behind their interface so you forget easily what dependencies you have.Note
There is one potential gotcha. The Nuget installer will put in an httphandler entry into your web.config that may not be compatible with IIS Express integrated managed pipeline mode.
You might therefore see the following error:
To fix this just remove the handler shown below.
您只需将其移植到 C# 即可。我已将 Jison 移植到 C#(这是使 CoffeeScript 运行的底层项目)。我认为这可能有点不同,但两个 Jison 解析器的工作原理是相同的。
我还没有将请求拉回 Jison 的主要架构,但很快就会这样做。
https://github.com/robertleeplummerjr
You could simply write a port of it to C#. I have ported Jison to C# (which is the underlying project that makes CoffeeScript run). I would think it may be a bit different, but both Jison parsers work the same.
I have not pull requested it back yet to Jison's main architecture, but will be doing so soon.
https://github.com/robertleeplummerjr
您可以使用 Node.js,而不是使用 CScript(这里是独立的 Windows 二进制文件)
Instead of shelling out to CScript you could shell out to Node.js (here are self-contained Windows binaries)
我尝试将 extras/
coffee-script.js
文件(未修改)编译为jsc
,即 JScript.NET .NET 编译器,我遇到了很多错误。以下是值得注意的:其他错误是由上述错误引起的。
I've tried to compile the extras/
coffee-script.js
file, unmodified, tojsc
, the JScript.NET compiler for .NET, and I got many errors. Here are the noteworthy ones:Other errors were caused by the above said errors.
您可能还想查看 jurassic-coffee,它也是一个咖啡脚本编译器,运行 Jurassic 中的原始编译器。
它具有链轮样式“#= require file.coffee”或“#= require
file.js
”,可用于在编译之前保持 .coffee 文件模块化和组合以及嵌入 .js 文件。它配备了一个带有 .js 和 .coffee 文件文件观察器的 HttpHandler,用于跟踪哪些 .coffee 文件需要重新编译,并传递给已编译的 *.js 文件以供其余文件使用。
jurassic-coffee 也可以作为 Nuget 包使用
https://github.com/creamdog/JurassicCoffee
You might also want to check out jurassic-coffee, it is also a coffee-script compiler running the original compiler in jurassic.
It features sprocket style "#= require file.coffee" or "#= require
file.js
" wich can be used to keep .coffee files modular and combined right before compilation as well as embedding .js files.It sports a HttpHandler with file watchers for .js and .coffee files that keeps track of what .coffee files needs to be re-compiled and pass through to the compiled *.js files for the rest.
jurassic-coffee is also available as a Nuget package
https://github.com/creamdog/JurassicCoffee
我已经完成了一个在幕后使用 Windows Script Host 的 HttpHandler: https://github.com/duncansmart/LessCoffee< /a> 并且效果很好(它还编译 *.less 文件)。
它位于 NuGet 上: http://nuget.org/List/Packages/LessCoffee
它基于此简单包装: https://github.com/duncansmart/coffeescript-windows
I've done an HttpHandler that uses Windows Script Host behind the scenes: https://github.com/duncansmart/LessCoffee and works great (it also compiles *.less files).
It's on NuGet: http://nuget.org/List/Packages/LessCoffee
It's based on this simple wrapper: https://github.com/duncansmart/coffeescript-windows
我使用 v8 编写了一个交互式 shell。
https://github.com/mattn/coffee-script-v8
这作为单一工作可执行文件。 (不要使用外部文件)
它不能使用 require()。但对于学习coffeescript来说已经足够了。
I wrote an inteructive shell using v8.
https://github.com/mattn/coffee-script-v8
This work as single executable file. (Don't use external files)
It can't use require(). But enough to learn coffeescript.