21

I've noticed that when compiling CoffeeScript, none of the single-line comments are retained.

This is problematic as I'm trying to write a greasemonkey/userscript in CoffeeScript, and they rely on comments for the metadata block.

I've tried using backticks, but there seems to be a problem with backticks around comments:

`// ==UserScript==
// @version       1.0
// ==/UserScript==`

alert "hello world"

Becomes

// ==UserScript==
// @version       1.0
// ==/UserScript==;alert("hello world");

And if I add an extra line before the closing backtick I get:

// ==UserScript==
// @version       1.0
// ==/UserScript==
;alert("hello world");

It would also be nice to have the convenience of automatic wrapping.. but I suppose without -bare the metadata block would be wrapped as well.

Is there a better way I could be going about this?

Acorn
  • 49,061
  • 27
  • 133
  • 172

1 Answers1

19

I don't use CoffeeScript, but from the docs it looks like you could use:

###
// ==UserScript==
// @version       1.0
// ==/UserScript==
###
alert "hello world"


Which would yield:

/*
// ==UserScript==
// @version       1.0
// ==/UserScript==
*/
alert("hello world");

which parses perfectly fine as a GM script. The metadata reads correctly.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • 1
    This *almost* works.... If you're trying to use Sprockets, however, you'll run into erros with this approach (since `//= ` is replaced with the code of ``, so you get /* //= require */ --> /* (code of something) */ – AlexeyMK Jul 23 '11 at 00:35
  • 3
    I doubt they are using sprockets for userscript development. – Andrew Burns Jul 20 '12 at 15:26
  • 1
    To emit `require` consumable by Sprockets (Rails asset pipeline), you can use `*=`, like so: https://gist.github.com/joliss/9480786 (in case anyone else is wondering) – Jo Liss Mar 11 '14 at 06:56