Need javascript version of coffee in my code


#1

I need the text of transpiled JS from a CS file. In other words I have a coffee file (myCS.coffee) and I need the javascript version of it as a string in my code. I have two ideas …

  • Find the Atom-compiled javascript file and read it in. I can’t find this file because apparently it is in a cache folder with a UUID name.

  • Compile the CS version in my code but I don’t know how to do this. I’ve been googling this a lot.

Can anyone help?


#2

I found out how to compile a CS file in my code …

coffee = require 'coffee-script'
console.log coffee.compile "->", {bare:yes}

I found it by guessing :-). This bothers me because it will have to compile at every activation. If anyone can think of anything better I would appreciate it.


#3

It sounds like you need the compiled* JS generated at runtime? And then you want to display it? Or execute it? Or what?

And the other question is why do you need this?

* I hate the term “transpiled”, compilation is the act of transforming code from one representation to another whether that is from a high-level language to machine code, from Java to Java bytecode, from CSharp to IL, or from CoffeeScript to JavaScript. We don’t need a new term for it.


#4

I’m writing a package (coffee-trace) that adds debug trace code to a coffeescript file. It’s something I’ve done before using a home-made utility. Each trace call needs to call a function at run time. I add this run-time function to the source file automatically. The run-time code needs to be JS so I can put it at the bottom and lift it using a named function (function x(){}). But I want to code the function in CS.

Did that make sense? Can you think of a better way to do this.

You’re fighting an uphill battle. Not only is it widely used but wikipedia defines it as “close to the same level” and CS and JS are definitely close. Much of the semantics are identical.


#5

Ok, I think I understand. There’s no way to import the CS file containing the run-time function using a require statement? Or is the CoffeeScript source that you’re trying to trace running somewhere other than inside Atom?

I realize that I’m fighting an uphill battle on the “transpile” thing. I have a rant brewing about the stuff I feel is underpinning things like this, it’ll end up on my blog eventually, I hope :grinning:


#6

So where is this blog?


#7

It’s the web page that’s in my profile, http://www.lee-dohm.com.