max pleaner max pleaner - 1 year ago 46
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"

refers to my
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
is that I'm loading patches for these core classes.

Answer Source

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.