modify amd2module
This commit is contained in:
parent
b9b14522e3
commit
76991cce52
@ -52,6 +52,10 @@ function getAst(code) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
ast = esprima.parse(code, {
|
ast = esprima.parse(code, {
|
||||||
|
// raw: true,
|
||||||
|
// tokens: true,
|
||||||
|
// range: true,
|
||||||
|
// comment: true
|
||||||
});
|
});
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
throw 'can\'t parse amd code';
|
throw 'can\'t parse amd code';
|
||||||
@ -93,11 +97,12 @@ function replaceDefine(ast) {
|
|||||||
estraverse.replace(ast, {
|
estraverse.replace(ast, {
|
||||||
enter: function (node, parent) {
|
enter: function (node, parent) {
|
||||||
|
|
||||||
if ( node.type == SYNTAX.ExpressionStatement
|
if ( node.type === SYNTAX.ExpressionStatement
|
||||||
&& node.expression.type == SYNTAX.CallExpression
|
&& node.expression.type === SYNTAX.CallExpression
|
||||||
&& node.expression.callee.name == 'define'
|
&& node.expression.callee.name === 'define'
|
||||||
) {
|
) {
|
||||||
var factory = getDefineFactory(node.expression);
|
var factory = getDefineFactory(node.expression);
|
||||||
|
|
||||||
// define('xxx', {})
|
// define('xxx', {})
|
||||||
if (factory.type === SYNTAX.ObjectExpression) {
|
if (factory.type === SYNTAX.ObjectExpression) {
|
||||||
var exportStatment = getExportStatement();
|
var exportStatment = getExportStatement();
|
||||||
@ -108,10 +113,11 @@ function replaceDefine(ast) {
|
|||||||
|
|
||||||
// define(function() {})
|
// define(function() {})
|
||||||
else if (factory.type === SYNTAX.FunctionExpression){
|
else if (factory.type === SYNTAX.FunctionExpression){
|
||||||
|
|
||||||
var body = factory.body.body;
|
var body = factory.body.body;
|
||||||
// 替换return
|
// 替换return
|
||||||
for (var i = body.length - 1; i >=0; i--) {
|
for (var i = body.length - 1; i >=0; i--) {
|
||||||
if (body[i].type == SYNTAX.ReturnStatement) {
|
if (body[i].type === SYNTAX.ReturnStatement) {
|
||||||
var exportStatment = getExportStatement();
|
var exportStatment = getExportStatement();
|
||||||
exportStatment.expression.right = body[i].argument;
|
exportStatment.expression.right = body[i].argument;
|
||||||
body.splice(i, 1, exportStatment);
|
body.splice(i, 1, exportStatment);
|
||||||
@ -121,18 +127,39 @@ function replaceDefine(ast) {
|
|||||||
|
|
||||||
var index = parent.body.indexOf(node);
|
var index = parent.body.indexOf(node);
|
||||||
Array.prototype.splice.apply(parent.body, [index, 1].concat(body));
|
Array.prototype.splice.apply(parent.body, [index, 1].concat(body));
|
||||||
|
|
||||||
return body[0];
|
return body[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
} );
|
});
|
||||||
|
|
||||||
return ast;
|
return ast;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 去除生成的代码缩进
|
||||||
|
*
|
||||||
|
* @param {Object} ast ast
|
||||||
|
* @return {Object} ast
|
||||||
|
*/
|
||||||
|
function replaceComments(ast) {
|
||||||
|
if (ast.comments && ast.comments.length) {
|
||||||
|
ast.comments.forEach(function (comment) {
|
||||||
|
if (comment.type === 'Block') {
|
||||||
|
// 去除缩进
|
||||||
|
comment.value = comment.value.replace(/ /g, '');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return ast;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 替换require的绝对路径为相对路径
|
* 替换require的绝对路径为相对路径
|
||||||
*
|
*
|
||||||
@ -145,10 +172,10 @@ function replaceRequire(ast, codeDepth) {
|
|||||||
estraverse.replace(ast, {
|
estraverse.replace(ast, {
|
||||||
enter: function (node, parent) {
|
enter: function (node, parent) {
|
||||||
|
|
||||||
if ( node.type == SYNTAX.CallExpression
|
if ( node.type === SYNTAX.CallExpression
|
||||||
&& node.callee.name == 'require'
|
&& node.callee.name === 'require'
|
||||||
&& node.arguments.length
|
&& node.arguments.length
|
||||||
&& node.arguments[0].type == 'Literal'
|
&& node.arguments[0].type === 'Literal'
|
||||||
) {
|
) {
|
||||||
var argument = node.arguments[0];
|
var argument = node.arguments[0];
|
||||||
if (REG_TOP_MODULE.test(argument.value)) {
|
if (REG_TOP_MODULE.test(argument.value)) {
|
||||||
@ -170,7 +197,11 @@ function replaceRequire(ast, codeDepth) {
|
|||||||
* @return {string} 生成后的代码
|
* @return {string} 生成后的代码
|
||||||
*/
|
*/
|
||||||
function genCommonJS(ast) {
|
function genCommonJS(ast) {
|
||||||
return escodegen.generate(ast);
|
|
||||||
|
// ast = escodegen.attachComments(ast, ast.comments, ast.tokens);
|
||||||
|
return escodegen.generate(ast, {
|
||||||
|
// comment: true
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function (code, codeDepth) {
|
module.exports = function (code, codeDepth) {
|
||||||
@ -180,7 +211,8 @@ module.exports = function (code, codeDepth) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var ast = getAst(code);
|
var ast = getAst(code);
|
||||||
var replacedAst = replaceRequire(ast, codeDepth);
|
ast = replaceRequire(ast, codeDepth);
|
||||||
replacedAst = replaceDefine(replacedAst);
|
ast = replaceDefine(ast);
|
||||||
return genCommonJS(replacedAst);
|
// ast = replaceComments(ast);
|
||||||
|
return genCommonJS(ast);
|
||||||
};
|
};
|
||||||
|
11
package.json
11
package.json
@ -2,7 +2,10 @@
|
|||||||
"name": "fonteditor",
|
"name": "fonteditor",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"description": "ttf font editor",
|
"description": "ttf font editor",
|
||||||
"dependencies": {
|
"devDependencies": {
|
||||||
|
"esprima": "~1.1.1",
|
||||||
|
"estraverse": "~1.5.0",
|
||||||
|
"escodegen": "~1.6.0"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -18,9 +21,5 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "MIT",
|
"license": "MIT"
|
||||||
"devDependencies": {
|
|
||||||
"esprima": "~1.1.1",
|
|
||||||
"estraverse": "~1.5.0",
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user