Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Created July 15, 2013 18:34
Show Gist options
  • Select an option

  • Save WebReflection/6002262 to your computer and use it in GitHub Desktop.

Select an option

Save WebReflection/6002262 to your computer and use it in GitHub Desktop.
UMD does not really have to be transpiled per each module ... or does it?
function UMD(name, dependencies, factory) {
// AMD
if ('function' === typeof define && define.AMD) {
define(name, dependencies, factory);
}
// node.js
else if('object' === typeof exports) {
module.exports = factory.apply(
null,
(dependencies || []).map(require)
);
}
// browsers / global
else {
Function('this.'.concat(
name,
'=f.apply(null,[',
(dependencies || []).join(','),
'])'
))();
}
// how abour Rhino load ? ... maybe later on
}
@WebReflection

Copy link
Copy Markdown
Author

so you can ...

UMD('Backbone', ['jQuery', '_'], function (jQuery, _) {
  var Backbone = {};
  // the stuff ...
  return Backbone;
});

@ryanflorence

Copy link
Copy Markdown

The point is to meet all the tools where they are today w/o anything more than a transpile step, and then requiring nothing at all when es6 is supported.

This would require the transpile step to create a global UMD function, confirm? I don't think that's acceptable, and all we gain is a few bytes. Gzip saves us.

@ryanflorence

Copy link
Copy Markdown

Also, I want to author in es6, and everybody has incentive to author in es6 modules because they are coming. Selling people on authoring with this UMD function is the same battle as just using AMD everywhere.

We have to agree on a module format, any proposal other than the spec will never fly. Then we transpile into formats that existing tools/environments understand already.

@WebReflection

Copy link
Copy Markdown
Author

you are ssuming people will actually write ES6 soon ... that ain't realistic, so this is an alternative that does not require a transpiling step at all and best of all does not require developers writing a non final/standard/complete yet specification as it is for ES6, even if module are frozen (they are not)

@ryanflorence

Copy link
Copy Markdown

We have to pick something, might as well be something that is going to be the spec (or close to it).

@ryanflorence

Copy link
Copy Markdown

you are assuming people will actually write ES6 soon

I'm asking them to, yes :)

@frostney

frostney commented Nov 6, 2014

Copy link
Copy Markdown

I actually made something like your gist: https://github.com/freezedev/udefine
While I've used this in some production apps, nowadays I rather want to write ES6 modules though :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment