当前位置 : 主页 > 网络编程 > JavaScript >

各种UMD方法集合

来源:互联网 收集:自由互联 发布时间:2021-06-30
通用模块代码,其中包含AMD,CMD等 // 方案一:umd Jquery plugin(function(factory) { if (typeof define === 'function' define.amd) { // AMD. Register as an anonymous module. define(['jquery'], factory); } else { // Browser global
通用模块代码,其中包含AMD,CMD等
// 方案一:umd Jquery plugin
(function(factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['jquery'], factory);
    } else {
        // Browser globals
        factory(jQuery);
    }
}(function($) {
    $.fn.jqueryPlugin = function() {};
}));

// 方案二:umd Node adapter
// Help Node out by setting up define.
if (typeof module === 'object' && typeof define !== 'function') {
    var define = function(factory) {
        module.exports = factory(require, exports, module);
    };
}
define(function(require, exports, module) {
    var b = require('b');
    return function() {};
});

// 方案三:umd Commonjs adapter
// Help Node out by setting up define.
if (typeof exports === 'object' && typeof define !== 'function') {
    var define = function(factory) {
        factory(require, exports, module);
    };
}
define(function(require, exports, module) {
    var b = require('b');
    // Only attach properties to the exports object to define
    // the module's properties.
    exports.action = function() {};
});

// 方案四:umd Commonjs strict
(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) {
    // attach properties to the exports object to define
    // the exported module properties.
    exports.action = function() {};
}));

// 方案五:umd AMD or browser global
(function(root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['b'], factory);
    } else {
        // Browser globals
        root.amdWeb = factory(root.b);
    }
}(this, function(b) {
    function amdWeb() {}
    return amdWeb;
}));

// 方案六:umd AMD with global export
(function(root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['b'], function(b) {
            // Also create a global in case some scripts
            // that are loaded still are looking for
            // a global even when an AMD loader is in use.
            return (root.amdWebGlobal = factory(b));
        });
    } else {
        // Browser globals
        root.amdWebGlobal = factory(root.b);
    }
}(this, function(b) {
    function amdWebGlobal() {}
    return amdWebGlobal;
}));

// 方案七:umd Commonjs strict with global
(function(root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['exports', 'b'], function(exports, b) {
            factory((root.commonJsStrictGlobal = exports), b);
        });
    } else if (typeof exports === 'object') {
        // CommonJS
        factory(exports, require('b'));
    } else {
        // Browser globals
        factory((root.commonJsStrictGlobal = {}), root.b);
    }
}(this, function(exports, b) {
    // attach properties to the exports object to define
    // the exported module properties.
    exports.action = function() {};
}));

// 方案八:umd jquery plugin with Commonjs
(function(factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['jquery'], factory);
    } else if (typeof exports === 'object') {
        // Node/CommonJS
        factory(require('jquery'));
    } else {
        // Browser globals
        factory(jQuery);
    }
}(function($) {
    $.fn.jqueryPlugin = function() {};
}));
网友评论