Skip to content
Permalink
Browse files

Transformer: visitAMDModules and extract dependencies.

  • Loading branch information...
iamdustan committed Dec 17, 2014
1 parent 2f21464 commit d1dc01c30f160172378cf662c0868cbd6ffe19be
Showing with 83 additions and 17 deletions.
  1. +27 −15 lib/generators/cjsrequire.js
  2. +56 −2 lib/transformers/amdToCjs.js
@@ -3,26 +3,38 @@ var b = recast.types.builders;
var n = recast.types.namedTypes;

/**
* Return the AST for `var variable = require(module);`
* @param {String} variable
* @param {String} module]
* Return the AST for `var variable = require(module);` or `require(module);`
* @param {String} module
* @param {String} [identifier]
* @example:
* recast.print(generateCJSExpr('React', 'react/addons').code;
* -> var React = require('react/addons');
*/
module.exports = function generateCJSExpr(variable, module) {
if (typeof module === 'undefined') {
module = variable;
module.exports = function generateCJSExpr(module, identifier) {
if (typeof identifier === 'string' && !n.Identifier.check(identifier)) {
identifier = b.identifier(identifier);
}

var identifier = b.identifier(variable);
return b.variableDeclaration("var", [
b.variableDeclarator(
identifier,
b.callExpression(b.identifier('require'), [
b.literal(module)
])
)
]);
if (!n.Literal.check(module)) {
module = b.literal(module);
}

var requireExpression = b.callExpression(
b.identifier('require'), [
module
]
);

if (identifier) {
return b.variableDeclaration("var", [
b.variableDeclarator(
identifier,
requireExpression
)
]);
}

// console.warn('Unnamed dependency: %s', module.value);
return b.expressionStatement(requireExpression);
}

@@ -1,12 +1,16 @@
var recast = require('recast');
// keep this private and not mutatable
var assert = require('assert');
var generators = require('../generators');
var generateRequire = generators.require;
var generateExports = generators.exports;

module.exports = function(code) {
var ast = recast.parse(code);
recast.visit(ast, module.exports.__visitors);
return recast.print(ast).code;
};

// keep this private and not mutatable
Object.defineProperty(module.exports, '__visitors', {
enumerable: false,
configurable: false,
@@ -20,7 +24,12 @@ Object.defineProperty(module.exports, '__visitors', {
},

visitAMDModule: function(path) {
//console.log('visitAMDModule', path.node.callee.name);
var node = path.node;
var dependencies = this.transformedDependencies(path);
var moduleBody = this.extractModuleBody(path);
var result;

//(dependencies || []).forEach(print);

return this.traverse(path);
},
@@ -33,7 +42,52 @@ Object.defineProperty(module.exports, '__visitors', {
return (n.CallExpression.check(node) &&
name === node.callee.name);
}
},

transformedDependencies: function(path) {
var node = path.node;
var dependencies = this.extractAMDDependencies(path);

if (dependencies) {
return dependencies.map(function(a) {
return generateRequire(a[0], a[1]);
});
}

return null;
},

/**
* @param {NodePath} path AMD Call Expression
* @return {Array} {value: Identifier|String, module: String}
*/
extractAMDDependencies: function(path) {
assert.ok(this.isAMDDefinition(path));

var node = path.node;
// TODO: http://requirejs.org/docs/whyamd.html#namedmodules
if (node.arguments.length < 2) return null;

var dependencies = node.arguments[0];
var factory = node.arguments[1];

var args = node.arguments;

if (n.ArrayExpression.check(dependencies)) {
dependencies = dependencies.elements;
}
else {
// TODO: lookup variable
// dependencies = find(dependencies);
}

return zip(dependencies, factory.params);
}
}),
});

function zip(a, b) {
return a.map(function(a, i) {
return [a, b[i]];
});
}

0 comments on commit d1dc01c

Please sign in to comment.
You can’t perform that action at this time.