Skip to content
This repository has been archived by the owner on Dec 31, 2017. It is now read-only.

UMD modules #91

Open
adros opened this issue Nov 15, 2016 · 1 comment
Open

UMD modules #91

adros opened this issue Nov 15, 2016 · 1 comment

Comments

@adros
Copy link

adros commented Nov 15, 2016

I have a module that I use in both Node and Dojo, so I have some kind of UMD mechanism:

({
	define : typeof define != "undefined" ? define : function(deps, factory) {
		module.exports = factory();
	}
}).define([], function() {

Is there any way to use this syntax and correctly parse the docs? Currently it fails to load this file as dependency of other AMD module.

Or could you give me a hint, what would be the best way to implement custom 'callHandler'? The doc says it's possible, but there are not instructions how should a callHandler be implmented.

@adros
Copy link
Author

adros commented Nov 16, 2016

If anyone interested, I have succeed in creating custom callHandler for UMD:

define([
    "dojo/_base/lang",
    "dojo/AdapterRegistry",
    "jsdocparser/lib/env",
    "jsdocparser/lib/callHandler/amd",
    "jsdocparser/lib/console"
], function(lang, AdapterRegistry, env, amd, console) {

    var handlers = new AdapterRegistry();

    //we can reuse same processing as amd, just detection is different
    //js-doc-parser was modified to expose '_handleDefine' function
    handlers.register("define", isUmd, amd._handleDefine);

    function isUmd(callInfo) {
        // we consider callInfo to be UMD definition if identifier has 2 items, 
        // first one is objet, and second one is Identifier with name 'define'

        // Sample of UMD module
        //      ({
        //          define : typeof define != "undefined" ? define : function(deps, factory) {
        //              module.exports = factory();
        //          }
        //      }).define([], function() {

        var identifier = callInfo.identifier;

        if (identifier.length == 2 //
                && identifier[0].type == "object" //
                && identifier[1].type == "Identifier" //
                && identifier[1].name == "define") {
            console.log("Detected module as UMD", callInfo.callee.file && callInfo.callee.file.moduleId);
            return true;
        }
        return false;
    }

    return handlers;
});

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

No branches or pull requests

1 participant