Search code examples
javascriptcoffeescriptamd

CoffeeScript - load lib with AMD or in the window object (classic way)


do ((context, lib)->
  console.log context
  console.log lib

)(this, (context, lib)->
  console.log context
  lib_ = context.lib

  lib.version = '0.1'
  return lib
)

I want to be able to load lib either with amd or in the window object. I manage to get the wanted result but has an error because of the last set of paranteses () The generated JavaScript code :

(function(context, lib) {
  console.log(context);
  return console.log(lib);

})(this, function(context, lib) {
  console.log(context);

  var lib_ = context.lib;
  lib.version = '0.1';
  return lib;

})(); // this last set of paranteses cause an error 

I tried to write the CoffeeScript to generate JS this way also : (with no succes)

(function(context, lib) {
  console.log(context);
  return console.log(lib);

}(this, function(context, lib) {
  console.log(context);

  var lib_ = context.lib;
  lib.version = '0.1';
  return lib;
}));

From what i know it's posssible to write JS code in CoffeeScript to bypass this issue but i would like that to be the last option.


Solution

  • If you remove the do from the first line, the output looks like this:

    (function(context, lib) {
      console.log(context);
      return console.log(lib);
    })(this, function(context, lib) {
      var lib_;
      console.log(context);
      lib_ = context.lib;
      lib.version = '0.1';
      return lib;
    });
    

    So, CS:

    ((context, lib)->
      console.log context
      console.log lib
    
    )(this, (context, lib)->
      console.log context
      lib_ = context.lib
    
      lib.version = '0.1'
      return lib
    )