Comparing version
@@ -62,2 +62,16 @@ var type = require('./type'); | ||
var globalVars = Object.create(null); | ||
['window', | ||
'global', | ||
'Error', | ||
'document', | ||
'navigator', | ||
'this', | ||
'undefined', | ||
'null', | ||
'location', | ||
'console'].forEach(function(o) { | ||
globalVars[o] = true; | ||
}); | ||
exports.convert = function(code, tp) { | ||
@@ -68,3 +82,2 @@ tp = tp || type.analyse(code); | ||
} | ||
//todo 对define的if判断语句移除 | ||
else if(tp.isAMD) { | ||
@@ -137,3 +150,2 @@ var res = cmdify.convert(code, tp); | ||
//TODO ~function(){}(this)的写法尚未考虑 | ||
//TODO 直接使用的未定义变量如jQuery作为依赖 | ||
var context = tp.context; | ||
@@ -151,6 +163,13 @@ //全局变量,包括全局函数 | ||
//全局使用的未声明的变量 | ||
var hash = Object.create(null); | ||
var reqs = context.getVids().map(function(v) { | ||
return v.token().content(); | ||
}).filter(function(v) { | ||
return !context.hasVar(v) && !context.hasChild(v); | ||
if(!context.hasVar(v) && !context.hasChild(v) && !(v in globalVars)) { | ||
if(v in hash) { | ||
return false; | ||
} | ||
hash[v] = true; | ||
return true; | ||
} | ||
}); | ||
@@ -157,0 +176,0 @@ var requires = ''; |
@@ -29,6 +29,2 @@ var type = require('./type'); | ||
var end = par.next().next(); | ||
//可能的define.amd.jQuery也移除 | ||
while(end.next()) { | ||
end = end.next(); | ||
} | ||
end = end.token(); | ||
@@ -75,8 +71,4 @@ res[1] = end.sIndex() + end.content().length; | ||
var index = removeAmd(context); | ||
if(index) { | ||
return code.slice(0, index[0]) + code.slice(index[1]); | ||
} | ||
else { | ||
return code; | ||
} | ||
//amd以define.amd作为判断一定会出现 | ||
return code.slice(0, index[0]) + code.slice(index[1]); | ||
} | ||
@@ -83,0 +75,0 @@ else if(tp.isCMD) { |
{ | ||
"name": "ranma", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "A converter between CommonJS/AMD/CMD/other", | ||
@@ -37,3 +37,3 @@ "maintainers": [ | ||
"dependencies": { | ||
"homunculus": "0.0.19" | ||
"homunculus": "0.1.0" | ||
}, | ||
@@ -40,0 +40,0 @@ "main": "./ranma", |
@@ -33,2 +33,8 @@ var ranma = require('../ranma'); | ||
}); | ||
it('define with if no {}', function() { | ||
var type = ranma.type.analyse('if(typeof define !== "undefined")define({})'); | ||
expect(type.isCommonJS).to.not.ok(); | ||
expect(type.isAMD).to.not.ok(); | ||
expect(type.isCMD).to.ok(); | ||
}); | ||
it('define.amd with if', function() { | ||
@@ -40,2 +46,8 @@ var type = ranma.type.analyse('if(typeof define !== "undefined" && define.amd){define({})}'); | ||
}); | ||
it('require is a function', function() { | ||
var type = ranma.type.analyse('function require(p){}require("a")'); | ||
expect(type.isCommonJS).to.not.ok(); | ||
expect(type.isAMD).to.not.ok(); | ||
expect(type.isCMD).to.not.ok(); | ||
}); | ||
it('use require', function() { | ||
@@ -144,2 +156,6 @@ var type = ranma.type.analyse('require("a")'); | ||
}); | ||
it('define with if', function() { | ||
var res = ranma.cjsify('if(typeof define !== "undefined")define({})'); | ||
expect(res).to.eql('module.exports = {}'); | ||
}); | ||
it('normal js with var', function() { | ||
@@ -161,2 +177,14 @@ var res = ranma.cjsify('var a = 1;'); | ||
}); | ||
it('normala js without var', function() { | ||
var res = ranma.cjsify('b.f();'); | ||
expect(res).to.eql('var b = require("b");b.f();'); | ||
}); | ||
it('global vars need ignore', function() { | ||
var res = ranma.cjsify('var a = window, b = document; b = c;'); | ||
expect(res).to.eql('var c = require("c");var a = window, b = document; b = c;;exports["a"] = a;exports["b"] = b;'); | ||
}); | ||
it('multi global vars', function() { | ||
var res = ranma.cjsify('var a = b;var a = b;'); | ||
expect(res).to.eql('var b = require("b");var a = b;var a = b;;module.exports = a;'); | ||
}); | ||
}); | ||
@@ -168,3 +196,3 @@ describe('cmdify', function() { | ||
}); | ||
it('define.amd.xxx', function() { | ||
it('define.amd && define.amd.xxx', function() { | ||
var res = ranma.cmdify('if(typeof define !== "undefined" && define.amd && define.amd.jQuery){define(function(){})}'); | ||
@@ -360,2 +388,56 @@ expect(res).to.eql('if(typeof define !== "undefined" ){define(function(){})}'); | ||
}); | ||
describe('bootstrap', function() { | ||
var s = fs.readFileSync(path.join(__dirname, './src/bootstrap.js'), { encoding: 'utf-8' }); | ||
var type = ranma.type.analyse(s); | ||
it('type isCommonJS', function() { | ||
expect(type.isCommonJS).to.eql(false); | ||
}); | ||
it('type isAMD', function() { | ||
expect(type.isAMD).to.eql(false); | ||
}); | ||
it('type isCMD', function() { | ||
expect(type.isCMD).to.eql(false); | ||
}); | ||
it('cjsify', function() { | ||
var res = ranma.cjsify(s); | ||
// fs.writeFileSync(path.join(__dirname, './cjs/bootstrap.js'), res, { encoding: 'utf-8' }); | ||
expect(res).to.eql(fs.readFileSync(path.join(__dirname, './cjs/bootstrap.js'), { encoding: 'utf-8' })); | ||
}); | ||
it('amdify', function() { | ||
var res = ranma.amdify(s); | ||
// fs.writeFileSync(path.join(__dirname, './amd/bootstrap.js'), res, { encoding: 'utf-8' }); | ||
expect(res).to.eql(fs.readFileSync(path.join(__dirname, './amd/bootstrap.js'), { encoding: 'utf-8' })); | ||
}); | ||
it('cmdify', function() { | ||
var res = ranma.cmdify(s); | ||
// fs.writeFileSync(path.join(__dirname, './cmd/bootstrap.js'), res, { encoding: 'utf-8' }); | ||
expect(res).to.eql(fs.readFileSync(path.join(__dirname, './cmd/bootstrap.js'), { encoding: 'utf-8' })); | ||
}); | ||
}); | ||
describe('formatter', function() { | ||
var s = fs.readFileSync(path.join(__dirname, './src/formatter.js'), { encoding: 'utf-8' }); | ||
var type = ranma.type.analyse(s); | ||
it('type isCommonJS', function() { | ||
expect(type.isCommonJS).to.eql(true); | ||
}); | ||
it('type isAMD', function() { | ||
expect(type.isAMD).to.eql(true); | ||
}); | ||
it('type isCMD', function() { | ||
expect(type.isCMD).to.eql(false); | ||
}); | ||
it('cjsify', function() { | ||
var res = ranma.cjsify(s); | ||
expect(res).to.eql(s); | ||
}); | ||
it('amdify', function() { | ||
var res = ranma.amdify(s); | ||
expect(res).to.eql(s); | ||
}); | ||
it('cmdify', function() { | ||
var res = ranma.cmdify(s); | ||
// fs.writeFileSync(path.join(__dirname, './cmd/formatter.js'), res, { encoding: 'utf-8' }); | ||
expect(res).to.eql(fs.readFileSync(path.join(__dirname, './cmd/formatter.js'), { encoding: 'utf-8' })); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
1990950
10.94%46
9.52%55161
10.32%+ Added
- Removed
Updated