From 76991cce528dc1300bfa4382011c97470e1773b6 Mon Sep 17 00:00:00 2001 From: kekee000 Date: Tue, 27 Jan 2015 23:51:53 +0800 Subject: [PATCH] modify amd2module --- node/amd2module.js | 56 ++++++++++++++++++++++++++++++++++++---------- package.json | 11 +++++---- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/node/amd2module.js b/node/amd2module.js index 0b25c05..05111da 100644 --- a/node/amd2module.js +++ b/node/amd2module.js @@ -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,18 +127,39 @@ function replaceDefine(ast) { var index = parent.body.indexOf(node); Array.prototype.splice.apply(parent.body, [index, 1].concat(body)); + return body[0]; } + } return node; } - } ); + }); 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的绝对路径为相对路径 * @@ -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); }; diff --git a/package.json b/package.json index b053b44..25ba55b 100644 --- a/package.json +++ b/package.json @@ -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" }