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条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-30 05:13

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:

WScript.Echo(CoffeeScript.compile('a: 1'));

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?

查看更多
混吃等死
3楼-- · 2019-01-30 05:14

Instead of shelling out to CScript you could shell out to Node.js (here are self-contained Windows binaries)

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

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.

查看更多
ら.Afraid
5楼-- · 2019-01-30 05:17

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.

查看更多
Summer. ? 凉城
6楼-- · 2019-01-30 05:18

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.

查看更多
【Aperson】
7楼-- · 2019-01-30 05:19

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.

<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="home.coffee"></script>
</head>

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:

#= require Classes\GridWrapper.coffee
class UsersGrid
  constructor:->
     @grid = new GridWrapper()

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:

An ASP.NET setting has been detected that does not apply in Integrated managed pipeline mode.

To fix this just remove the handler shown below.

<system.web>
  //other stuff

  <httpHandlers>
    <add type="JurassicCoffee.Web.JurassicCoffeeHttpHandler,JurassicCoffee" validate="false" path="*.coffee" verb="*" />
  </httpHandlers>

</system.web>
查看更多
登录 后发表回答