Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional export for UMD standalone #5

Open
bisubus opened this issue Dec 23, 2015 · 2 comments
Open

Optional export for UMD standalone #5

bisubus opened this issue Dec 23, 2015 · 2 comments

Comments

@bisubus
Copy link

bisubus commented Dec 23, 2015

This would be beneficial because it tends to pollute globals even when it is unncessary.

The possible case is Angular module, e.g.

(function(__root, __factory) {
    if (typeof define === "function" && define.amd) {
        define("module-name", ["angular"], __factory);
    } else if (typeof exports === "object") {
        module.exports = __factory(require("angular"));
    } else {
        __root["module-name"] = __factory(angular);}
})(this, (function(__small$_mod_0) {

var exports = {};
...
exports = "moduleName";
return exports;
}))

The wide-spread convention for modular Angular workflow is

angular.module('appName', [require('module-name')]);
// instead of
// require('module-name');
// angular.module('appName', ['moduleName']);

So exported string is used only in the context of require or import, and __root["module-name"] = could be safely omitted if exportPackage.standalone === ''.

The checks for exportPackage.amd and exportPackage.commonjs could be added for consistency as well, but I see no possible use for that.

Can we do that?

@ivogabe
Copy link
Owner

ivogabe commented Dec 23, 2015

I think it would be better to add this to a new export style instead of modifying UMD. I'm a bit afraid because Angular 2 is coming, and this option would become useless in the future. I could see some value in adding customization support, for instance by templates or something like that. What would you think of that? Or maybe something like this:

function wrapAngular(name, dependencies) {
  return {
    header: "angular.module(" + JSON.stringify(name) + ", [" + dependencies.map(x => "require(" + JSON.stringify(x) + ")").join(",")),
    footer: ");"
  }
}
...
small(..., { wrap: wrapAngular });

@bisubus
Copy link
Author

bisubus commented Dec 23, 2015

It is a common case for any plugin/modular bundle, jQuery plugins are similar. They may have something to export, but in global scope they won't expose everything outside jQuery. There are a few ones that exploit UMD template like that.

Angular 2 is totally different beast, I wouldn't worry about the future of 1.x for the next 2-3 years. But yes, the things are going to change for 1.x, I guess that angular.module hardly requires any special treatment from module bundler, the same thing can be done now with ES7/TS decorators but in more conventional way.

Anyway, customisable UMD template with logic (i.e. EJS) would be advantageous in this case imo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants