babel-plugin-transform-amd-to-commonjs
Advanced tools
Comparing version 0.1.0 to 0.2.0
67
index.js
@@ -24,2 +24,23 @@ module.exports = ({ types: t }) => { | ||
const createModuleExportsAssignmentExpression = (value) => { | ||
return t.expressionStatement( | ||
t.assignmentExpression( | ||
'=', | ||
t.memberExpression( | ||
t.identifier('module'), t.identifier('exports') | ||
), | ||
value | ||
) | ||
); | ||
}; | ||
const createRequireExpression = (dependencyNode, variableName) => { | ||
const requireCall = t.callExpression(t.identifier('require'), [dependencyNode]); | ||
if(variableName) { | ||
return t.variableDeclaration('var', [t.variableDeclarator(variableName, requireCall)]); | ||
} else { | ||
return t.expressionStatement(requireCall); | ||
} | ||
}; | ||
return { | ||
@@ -64,10 +85,3 @@ visitor: { | ||
const paramName = factory && factory.params[i]; | ||
const requireCall = t.callExpression(t.identifier('require'), [el]); | ||
if(paramName) { | ||
requireExpressions.push(t.variableDeclaration('var', [ | ||
t.variableDeclarator(paramName, requireCall) | ||
])); | ||
} else { | ||
requireExpressions.push(t.expressionStatement(requireCall)); | ||
} | ||
requireExpressions.push(createRequireExpression(el, paramName)); | ||
}); | ||
@@ -77,23 +91,20 @@ } | ||
if(factory) { | ||
injectsModuleOrExports = injectsModuleOrExports || | ||
!dependencyList && factory.params.filter( | ||
(node) => ['module', 'exports'].includes(node.name) | ||
).length; | ||
const factoryReplacement = t.callExpression( | ||
t.functionExpression(null, [], t.blockStatement( | ||
requireExpressions.concat(factory.body.body) | ||
)), [] | ||
); | ||
const factoryArity = factory.params.length; | ||
let replacementFuncExpr = t.functionExpression(null, [], t.blockStatement( | ||
requireExpressions.concat(factory.body.body) | ||
)); | ||
let replacementCallExprParams = []; | ||
const hasParamsForInjection = !dependencyList && factoryArity > 0; | ||
if(hasParamsForInjection) { | ||
replacementFuncExpr = factory; | ||
const identifiers = ['require', 'exports', 'module']; | ||
replacementCallExprParams = identifiers.slice(0, factoryArity).map(a => t.identifier(a)); | ||
} | ||
const factoryReplacement = t.callExpression(replacementFuncExpr, replacementCallExprParams); | ||
injectsModuleOrExports = injectsModuleOrExports || !dependencyList && factoryArity > 1; | ||
if(name === 'define' && !injectsModuleOrExports) { | ||
path.replaceWith( | ||
t.expressionStatement( | ||
t.assignmentExpression( | ||
'=', | ||
t.memberExpression( | ||
t.identifier('module'), t.identifier('exports') | ||
), | ||
factoryReplacement | ||
) | ||
) | ||
); | ||
path.replaceWith(createModuleExportsAssignmentExpression(factoryReplacement)); | ||
} else { | ||
@@ -100,0 +111,0 @@ path.replaceWith(factoryReplacement); |
{ | ||
"name": "babel-plugin-transform-amd-to-commonjs", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Transforms AMD code to CommonJS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# babel-plugin-transform-amd-to-commonjs | ||
[![npm version](https://badge.fury.io/js/babel-plugin-transform-amd-to-commonjs.svg)](https://badge.fury.io/js/babel-plugin-transform-amd-to-commonjs) | ||
[![Build Status](https://travis-ci.org/msrose/babel-plugin-transform-amd-to-commonjs.svg?branch=master)](https://travis-ci.org/msrose/babel-plugin-transform-amd-to-commonjs) | ||
@@ -4,0 +5,0 @@ |
11335
99
173