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

UMD (Universal Module Definition) patterns for JavaScript modules that work everywhere. #116

Closed
whitehat101 opened this issue Feb 3, 2015 · 2 comments

Comments

@whitehat101
Copy link
Contributor

https://github.com/umdjs/umd

I stumbled upon a project that may provide the cross-environment module loading goodness Marcus has been dreaming about. The issue title / repo description pretty much says it all. I haven't tested any of this personally, but I'm optimistic.

I believe the CommonJs Strict example is the most compatible flavor out there. I'm not a Node developer. If CommonJs is just for loading backend Node dependencies, then this may not be the best flavor. However, the project is well documented, and I expect a little bit of reading will answer most questions.

I'd love to polish this into a PR, but I have a bunch on my plate right now.

Hope it helps.

https://github.com/umdjs/umd/blob/master/commonjsStrict.js:

// Uses CommonJS, AMD or browser globals to create a module.

// If you just want to support Node, or other CommonJS-like environments that
// support module.exports, and you are not creating a module that has a
// circular dependency, then see returnExports.js instead. It will allow
// you to export a function as the module value.

// Defines a module "commonJsStrict" that depends another module called "b".
// Note that the name of the module is implied by the file name. It is best
// if the file name and the exported global have matching names.

// If the 'b' module also uses this type of boilerplate, then
// in the browser, it will create a global .b that is used below.

// If you do not want to support the browser global path, then you
// can remove the `root` use and the passing `this` as the first arg to
// the top function.

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['exports', 'b'], factory);
    } else if (typeof exports === 'object') {
        // CommonJS
        factory(exports, require('b'));
    } else {
        // Browser globals
        factory((root.commonJsStrict = {}), root.b);
    }
}(this, function (exports, b) {
    //use b in some fashion.

    // attach properties to the exports object to define
    // the exported module properties.
    exports.action = function () {};
}));
@adam-lynch
Copy link

You can automate this with gulp-wrap-umd. I've used it for simple-feature-detector.

@marcuswestin
Copy link
Owner

@whitehat101 you're awesome.

Now let's see if amdjs holds up to its promise.

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

3 participants