Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-transform-amd-to-commonjs

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-amd-to-commonjs - npm Package Compare versions

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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc