modify amd2module

This commit is contained in:
kekee000 2015-01-27 23:51:53 +08:00
parent b9b14522e3
commit 76991cce52
2 changed files with 49 additions and 18 deletions

View File

@ -52,6 +52,10 @@ function getAst(code) {
try {
ast = esprima.parse(code, {
// raw: true,
// tokens: true,
// range: true,
// comment: true
});
} catch (ex) {
throw 'can\'t parse amd code';
@ -93,11 +97,12 @@ function replaceDefine(ast) {
estraverse.replace(ast, {
enter: function (node, parent) {
if ( node.type == SYNTAX.ExpressionStatement
&& node.expression.type == SYNTAX.CallExpression
&& node.expression.callee.name == 'define'
if ( node.type === SYNTAX.ExpressionStatement
&& node.expression.type === SYNTAX.CallExpression
&& node.expression.callee.name === 'define'
) {
var factory = getDefineFactory(node.expression);
// define('xxx', {})
if (factory.type === SYNTAX.ObjectExpression) {
var exportStatment = getExportStatement();
@ -108,10 +113,11 @@ function replaceDefine(ast) {
// define(function() {})
else if (factory.type === SYNTAX.FunctionExpression){
var body = factory.body.body;
// 替换return
for (var i = body.length - 1; i >=0; i--) {
if (body[i].type == SYNTAX.ReturnStatement) {
if (body[i].type === SYNTAX.ReturnStatement) {
var exportStatment = getExportStatement();
exportStatment.expression.right = body[i].argument;
body.splice(i, 1, exportStatment);
@ -121,8 +127,10 @@ function replaceDefine(ast) {
var index = parent.body.indexOf(node);
Array.prototype.splice.apply(parent.body, [index, 1].concat(body));
return body[0];
}
}
return node;
@ -133,6 +141,25 @@ function replaceDefine(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的绝对路径为相对路径
*
@ -145,10 +172,10 @@ function replaceRequire(ast, codeDepth) {
estraverse.replace(ast, {
enter: function (node, parent) {
if ( node.type == SYNTAX.CallExpression
&& node.callee.name == 'require'
if ( node.type === SYNTAX.CallExpression
&& node.callee.name === 'require'
&& node.arguments.length
&& node.arguments[0].type == 'Literal'
&& node.arguments[0].type === 'Literal'
) {
var argument = node.arguments[0];
if (REG_TOP_MODULE.test(argument.value)) {
@ -170,7 +197,11 @@ function replaceRequire(ast, codeDepth) {
* @return {string} 生成后的代码
*/
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) {
@ -180,7 +211,8 @@ module.exports = function (code, codeDepth) {
}
var ast = getAst(code);
var replacedAst = replaceRequire(ast, codeDepth);
replacedAst = replaceDefine(replacedAst);
return genCommonJS(replacedAst);
ast = replaceRequire(ast, codeDepth);
ast = replaceDefine(ast);
// ast = replaceComments(ast);
return genCommonJS(ast);
};

View File

@ -2,7 +2,10 @@
"name": "fonteditor",
"version": "0.0.1",
"description": "ttf font editor",
"dependencies": {
"devDependencies": {
"esprima": "~1.1.1",
"estraverse": "~1.5.0",
"escodegen": "~1.6.0"
},
"repository": {
"type": "git",
@ -18,9 +21,5 @@
}
],
"author": "",
"license": "MIT",
"devDependencies": {
"esprima": "~1.1.1",
"estraverse": "~1.5.0",
}
"license": "MIT"
}