加载器中的预编
if (_type == "js") {//js预编译 var _script = "_define(function(exports,module){\n"; _script += "var $parent = \"" + _basePath + "\";\n"; _script += data.replace(/require\(/g, "_require($parent,"); _script += ";\n});" + "//@ sourceURL=" + _absUrl(path); _moudle = window["eval"](_script);
}
上面这段程序会把下面的代码
exports.txt = "hello word"; var rep=require("./js/moule"); module.exports={ "name":"张三" }
预编译为
_define(function(exports,module){ var $parent = ""; exports.txt = "hello word"; var rep=_require($parent,"./js/moule"); module.exports={ "name":"张三" }; });//@ sourceURL=http://localhost:63342/jiaocheng/moudle.js
1.加上了_define(function(exports,module){
2.var rep=require("./js/moule"); 变为了var rep=_require($parent,"./js/moule");
3.加上了var $parent = ""; $parent 是 当前js 的路径,因为 当前js在根目录 所以$parent为空