回顾

上篇文章大概展示了kmdjs0.1.x时期的编程范式:

如下面所示,可以直接依赖注入到function里,

kmdjs.define('main',['util.bom','app.Ball','util.dom.test'], function(bom,Ball,test) {
var ball = new Ball(0, 0, 28, 1, -2, 'kmdjs');
var vp = bom.getViewport();
});

也可以直接在代码里把full namespace加上来调用,如:

kmdjs.define('main',['util.bom','app.Ball','util.dom.test'], function() {
var ball = new app.Ball(0, 0, 28, 1, -2, 'kmdjs');
var vp = util.bom.getViewport();
});

而且,在循环依赖的场景,因为执行顺序的问题,会导致第一种方式注入undefined,所以循环依赖的情况下只能用full namespace的方式来调用。

这种编程体验虽然已经足够好,但是可以更好。怎样才算更好?

  1. 不用依赖注入function
  2. 不用写full namespace,自动匹配依赖

如下所示:

kmdjs.define('main',['util.bom','app.Ball','util.dom.test'], function() {
var ball = new Ball(0, 0, 28, 1, -2, 'kmdjs');
var vp = bom.getViewport();
});

这就要借助uglifyjs能力,把function的字符串替换成带有namespace就可以实现上面的效果。

uglifyjs依赖分析和代码重构

function fixDeps(fn,deps) {
var U2 = UglifyJS;
//uglify2不支持匿名转ast
var code = fn.toString().replace('function','function ___kmdjs_temp');
var ast = U2.parse(code);
ast.figure_out_scope();
var nodes = []; ast.walk(new U2.TreeWalker(function (node) { if (node instanceof U2.AST_New) {
var ex = node.expression;
var name = ex.name;
isInWindow(name) || isInArray(nodes, node) || isInScopeChainVariables(ex.scope, name) || nodes.push({name:name,node:node});
} if (node instanceof U2.AST_Dot) {
var ex = node.expression;
var name = ex.name;
var scope = ex.scope;
if (scope) {
isInWindow(name) || isInArray(nodes, node) || isInScopeChainVariables(ex.scope, name) || nodes.push({name:name,node:node});
}
} if (node instanceof U2.AST_SymbolRef) {
var name = node.name;
isInWindow(name) || isInArray(nodes, node) || isInScopeChainVariables(node.scope, name) || nodes.push({name:name,node:node});
}
})); var cloneNodes = [].concat(nodes);
//过滤new nodes 中的symbo nodes
for (var i = 0, len = nodes.length; i < len; i++) {
var nodeA = nodes[i].node;
for (var j = 0, cLen = cloneNodes.length; j < cLen; j++) {
var nodeB = cloneNodes[j].node;
if (nodeB.expression === nodeA) {
nodes.splice(i, 1);
i--;
len--;
}
}
} for (var i = nodes.length; --i >= 0;) {
var item = nodes[i],
node=item.node,
name=item.name;
var fullName=getFullName(deps,name);
var replacement;
if (node instanceof U2.AST_New) {
replacement = new U2.AST_New({
expression: new U2.AST_SymbolRef({
name:fullName
}),
args: node.args
});
} else if (node instanceof U2.AST_Dot) {
replacement = new U2.AST_Dot({
expression: new U2.AST_SymbolRef({
name: fullName
}),
property: node.property
});
}else if(node instanceof U2.AST_SymbolRef){
replacement = new U2.AST_SymbolRef({
name: fullName
});
} var start_pos = node.start.pos;
var end_pos = node.end.endpos; code = splice_string(code, start_pos, end_pos, replacement.print_to_string({
beautify: true
}));
}
return code.replace('function ___kmdjs_temp','function');
} function getFullName(deps,name){
var i= 0,
len=deps.length,
matchCount= 0,
result=[]; for(;i<len;i++) {
var fullName = deps[i];
if (fullName.split('.').pop() === name) {
matchCount++;
if (!isInArray(result, fullName)) result.push(fullName);
}
} if(matchCount>1){
throw "the same name conflict: "+result.join(" and ");
} else if(matchCount===1){
return result[0];
}else{
throw ' can not find module ['+name+']';
}
} function splice_string(str, begin, end, replacement) {
return str.substr(0, begin) + replacement + str.substr(end);
} function isInScopeChainVariables(scope, name) {
var vars = scope.variables._values;
if (Object.prototype.hasOwnProperty.call(vars, "$" + name)) {
return true;
} if (scope.parent_scope) {
return isInScopeChainVariables(scope.parent_scope, name);
} return false;
} function isInArray(arr,name){
var i= 0,len=arr.length;
for(;i<len;i++){
if(arr[i]===name){
return true;
}
}
return false;
} function isInWindow(name){
if(name==='this')return true;
return name in window;
}

通过上面的fixDeps,可以对代码就行变换。如:

 console.log(fixDeps(function (A) {
var eee = m;
var b = new A();
var b = new B();
var c = new C();
var d = G.a;
},['c.B','AAA.G','SFSF.C','AAAA.m'] ))

输出:

function (A) {
var eee = AAAA.m;
var b = new A();
var b = new c.B();
var c = new SFSF.C();
var d = AAA.G.a;
}

这样,kmdjs在执行模块function的时候,只需要fixDeps加上full namespace就行:

function buildBundler(){
var topNsStr = "";
each(kmdjs.factories, function (item) {
nsToCode(item[0]);
});
topNsStr+= kmdjs.nsList.join('\n') +"\n\n";
each(kmdjs.factories, function (item) {
topNsStr+=item[0]+' = ('+ fixDeps(item[2],item[1])+')();\n\n' ;
});
if(kmdjs.buildEnd) kmdjs.buildEnd(topNsStr);
return topNsStr;
}

build出来的包,当然全都加上了namespace。再也不用区分循环依赖和非循环依赖了~~~

Github

上面的所有代码可以Github上找到:

https://github.com/kmdjs/kmdjs

最新文章

  1. 弹出层layer的使用
  2. java.io.IOException: invalid header field
  3. Spring MVC处理异常的4种方式
  4. QT开发实战精解
  5. 查询Sql Server Agent 的job的执行情况。
  6. float类型进行计算精度丢失的问题
  7. Transform 1
  8. mysql 保留的关键字
  9. 国内5款优秀的WEB前端框架
  10. 我用的php开发环境是appserv一键安装,通过http://localhost测试成功,但是我有点不清楚的就是为什么访问.php文件要在地址栏上加上localhost(即http://localhost/text.php)才能成功访问?
  11. fastreport.net cdoe 自己的代码
  12. 为什么String类是不可变的?
  13. VSCode 同步设置及扩展插件 实现设备上设置统一
  14. 创建servlet程序知识点详解---servlet-day05
  15. CRM 权限设置 ss
  16. 第一天---关于环境和java基础
  17. cpu监控之二:dstat
  18. 微服务 通过EnableFeignClients调用其他服务的api
  19. std::string compare
  20. 从userAgent判断浏览器是什么(chorme ie 火狐)浏览器类型检测、浏览器检测

热门文章

  1. Impress.js上手 - 抛开PPT、制作Web 3D幻灯片放映
  2. Greenplum 的分布式框架结构
  3. Zookeeper常用命令
  4. Floyd-Warshall 全源最短路径算法
  5. 【技巧】使用weeman来做一个钓鱼网页
  6. ERROR ITMS-90167: &quot;No .app bundles found in the package&quot;错误
  7. vue源码解析阅读列表
  8. ASP.NET 页面禁止被 iframe 框架引用
  9. php-resque的设计和使用
  10. 《PDF.NE数据框架常见问题及解决方案-初》