Comparing version 0.0.3 to 0.0.4
@@ -138,3 +138,5 @@ var type = require('./type'); | ||
//全局变量,包括全局函数 | ||
var gVars = context.getVars(); | ||
var gVars = context.getVars().map(function(v) { | ||
return v.leaves()[0].token().content(); | ||
}); | ||
var gChildren = context.getChildren(); | ||
@@ -146,17 +148,27 @@ gChildren.forEach(function(child) { | ||
}); | ||
//全局使用的未声明的变量 | ||
var reqs = context.getVids().map(function(v) { | ||
return v.token().content(); | ||
}).filter(function(v) { | ||
return !context.hasVar(v) && !context.hasChild(v); | ||
}); | ||
var requires = ''; | ||
reqs.forEach(function(req) { | ||
requires += 'var ' + req + ' = require("' + req + '");'; | ||
}); | ||
//没有全局变量赋值null,只有一个则直接赋给module.exports;否则将变量名作为key加上值组成hash赋给exports | ||
if(gVars.length == 0) { | ||
return code + ';module.exports = null;' | ||
return requires + code; | ||
} | ||
else if(gVars.length == 1) { | ||
return code + ';module.exports = ' + gVars[0].leaves()[0].token().content() + ';'; | ||
return requires + code + ';module.exports = ' + gVars[0] + ';'; | ||
} | ||
else { | ||
var res = code + ';'; | ||
var res = requires + code + ';'; | ||
gVars.forEach(function(v) { | ||
v = v.leaves()[0].token().content(); | ||
res += 'exports["' + v + '"] = ' + 'v;'; | ||
res += 'exports["' + v + '"] = ' + v + ';'; | ||
}); | ||
return res; | ||
} | ||
} | ||
}; |
{ | ||
"name": "ranma", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A converter between CommonJS/AMD/CMD/other", | ||
@@ -5,0 +5,0 @@ "maintainers": [ |
@@ -142,2 +142,18 @@ var ranma = require('../ranma'); | ||
}); | ||
it('normal js with var', function() { | ||
var res = ranma.cjsify('var a = 1;'); | ||
expect(res).to.eql('var a = 1;;module.exports = a;') | ||
}); | ||
it('normal js with function', function() { | ||
var res = ranma.cjsify('function a(){}'); | ||
expect(res).to.eql('function a(){};module.exports = a;') | ||
}); | ||
it('normal js with deps', function() { | ||
var res = ranma.cjsify('var a = b;'); | ||
expect(res).to.eql('var b = require("b");var a = b;;module.exports = a;'); | ||
}); | ||
it('normal js with multi global vars', function() { | ||
var res = ranma.cjsify('var a = b, c = d;'); | ||
expect(res).to.eql('var b = require("b");var d = require("d");var a = b, c = d;;exports["a"] = a;exports["c"] = c;'); | ||
}); | ||
}); | ||
@@ -144,0 +160,0 @@ describe('cmdify', function() { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
1794635
50002
0