How can I compile CoffeeScript from .NET?

后端 未结 18 1805
后悔当初
后悔当初 2020-12-23 01:54

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

相关标签:
18条回答
  • 2020-12-23 02:32

    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

    0 讨论(0)
  • 2020-12-23 02:34

    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 with CoffeeScript.compile(code).

    0 讨论(0)
  • 2020-12-23 02:35

    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.

    0 讨论(0)
  • 2020-12-23 02:36

    CoffeeScript is now fully supported by Chirpy: http://chirpy.codeplex.com/

    0 讨论(0)
  • 2020-12-23 02:36

    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?

    0 讨论(0)
  • 2020-12-23 02:36

    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>
    
    0 讨论(0)
提交回复
热议问题