babel-plugin-transform-amd-to-commonjs
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -39,3 +39,12 @@ 'use strict'; | ||
const createRequireExpression = (dependencyNode, variableName) => { | ||
const createDependencyInjectionExpression = (dependencyNode, variableName) => { | ||
if (t.isStringLiteral(dependencyNode) && [MODULE, EXPORTS, REQUIRE].includes(dependencyNode.value)) { | ||
// In case of the AMD keywords, only create an expression if the variable name | ||
// does not match the keyword. This to prevent 'require = require' statements. | ||
if (variableName && variableName.name !== dependencyNode.value) { | ||
return t.variableDeclaration('var', [t.variableDeclarator(variableName, t.identifier(dependencyNode.value))]); | ||
} | ||
return undefined; | ||
} | ||
const requireCall = t.callExpression(t.identifier(REQUIRE), [dependencyNode]); | ||
@@ -74,5 +83,5 @@ if (variableName) { | ||
const createFactoryReplacementExpression = (factory, requireExpressions) => { | ||
const createFactoryReplacementExpression = (factory, dependencyInjections) => { | ||
if (t.isFunctionExpression(factory)) { | ||
return t.functionExpression(null, [], t.blockStatement(requireExpressions.concat(factory.body.body))); | ||
return t.functionExpression(null, [], t.blockStatement(dependencyInjections.concat(factory.body.body))); | ||
} | ||
@@ -86,3 +95,3 @@ let bodyStatement; | ||
} | ||
return t.arrowFunctionExpression([], t.blockStatement(requireExpressions.concat(bodyStatement))); | ||
return t.arrowFunctionExpression([], t.blockStatement(dependencyInjections.concat(bodyStatement))); | ||
}; | ||
@@ -95,3 +104,3 @@ | ||
createModuleExportsResultCheck, | ||
createRequireExpression, | ||
createDependencyInjectionExpression, | ||
isSimplifiedCommonJSWrapper, | ||
@@ -98,0 +107,0 @@ isModuleOrExportsInjected, |
@@ -20,3 +20,3 @@ 'use strict'; | ||
isSimplifiedCommonJSWrapper = _createHelpers.isSimplifiedCommonJSWrapper, | ||
createRequireExpression = _createHelpers.createRequireExpression, | ||
createDependencyInjectionExpression = _createHelpers.createDependencyInjectionExpression, | ||
createModuleExportsAssignmentExpression = _createHelpers.createModuleExportsAssignmentExpression, | ||
@@ -39,3 +39,3 @@ createModuleExportsResultCheck = _createHelpers.createModuleExportsResultCheck, | ||
const ExpressionStatement = path => { | ||
const ExpressionStatement = (path, { opts }) => { | ||
const node = path.node, | ||
@@ -47,2 +47,4 @@ parent = path.parent; | ||
const options = Object.assign({ restrictToTopLevelDefine: true }, opts); | ||
const name = node.expression.callee.name; | ||
@@ -52,3 +54,3 @@ | ||
if (isDefineCall && !t.isProgram(parent)) return; | ||
if (isDefineCall && options.restrictToTopLevelDefine && !t.isProgram(parent)) return; | ||
@@ -68,5 +70,3 @@ const argumentDecoder = argumentDecoders[name]; | ||
const isFunctionFactory = isFunctionExpression(factory); | ||
const requireExpressions = []; | ||
// Order is important here for the simplified commonjs wrapper | ||
const keywords = [REQUIRE, EXPORTS, MODULE]; | ||
const dependencyInjections = []; | ||
@@ -76,9 +76,9 @@ if (dependencyList) { | ||
const explicitRequires = dependencyParameterPairs.filter(([dependency]) => { | ||
return !t.isStringLiteral(dependency) || !keywords.includes(dependency.value); | ||
}).map(([dependency, paramName]) => { | ||
return createRequireExpression(dependency, paramName); | ||
const dependencyInjectionExpressions = dependencyParameterPairs.map(([dependency, paramName]) => { | ||
return createDependencyInjectionExpression(dependency, paramName); | ||
}).filter(dependencyInjection => { | ||
return dependencyInjection !== undefined; | ||
}); | ||
requireExpressions.push(...explicitRequires); | ||
dependencyInjections.push(...dependencyInjectionExpressions); | ||
} | ||
@@ -88,3 +88,3 @@ | ||
const factoryArity = factory.params.length; | ||
let replacementFuncExpr = createFactoryReplacementExpression(factory, requireExpressions); | ||
let replacementFuncExpr = createFactoryReplacementExpression(factory, dependencyInjections); | ||
let replacementCallExprParams = []; | ||
@@ -94,3 +94,7 @@ | ||
replacementFuncExpr = factory; | ||
replacementCallExprParams = keywords.slice(0, factoryArity).map(a => t.identifier(a)); | ||
// Order is important here for the simplified commonjs wrapper | ||
const amdKeywords = [REQUIRE, EXPORTS, MODULE]; | ||
replacementCallExprParams = amdKeywords.slice(0, factoryArity).map(keyword => t.identifier(keyword)); | ||
} | ||
@@ -112,6 +116,6 @@ | ||
const exportExpression = createModuleExportsAssignmentExpression(factory); | ||
const nodes = requireExpressions.concat(exportExpression); | ||
const nodes = dependencyInjections.concat(exportExpression); | ||
path.replaceWithMultiple(nodes); | ||
} else { | ||
path.replaceWithMultiple(requireExpressions); | ||
path.replaceWithMultiple(dependencyInjections); | ||
} | ||
@@ -118,0 +122,0 @@ }; |
{ | ||
"name": "babel-plugin-transform-amd-to-commonjs", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Transforms AMD code to CommonJS", | ||
@@ -35,7 +35,7 @@ "main": "build/index.js", | ||
"devDependencies": { | ||
"babel-cli": "^6.18.0", | ||
"babel-core": "^6.20.0", | ||
"babel-jest": "^22.0.1", | ||
"babel-preset-env": "^1.6.1", | ||
"eslint": "^4.5.0", | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.3", | ||
"babel-jest": "^23.2.0", | ||
"babel-preset-env": "^1.7.0", | ||
"eslint": "^5.0.1", | ||
"eslint-config-msrose": "^1.0.0", | ||
@@ -45,6 +45,6 @@ "eslint-config-prettier": "^2.3.0", | ||
"eslint-plugin-prettier": "^2.2.0", | ||
"jest": "^22.0.1", | ||
"jest-diff": "^22.0.1", | ||
"jest": "^23.2.0", | ||
"jest-diff": "^23.2.0", | ||
"prettier": "^1.6.1", | ||
"regenerator-runtime": "^0.11.0" | ||
"regenerator-runtime": "^0.12.0" | ||
}, | ||
@@ -51,0 +51,0 @@ "jest": { |
@@ -85,2 +85,16 @@ # babel-plugin-transform-amd-to-commonjs | ||
## Options | ||
Specify options in your .babelrc: | ||
``` | ||
{ | ||
"plugins": [ | ||
["transform-amd-to-commonjs", { "restrictToTopLevelDefine": true }] | ||
] | ||
} | ||
``` | ||
- `restrictToTopLevelDefine`: (default: `true`) When `true`, only transform `define` calls that appear at the top-level of a program. Set to `false` to transform _all_ calls to `define`. | ||
## Details | ||
@@ -100,3 +114,3 @@ | ||
- Only _top-level_ calls to a `define` function will be transformed. | ||
- By default, only _top-level_ calls to a `define` function will be transformed. Use the `restrictToTopLevelDefine` option to transform calls that are not at the top-level. | ||
- _All_ calls to `require` where it is given an array of dependencies as its first argument will be transformed. | ||
@@ -116,3 +130,3 @@ - Explicitly requiring `require`, `module`, and `exports` in an AMD module will not generate a call to require, but instead defer to the global require, module, and exports assumed to be in the CommonJS environment you are transforming to. | ||
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. | ||
@@ -119,0 +133,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
18531
185
194