How can I compile CoffeeScript from .NET? - javascript

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?

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 is now fully supported by Chirpy:
http://chirpy.codeplex.com/

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.

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:
Download IKVM, jcoffeescript and Rhino.
Run ikvmc against js.jar, creating js.dll.
Run ikvmc against the jcoffeescript jar.
Add a reference to the jcoffeescript dll in Visual Studio. More references may be needed, but you will be warned about those.
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.

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

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).

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?

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.
Jint - JavaScript interpreter for .NET
Using IKVM to compile Rhino would get rid of the Java runtime requirement.
jcoffeescript. I haven't looked at jcoffeescript, but I think it depends on JRuby and Rhino. You could possibly IKVM.NET this as well.

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

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.

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>

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

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

I've tried to compile the extras/coffee-script.js file, unmodified, to jsc, the JScript.NET compiler for .NET, and I got many errors. Here are the noteworthy ones:
'require' is a new reserved word and should not be used as an identifier
'ensure' is a new reserved word and should not be used as an identifier
Objects of type 'Global Object' do not have such a member
Other errors were caused by the above said errors.

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

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

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.

Related

Why JavaScript frontend framework is needed for Node.js to run?

When we talk about JavaScript vanilla it's frontend programming language; It needs a webserver like IIS, Apache or nginx etc to deliver the content to a client when requested. After that, JavaScript runs on client browser, but every video or article I found said we need to install node.js to make this work. What I know about node.js is its a runtime environment to make JavaScript work outside the browser; like for a backend api or regular desktop application.
Here is my question:
Why do we need to use Node.js if our target is to deploy a frontend webapp that's gonna run on the client browser?
You don't have to install and use Node to make frontend applications, but it can help a lot, especially in large projects. The main reason it's used is so that script-writers can easily install, use, and update external packages via NPM. For a few examples:
Webpack, to consolidate multiple script files into a single one for production (and to minify, if desired)
Babel, to automatically transpile scripts written in modern syntax down to ES6 or ES5
A linter like ESLint to avoid accidental bugs and enforce a consistent code style
A CSS preprocessor for Sass that can turn (concise) Sass into standard (more verbose) CSS consumable by browsers
And so on. Organizing an environment for these sorts of things would be very difficult without NPM (which depends on Node).
None if it is necessary, but many find that it can make the development process much easier.
In the process of creating files for the client to consume, if you want to do anything more elaborate than write plain raw .js, .html, .css files, you'll need something extra - which is most often done via NPM.
It's only for extra support during development, and ease of installing libraries. almost like an extra IDE / helpful editor
for example you might want to see changes you make on your HTML and frontend javascript code, without having to refresh the preview browser. node will provide a package that does that...
it also helps install and use libraries easier. for example, if you want to add a library like bootstrap to your frontend, rather than searching around and downloading the files... but if you use node project, you can simply use npm install bootstrap that will automatically download the lastest version from the right source.
that's all

Compile Dlib for webassembly

I am trying to build dlib for webassembly using emscripten but I am not sure how to do so.
Currently, dlib generates executables and not bytecode which is needed for emscripten. Is there some way to get around this issue?
Currently this is what i am doing (from within the dlib-19.4 folder.
cd examples
mkdir build
cd build
cmake ..
emmake make
for the next step, I need to input a bytecode file however, dlib seems to just generate executables which the emcc command will not accept.
From what I can read from Dlib's documentation is that it has a lot of features that emscripten does not provide including threading and so on. So you will need to exclude functionality thats not supported.
Also referring to How to Compile section, it mentions how to use Dlib without Cmake.
It has a simple gcc example for SVM.
You should probably use this approach ie include the appropriate dlib headers in your project, write your functions and expose them for emscipten.
Then after building the js file you should be able to call the "exposed" functions from JS.

Intellisense and JsHint support for Ext Js in VisualStudio

I want to work with Senach Ext Js in a Website project in VisualStudio 2013.
The problem: VisualStudio respectively WebEssentials throws warnings like:
JsHint (W117): 'Ext' is not defined.
Here is the situation as it looks today:
The project is exactly what Sencha Cmd produced for me.
At the moment I want only a Website project and no server sided code involved (therefore I did not make a MVC or Web API project).
I know that there is a way to define some global symbols in each JavaScript file for JsHint, but I want to get the intellisense working too. So I am looking for the correct configuration for both use cases: WebEssential JSHint Warnings and Intellisense.
Maybe both problems need to be addressed separately. That would be fine too.
Is my directory structure correct (just took what Sencha Cmd built for me)?
How to address the problem to make intellisense working?
(How to avoid Webessential JavaScript warnings (it seems most or all of them are JsHint)?)
Update: Related question:
Enabling JSHint Support for Ext.js in Intellij Idea
Update:
"An _references.js file in any other location than
“~/Scripts/_references.js” will be just like any other JS file and
won’t be used for global intellisense. Even putting the file in the
root of your web project will not work as well."
Source: http://gurustop.net/blog/2012/03/03/javascript-js-intellisense-auto_complete-in-visual-studio-11-beta-the-web-_references-js-file/
still not working when I have the _references in a new "Script" folder:
So it seems like the final answer was to add a file called ~/scripts/_reference.js and then include a line to reference Ext JS in there. This enables Visual Studio to index the Ext JS source for Intellisense. For example:
/// <reference path="../ext/ext-all-debug-full.js" />
More on the Intellisense and _reference.js:
http://msdn.microsoft.com/en-us/library/bb385682.aspx
Note that due to the size of the Ext JS codebase, Visual Studio might take a few minutes to complete the indexing process and for Intellisense to start working smoothly.

Is there a node module that will compile a project to a client side file?

I'm new to node, but so many client-side open source projects use it that I am assuming there is a way to compile a node project to be compressed into one minified file that is ready for client-side usage. The open source project I'm building is using node for it's nice module and testing support, but all of the code is meant to be run on the client.
I've seen projects like this: https://github.com/mape/connect-assetmanager
But what I'm not sure of, is whether node-specific constructs like export and module are removed or supported, so that there aren't problems on the client side?
You should look at http://requirejs.org/
It replaces and extends require for both node (server-side) and browser side. It also supports merge and minification
Just came across this:
https://github.com/substack/node-browserify
Awesome project!!
You are incorrect. Node is server-side only. Perhaps you have a bit of a misunderstanding what Node is and isn't.
Nodejs is basically the Google Chrome V8 Javascript engine, packages as an executable that can run on a console. Javascript code written for node could run on the client though, but it's not as simple as you may think.
What functionality that you currently have on the server do you want to run on the client? Are you just looking for a minifier like yui compressor?

How do you automagically minify your JS and CSS on OS X or in Webstorm?

I use two different IDE's based on what I'm doing. My primary IDE is Visual Studio, whereby I use Chirpy to mash and minify my code. It works flawlessly and I love it. Problem is that when I'm not on my Windows box, I don't have access to it.
When not using Visual Studio, I'm usually writing Javascript apps in Webstorm on my Macbook Pro. Here in lies the problem. I haven't found a Webstorm plugin or any other app that I can configure to watch my scripts and mash/minify them.
How do you Mac users mash/minify your JS and CSS at design time with minimal effort?
For those who have now updated to WebStorm 6, this functionality is in-built. Simply go to File (or whatever the Mac equivalent is) -> Settings -> File Watchers and define a file watcher for the type of file you need to watch.
The relevant help documentation is here - http://www.jetbrains.com/webstorm/webhelp/using-file-watchers.html
You could use YUI Compressor without Command Line with these little Apps:
http://www.webmaster-source.com/minimus/ – free
http://www.matmartinez.net/delivery/ – free
I'm neither a Mac nor Webstorm user, but this might still be relevant.
For javascript I use the closure compiler as part of an upload script to minify. It doesn't monitor the files, it runs when I run the upload (a bash file).
If you wanted to keep it all in the IDE, it looks like Webstorm has an Ant plugin http://plugins.intellij.net/plugin/?webide&id=4526 that you could use to execute the closure compiler.
If you can find a command line css minifier then you could put that in the Ant script as well.
I use lessc and uglifyjs to concatenate and minify my css and js files. Here's a makefile from Twitter Bootstrap that I used a modified version of:
https://github.com/twitter/bootstrap/blob/master/Makefile
It's simple since all I do is type make in the command line whenever I want to compile.
I use Minify. It's on the Mac App Store.
I developed it to support my own workflow. minifyapp.com

Categories

Resources