Comparing version 0.1.0 to 0.2.0
@@ -85,2 +85,8 @@ var type = require('./type'); | ||
} | ||
else if(tp.isModule) { | ||
return tp.code; | ||
} | ||
else if(tp.es6) { | ||
return exports.convert(tp.code); | ||
} | ||
else if(tp.isAMD) { | ||
@@ -87,0 +93,0 @@ var res = cmdify.convert(code, tp); |
@@ -201,2 +201,8 @@ var type = require('./type'); | ||
} | ||
else if(tp.isModule) { | ||
return 'define(function(require, exports, module) {' + tp.code + '});'; | ||
} | ||
else if(tp.es6) { | ||
return 'define(function(require, exports, module) {' + cjsify.convert(tp.code) + '});'; | ||
} | ||
else { | ||
@@ -203,0 +209,0 @@ return 'define(function(require, exports, module) {' + cjsify.convert(code, tp) + '});'; |
{ | ||
"name": "ranma", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A converter between CommonJS/AMD/CMD/other", | ||
@@ -16,3 +16,4 @@ "maintainers": [ | ||
"blanket": { | ||
"pattern": ["ranma/amdify.js", | ||
"pattern": [ | ||
"ranma/amdify.js", | ||
"ranma/cmdify.js", | ||
@@ -22,3 +23,4 @@ "ranma/cjsify.js", | ||
"ranma/type.js", | ||
"ranma/exist.js"] | ||
"ranma/exist.js" | ||
] | ||
} | ||
@@ -34,3 +36,4 @@ }, | ||
"amd", | ||
"cmd" | ||
"cmd", | ||
"es6 module" | ||
], | ||
@@ -40,3 +43,4 @@ "author": "army8735", | ||
"dependencies": { | ||
"homunculus": "^0.2.3" | ||
"homunculus": "^0.3.7", | ||
"jsdc": "^0.4.2" | ||
}, | ||
@@ -43,0 +47,0 @@ "main": "./index", |
@@ -1,3 +0,5 @@ | ||
## A converter between CommonJS/AMD/CMD/other | ||
# A converter between CommonJS/AMD/CMD/other | ||
ranma取自同名动漫人物乱马,意指同一个事物的不同形式。 | ||
[![NPM version](https://badge.fury.io/js/ranma.png)](https://npmjs.org/package/ranma) | ||
@@ -9,5 +11,4 @@ [![Build Status](https://travis-ci.org/army8735/ranma.svg?branch=master)](https://travis-ci.org/army8735/ranma) | ||
为满足所写的代码能同时运行于server环境和web环境,而不需手动修改,所以做了个转换方法,使得几者之间的模块能够互相等价转化。 | ||
<br/>需要注意的是AMD模块的写法应遵守文件和模块一对一的原则。 | ||
ranma取自同名动漫人物乱马,意指同一个事物的不同形式。 | ||
需要注意的是AMD模块的写法应遵守文件和模块一对一的原则。 | ||
@@ -22,29 +23,33 @@ ## INSTALL | ||
* ranma.cjsify(code:String):String | ||
<br/>将代码转换为CommonJS | ||
<br/>对于AMD和CMD,会将define的factory提取,改写return为module.exports并删除define,如果define父语句有if判断也会删除 | ||
<br/>对于普通文件,会将全局声明的变量作为exports,全局使用的未声明变量作为require | ||
* cjsify(code:String):String | ||
* 将代码转换为`CommonJS` | ||
* 对于`AMD`和`CMD`,会将define的factory提取,改写return为module.exports并删除define,如果define父语句有if判断也会删除 | ||
* 对于`es6 module`,会将module和import变为require,export变为exports,export default变为module.exports | ||
* 对于普通文件,会将全局声明的变量作为exports,全局使用的未声明变量作为require | ||
* ranma.amdify(code:String):String | ||
<br/>将代码转换为AMD | ||
<br/>在cjsify的基础上进行define包裹 | ||
<br/>如果代码是CMD不做修改,因为AMD兼容这种写法 | ||
* amdify(code:String):String | ||
* 将代码转换为`AMD` | ||
* 在cjsify的基础上进行define包裹 | ||
* 如果代码是`CMD`不做修改,因为`AMD`兼容这种写法 | ||
* ranma.cmdify(code:String):String | ||
<br/>将代码转换为CMD | ||
<br/>在cjsify的基础上进行define包裹 | ||
<br/>如果code是AMD类型,会进行依赖转化——即将factory的参数改为CMD的require, exports, module固定参数,同时依赖变为require变量声明 | ||
* cmdify(code:String):String | ||
* 将代码转换为`CMD` | ||
* 在cjsify的基础上进行define包裹 | ||
* 如果code是`AMD`类型,会进行依赖转化——即将factory的参数改为`CMD`的require, exports, module固定参数,同时依赖变为require变量声明 | ||
* ranma.type.isCommonJS(code:String):Boolean | ||
<br/>code是否是CommonJS | ||
* type.isCommonJS(code:String):Boolean | ||
* code是否是`CommonJS` | ||
* ranma.type.isAMD(code:String):Boolean | ||
<br/>code是否是AMD | ||
* type.isAMD(code:String):Boolean | ||
* code是否是`AMD` | ||
* ranma.type.isCMD(code:String):Boolean | ||
<br/>code是否是CMD | ||
* type.isCMD(code:String):Boolean | ||
* code是否是`CMD` | ||
* type.isModule(code:String):Boolean | ||
* code是否是`es6 module` | ||
#### AMD和CMD的区分依据 | ||
* 是否出现define.amd的判断 | ||
* factory的参数是否为固定的require, exports, module | ||
* 是否出现`define.amd`的判断 | ||
* factory的参数是否为固定的`require, exports, module` | ||
@@ -51,0 +56,0 @@ ## License |
@@ -123,2 +123,23 @@ var ranma = require('../'); | ||
}); | ||
it('es6 module', function() { | ||
var type = ranma.type.analyse('module a from "a"'); | ||
expect(type.isCommonJS).to.not.ok(); | ||
expect(type.isAMD).to.not.ok(); | ||
expect(type.isCMD).to.not.ok(); | ||
expect(type.isModule).to.ok(); | ||
}); | ||
it('es6 export', function() { | ||
var type = ranma.type.analyse('export var a = 1'); | ||
expect(type.isCommonJS).to.not.ok(); | ||
expect(type.isAMD).to.not.ok(); | ||
expect(type.isCMD).to.not.ok(); | ||
expect(type.isModule).to.ok(); | ||
}); | ||
it('es6 import', function() { | ||
var type = ranma.type.analyse('import a from "a"'); | ||
expect(type.isCommonJS).to.not.ok(); | ||
expect(type.isAMD).to.not.ok(); | ||
expect(type.isCMD).to.not.ok(); | ||
expect(type.isModule).to.ok(); | ||
}); | ||
it('#isCommonJs', function() { | ||
@@ -133,2 +154,5 @@ expect(ranma.type.isCommonJS('exports.a = 1;')).to.be.ok(); | ||
}); | ||
it('#isModule', function() { | ||
expect(ranma.type.isModule('export default a')).to.be.ok(); | ||
}); | ||
}); | ||
@@ -240,2 +264,22 @@ describe('cjsify', function() { | ||
}); | ||
it('commonjs', function() { | ||
var res = ranma.cjsify('module.exports = a;'); | ||
expect(res).to.eql('module.exports = a;'); | ||
}); | ||
it('cmd', function() { | ||
var res = ranma.cjsify('define(function(require, exports, module) {module.exports = a;});'); | ||
expect(res).to.eql('module.exports = a;;'); | ||
}); | ||
it('cmd with deps', function() { | ||
var res = ranma.cjsify('define(["b"], function(require, exports, module) {var b = require("b");module.exports = a;});'); | ||
expect(res).to.eql('var b = require("b");module.exports = a;;'); | ||
}); | ||
it('es6 module', function() { | ||
var res = ranma.cjsify('module a from "a"'); | ||
expect(res).to.eql('var a=require("a");'); | ||
}); | ||
it('es6 not module', function() { | ||
var res = ranma.cjsify('var [a] = [1]'); | ||
expect(res).to.eql('var a;!function(){var _0_= [1];a=_0_[0]}();module.exports = a;'); | ||
}); | ||
}); | ||
@@ -267,16 +311,10 @@ describe('cmdify', function() { | ||
}); | ||
}); | ||
describe('cjsify', function() { | ||
it('commonjs', function() { | ||
var res = ranma.cjsify('module.exports = a;'); | ||
expect(res).to.eql('module.exports = a;'); | ||
it('es6 module', function() { | ||
var res = ranma.cmdify('module a from "a"'); | ||
expect(res).to.eql('define(function(require, exports, module) {var a=require("a");});'); | ||
}); | ||
it('cmd', function() { | ||
var res = ranma.cjsify('define(function(require, exports, module) {module.exports = a;});'); | ||
expect(res).to.eql('module.exports = a;;'); | ||
it('es6 not module', function() { | ||
var res = ranma.cmdify('var [a] = [1]'); | ||
expect(res).to.eql('define(function(require, exports, module) {var a;!function(){var _0_= [1];a=_0_[0]}();module.exports = a;});'); | ||
}); | ||
it('cmd with deps', function() { | ||
var res = ranma.cjsify('define(["b"], function(require, exports, module) {var b = require("b");module.exports = a;});'); | ||
expect(res).to.eql('var b = require("b");module.exports = a;;'); | ||
}); | ||
}); | ||
@@ -283,0 +321,0 @@ describe('amdify', function() { |
50
type.js
var homunculus = require('homunculus'); | ||
var Token = homunculus.getClass('token'); | ||
var JsNode = homunculus.getClass('node', 'js'); | ||
var Es6Node = homunculus.getClass('node', 'es6'); | ||
var jsdc = require('jsdc'); | ||
var exist = require('./exist'); | ||
@@ -10,2 +13,3 @@ | ||
var isCMD; | ||
var isModule; | ||
@@ -83,14 +87,31 @@ function analyse(context) { | ||
isCMD = false; | ||
isModule = false; | ||
var context = homunculus.getContext('js'); | ||
context.parse(code); | ||
//分析上下文 | ||
analyse(context); | ||
var ast; | ||
jsdc.reset(); | ||
var es6 = jsdc.parse(code); | ||
//jsdc编译不会动es5部分,只要有修改就是es6语法 | ||
if(code && es6 != code) { | ||
ast = jsdc.ast(); | ||
if(ast.first() && ast.first().name() == Es6Node.MODULEBODY) { | ||
isModule = true; | ||
} | ||
} | ||
else { | ||
var context = homunculus.getContext('js'); | ||
context.parse(code); | ||
//分析上下文 | ||
analyse(context); | ||
ast = context.parser.ast(); | ||
} | ||
return { | ||
'code': es6, | ||
'es6': code && es6 != code, | ||
'isCommonJS': isCommonJS, | ||
'isAMD': isAMD, | ||
'isCMD': isCMD, | ||
'context': context, | ||
'ast': context.parser.ast() | ||
'isModule': isModule, | ||
'context': context || null, | ||
'ast': ast | ||
}; | ||
@@ -100,5 +121,3 @@ }; | ||
exports.isCommonJS = function(code) { | ||
if(code) { | ||
exports.analyse(code); | ||
} | ||
exports.analyse(code || ''); | ||
return isCommonJS; | ||
@@ -108,5 +127,3 @@ }; | ||
exports.isAMD = function(code) { | ||
if(code) { | ||
exports.analyse(code); | ||
} | ||
exports.analyse(code || ''); | ||
return isAMD; | ||
@@ -116,6 +133,9 @@ }; | ||
exports.isCMD = function(code) { | ||
if(code) { | ||
exports.analyse(code); | ||
} | ||
exports.analyse(code || ''); | ||
return isCMD; | ||
}; | ||
exports.isModule = function(code) { | ||
exports.analyse(code || ''); | ||
return isModule; | ||
}; |
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
2112752
56798
56
2
+ Addedjsdc@^0.4.2
+ Addedhomunculus@0.3.80.7.9(transitive)
+ Addedjsdc@0.4.16(transitive)
- Removedhomunculus@0.2.9(transitive)
Updatedhomunculus@^0.3.7