babel-plugin-transform-amd-to-commonjs
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -28,7 +28,3 @@ 'use strict'; | ||
const decodeRequireArguments = argNodes => { | ||
if (argNodes.length === 1) { | ||
return { dependencyList: argNodes[0] }; | ||
} else { | ||
return { dependencyList: argNodes[0], factory: argNodes[1] }; | ||
} | ||
return { dependencyList: argNodes[0], factory: argNodes[1] }; | ||
}; | ||
@@ -57,4 +53,9 @@ | ||
// https://github.com/requirejs/requirejs/wiki/differences-between-the-simplified-commonjs-wrapper-and-standard-amd-define | ||
const isSimplifiedCommonJSWrapper = (dependencyList, factoryArity) => { | ||
return !dependencyList && factoryArity > 0; | ||
}; | ||
const isSimplifiedCommonJSWrapperWithModuleOrExports = (dependencyList, factoryArity) => { | ||
return !dependencyList && factoryArity > 1; | ||
return isSimplifiedCommonJSWrapper(dependencyList, factoryArity) && factoryArity > 1; | ||
}; | ||
@@ -66,2 +67,24 @@ | ||
const getUniqueIdentifier = (scope, name) => { | ||
return scope.hasOwnBinding(name) ? scope.generateUidIdentifier(name) : t.identifier(name); | ||
}; | ||
const isFunctionExpression = factory => { | ||
return t.isFunctionExpression(factory) || t.isArrowFunctionExpression(factory); | ||
}; | ||
const createFactoryReplacementExpression = (factory, requireExpressions) => { | ||
if (t.isFunctionExpression(factory)) { | ||
return t.functionExpression(null, [], t.blockStatement(requireExpressions.concat(factory.body.body))); | ||
} | ||
let bodyStatement; | ||
if (t.isBlockStatement(factory.body)) { | ||
bodyStatement = factory.body.body; | ||
} else { | ||
// implicit return arrow function | ||
bodyStatement = t.returnStatement(factory.body); | ||
} | ||
return t.arrowFunctionExpression([], t.blockStatement(requireExpressions.concat(bodyStatement))); | ||
}; | ||
return { | ||
@@ -73,4 +96,8 @@ decodeDefineArguments, | ||
createRequireExpression, | ||
isModuleOrExportsInjected | ||
isSimplifiedCommonJSWrapper, | ||
isModuleOrExportsInjected, | ||
getUniqueIdentifier, | ||
isFunctionExpression, | ||
createFactoryReplacementExpression | ||
}; | ||
}; |
@@ -19,5 +19,9 @@ 'use strict'; | ||
isModuleOrExportsInjected = _createHelpers.isModuleOrExportsInjected, | ||
isSimplifiedCommonJSWrapper = _createHelpers.isSimplifiedCommonJSWrapper, | ||
createRequireExpression = _createHelpers.createRequireExpression, | ||
createModuleExportsAssignmentExpression = _createHelpers.createModuleExportsAssignmentExpression, | ||
createModuleExportsResultCheck = _createHelpers.createModuleExportsResultCheck; | ||
createModuleExportsResultCheck = _createHelpers.createModuleExportsResultCheck, | ||
getUniqueIdentifier = _createHelpers.getUniqueIdentifier, | ||
isFunctionExpression = _createHelpers.isFunctionExpression, | ||
createFactoryReplacementExpression = _createHelpers.createFactoryReplacementExpression; | ||
@@ -60,3 +64,3 @@ | ||
const isFunctionFactory = t.isFunctionExpression(factory); | ||
const isFunctionFactory = isFunctionExpression(factory); | ||
const requireExpressions = []; | ||
@@ -70,3 +74,3 @@ // Order is important here for the simplified commonjs wrapper | ||
const explicitRequires = dependencyParameterPairs.filter(([dependency]) => { | ||
return !t.isStringLiteral(dependency) || keywords.indexOf(dependency.value) === -1; | ||
return !t.isStringLiteral(dependency) || !keywords.includes(dependency.value); | ||
}).map(([dependency, paramName]) => { | ||
@@ -81,8 +85,6 @@ return createRequireExpression(dependency, paramName); | ||
const factoryArity = factory.params.length; | ||
let replacementFuncExpr = t.functionExpression(null, [], t.blockStatement(requireExpressions.concat(factory.body.body))); | ||
let replacementFuncExpr = createFactoryReplacementExpression(factory, requireExpressions); | ||
let replacementCallExprParams = []; | ||
// https://github.com/requirejs/requirejs/wiki/differences-between-the-simplified-commonjs-wrapper-and-standard-amd-define | ||
const isSimplifiedCommonJSWrapper = !dependencyList && factoryArity > 0; | ||
if (isSimplifiedCommonJSWrapper) { | ||
if (isSimplifiedCommonJSWrapper(dependencyList, factoryArity)) { | ||
replacementFuncExpr = factory; | ||
@@ -98,3 +100,3 @@ replacementCallExprParams = keywords.slice(0, factoryArity).map(a => t.identifier(a)); | ||
} else { | ||
const resultCheckIdentifier = path.scope.hasOwnBinding(AMD_DEFINE_RESULT) ? path.scope.generateUidIdentifier(AMD_DEFINE_RESULT) : t.identifier(AMD_DEFINE_RESULT); | ||
const resultCheckIdentifier = getUniqueIdentifier(path.scope, AMD_DEFINE_RESULT); | ||
path.replaceWithMultiple(createModuleExportsResultCheck(factoryReplacement, resultCheckIdentifier)); | ||
@@ -101,0 +103,0 @@ } |
{ | ||
"name": "babel-plugin-transform-amd-to-commonjs", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Transforms AMD code to CommonJS", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -110,7 +110,7 @@ # babel-plugin-transform-amd-to-commonjs | ||
- Versions >= 0.2.1 and < 1.0.0 support Node.js 4. | ||
1.0.0 and above only support Node.js >= 6. | ||
To upgrade to 1.0.0, first upgrade to Node.js >= 6. | ||
1.0.0 and above only support Node.js 6 and above. | ||
To upgrade to >= 1.0.0, first upgrade to Node.js >= 6. | ||
- If everything works fine with < 1.0.0, you should just be able to drop in >= 1.0.0 after upgrading Node.js. | ||
If you have any issues, there is one more edge-case breaking change that _might_ be affecting you (but probably is not): | ||
- 1.0.0 accounts for the case where you're using a combination of return statements and module/exports to define the exports of your AMD modules. | ||
- >= 1.0.0 accounts for the case where you're using a combination of return statements and module/exports to define the exports of your AMD modules. | ||
Earlier versions don't account for this case, so if you're upgrading, make sure that each AMD module only uses either return statements _or_ module/exports to define its exports. | ||
@@ -117,0 +117,0 @@ See [#26](https://github.com/msrose/babel-plugin-transform-amd-to-commonjs/pull/26) and the [caveats](#injecting-require-module-or-exports-as-dependencies) section of the README for more details. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
17352
176
0