I have a class in CoffeeScript that I would like to use both on the server and in the broser.
For the server I need
class classname
constructor: (@arg)->
#code
module.classname = classname
But for the brwoser I need
class classname
constructor: (@arg)->
#code
window.classname = classname
Is there anyway to make it so that only one file has to be maintained?
In the general case it requires using libraries like requirejs and browserify, but in your case you can simply write:
class classname
constructor: (@arg)->
#code
module?.classname = classname
window?.classname = classname
This code will check the existence of module and window variables before modifying them.