max pleaner max pleaner - 1 month ago 6
CoffeeScript Question

With the coffee CLI, how I can assign variables to required files?

I have figured out that it's possible to do something like this:

coffee -r "_=underscore"


In Javascript there seems to be no way to autoload constants; i.e. whatever you export you have to not only require but also explicitly assign a variable.

I am trying to create a REPL for my program, a shell script that contains this:

coffee -r "{String, Array, Algorithms}=./port"


where
./port
refers to my
port.coffee
file. However, that doesn't work. I get this error:

{String, Array, Algorithms} = require('./port')
^
SyntaxError: Unexpected token =


This looks like valid NodeJs so I'm not sure why it's not working.

The reason I'm exporting & requiring
String
and
Array
is that I'm loading patches for these core classes.

Answer

If you are fine with polluting the global scope with q namespace variable, you can import in CoffeeScript using this workaround:

App = window.App || {}

_ = window._

# Your code here...

App.String = String
App.Array = Array
App.Algorithms = Algorithms

Then just include your script tags in order of their dependencies where all non-library code has the same App namespace at the top to share with each other through the global object.