How can I compile CoffeeScript from .NET?

2019-01-30 04:53发布

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?

18条回答
Explosion°爆炸
2楼-- · 2019-01-30 05:30

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

查看更多
三岁会撩人
3楼-- · 2019-01-30 05:30

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

查看更多
何必那么认真
4楼-- · 2019-01-30 05:35

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.

查看更多
唯我独甜
5楼-- · 2019-01-30 05:36

Check out the new coffeescript-dotnet project, which uses the Jurassic JavaScript implementation.

查看更多
霸刀☆藐视天下
6楼-- · 2019-01-30 05:37

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

查看更多
不美不萌又怎样
7楼-- · 2019-01-30 05:38

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:

  1. Download IKVM, jcoffeescript and Rhino.
  2. Run ikvmc against js.jar, creating js.dll.
  3. Run ikvmc against the jcoffeescript jar.
  4. Add a reference to the jcoffeescript dll in Visual Studio. More references may be needed, but you will be warned about those.
  5. Run new org.jcoffeescript.JCoffeeScriptCompiler(java.util.Collections.EMPTY_LIST).compile() in your code.

The next step would be to create a build task and/or an HTTP handler.

查看更多
登录 后发表回答