返回列表

JS模块管理

默认分类 2013/10/04 21:51

又造了一个轮子.....

'use strict';
//define.js
if (typeof (define)  == 'undefined') {
    //define('./2.js',['./bui.js','./htmlparser.js'], function(exports){exports.todo='...';});
    var define = function (name, deps, fun) {
        //Allow for anonymous modules
        if (typeof name !== 'string') {
            //Adjust args appropriately
            fun = deps;
            deps = name;
            name = null;
        }

        //This module may not have dependencies
        if (!deps || !deps.splice) {
            fun = deps;
            deps = null;
        }

        deps = deps && deps.splice && deps.length ? deps : [];

        define.modules = define.modules || [];
        define.modules.push({
            name: name, 
            depend: deps, 
            left: deps.join(',').replace(/[ ]/g, ''), 
            todo: fun, 
            exports: {}
        });

        for (var i=0,len=deps.length; i<len; i++) {
            var exist = false;
            var url = deps[i];
            // Check loaded url
            for (var j=0,len2=define.loaded.length; j<len2; j++) {
                if (define.loaded[j] == url) {
                    exist = true;
                    break;
                }
            }

            if (!exist) {
                require(url);
                // File loaded immediately in Nodejs.
                if (typeof (window) == 'undefined') {
                    define.loaded.push(url);
                }
            }
        }
        define.checkDepend();
    };
    define.loaded = [];
    define.checkDepend = function () {
        define.modules = define.modules || [];
        var list = define.modules,
            loaded = define.loaded,
            url;
        for (var j=0,len2=loaded.length; j<len2; j++) {
            url = loaded[j];
            for (var i=0,len=list.length; i<len; i++) {

                list[i].left = list[i].left.replace(','+url, '').replace(url+',', '').replace(url, '');
                console.log(list[i].left);
                if (list[i].left.length<1) {
                    list[i].todo(list[i].exports);
                }
            }
        }
    };
    (typeof (global) == 'undefined' ? window.define = define : global.define = define);
};
// require for window
if (typeof (require) == 'undefined') {
    // require('./define.js');
    var require = function(url) { 
        var callback = function() { 
            // Check depends
            if (typeof (define) != 'undefined' && define.checkDepend) {
                define.loaded.push(url);
                define.checkDepend();
                console.log(url);
            }
        };

        window.require.onloadStack = window.require.onloadStack || [];
        window.require.onloadStack.push(callback);        

        if (document.body != null || (document.getElementsByTagName && document.getElementsByTagName('body')[0] != null)) { 
            //write ready function here 
            var domscript = document.createElement('script'); 
            domscript.src = url; 
            domscript.onload = callback; 
            document.getElementsByTagName('head')[0].appendChild(domscript); 
        }
        else {
            document.write('<script type="text/javascript" src="' + url + '"' +
            ' onload="window.require.onloadStack[' + (window.require.onloadStack.length-1) + ']()"><'+'/script>'); 
        }
    };
    window.require = require;
}
// global for window
if (typeof (global)  == 'undefined') {
    window.global = window;
}

使用示例1:

'use strict';
//a.js
define('./a.js', [], function(exports){
    var dd = {};
    dd.name = 'out-a';
    dd.out = function () {
        alert(dd.name);
    };
    exports.out = dd.out;
    console.log(exports);
});

使用示例2:

'use strict';
//b.js
define('./b.js', ['./a.js'], function(exports){
    var dd = {};
    dd.name = 'out-b';
    dd.out = function () {
        alert(dd.name);
    };
    exports.out = dd.out;
    console.log(exports);
});