@babel/preset-modules
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -22,3 +22,35 @@ "use strict"; | ||
}) => { | ||
function convertAttribute(node) { | ||
// converts a set of JSXAttributes to an Object.assign() call | ||
function convertAttributesAssign(attributes) { | ||
const args = []; | ||
for (let i = 0, current; i < attributes.length; i++) { | ||
const node = attributes[i]; | ||
if (t.isJSXSpreadAttribute(node)) { | ||
// the first attribute is a spread, avoid copying all other attributes onto it | ||
if (i === 0) { | ||
args.push(t.objectExpression([])); | ||
} | ||
current = null; | ||
args.push(node.argument); | ||
} else { | ||
const name = getAttributeName(node); | ||
const value = getAttributeValue(node); | ||
if (!current) { | ||
current = t.objectExpression([]); | ||
args.push(current); | ||
} | ||
current.properties.push(t.objectProperty(name, value)); | ||
} | ||
} | ||
return t.callExpression(t.memberExpression(t.identifier("Object"), t.identifier("assign")), args); | ||
} // Converts a JSXAttribute to the equivalent ObjectExpression property | ||
function convertAttributeSpread(node) { | ||
if (t.isJSXSpreadAttribute(node)) { | ||
@@ -28,2 +60,22 @@ return t.spreadElement(node.argument); | ||
const name = getAttributeName(node); | ||
const value = getAttributeValue(node); | ||
return t.inherits(t.objectProperty(name, value), node); | ||
} // Convert a JSX attribute name to an Object expression property name | ||
function getAttributeName(node) { | ||
if (t.isJSXNamespacedName(node.name)) { | ||
return t.stringLiteral(node.name.namespace.name + ":" + node.name.name.name); | ||
} | ||
if (_esutils.default.keyword.isIdentifierNameES6(node.name.name)) { | ||
return t.identifier(node.name.name); | ||
} | ||
return t.stringLiteral(node.name.name); | ||
} // Convert a JSX attribute value to a JavaScript expression value | ||
function getAttributeValue(node) { | ||
let value = node.value || t.booleanLiteral(true); | ||
@@ -41,21 +93,19 @@ | ||
if (t.isJSXNamespacedName(node.name)) { | ||
node.name = t.stringLiteral(node.name.namespace.name + ":" + node.name.name.name); | ||
} else if (_esutils.default.keyword.isIdentifierNameES6(node.name.name)) { | ||
node.name.type = "Identifier"; | ||
} else { | ||
node.name = t.stringLiteral(node.name.name); | ||
} | ||
return t.inherits(t.objectProperty(node.name, value), node); | ||
return value; | ||
} | ||
return { | ||
name: "transform-hoist-tagged-templates", | ||
name: "transform-jsx-spread", | ||
visitor: { | ||
JSXOpeningElement(path) { | ||
const hasSpread = path.node.attributes.some(t.isJSXSpreadAttribute); // ignore JSX Elements without spread or with lone spread: | ||
JSXOpeningElement(path, state) { | ||
const useSpread = state.opts.useSpread === true; | ||
const hasSpread = path.node.attributes.some(attr => t.isJSXSpreadAttribute(attr)); // ignore JSX Elements without spread or with lone spread: | ||
if (!hasSpread || path.node.attributes.length === 1) return; | ||
path.node.attributes = [t.jsxSpreadAttribute(t.objectExpression(path.node.attributes.map(convertAttribute)))]; | ||
if (useSpread) { | ||
path.node.attributes = [t.jsxSpreadAttribute(t.objectExpression(path.node.attributes.map(convertAttributeSpread)))]; | ||
} else { | ||
path.node.attributes = [t.jsxSpreadAttribute(convertAttributesAssign(path.node.attributes))]; | ||
} | ||
} | ||
@@ -62,0 +112,0 @@ |
{ | ||
"name": "@babel/preset-modules", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "A Babel preset that targets modern browsers by fixing engine bugs.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -145,10 +145,12 @@ # `@babel/preset-modules` | ||
module.exports = { | ||
minifier: [ | ||
new TerserPlugin({ | ||
terserOptions: { | ||
ecma: 8, | ||
safari10: true | ||
} | ||
}) | ||
] | ||
optimization: { | ||
minimizer: [ | ||
new TerserPlugin({ | ||
terserOptions: { | ||
ecma: 8, | ||
safari10: true | ||
} | ||
}) | ||
] | ||
} | ||
}; | ||
@@ -158,2 +160,2 @@ ``` | ||
All of the above configurations also apply to [uglify-es](https://github.com/mishoo/UglifyJS2/tree/harmony). | ||
UglifyJS (2.x and prior) does not support modern JavaScript, so it cannot be used in conjuction with this preset. | ||
UglifyJS (2.x and prior) does not support modern JavaScript, so it cannot be used in conjunction with this preset. |
@@ -13,3 +13,32 @@ import esutils from "esutils"; | ||
export default ({ types: t }) => { | ||
function convertAttribute(node) { | ||
// converts a set of JSXAttributes to an Object.assign() call | ||
function convertAttributesAssign(attributes) { | ||
const args = []; | ||
for (let i = 0, current; i < attributes.length; i++) { | ||
const node = attributes[i]; | ||
if (t.isJSXSpreadAttribute(node)) { | ||
// the first attribute is a spread, avoid copying all other attributes onto it | ||
if (i === 0) { | ||
args.push(t.objectExpression([])); | ||
} | ||
current = null; | ||
args.push(node.argument); | ||
} else { | ||
const name = getAttributeName(node); | ||
const value = getAttributeValue(node); | ||
if (!current) { | ||
current = t.objectExpression([]); | ||
args.push(current); | ||
} | ||
current.properties.push(t.objectProperty(name, value)); | ||
} | ||
} | ||
return t.callExpression( | ||
t.memberExpression(t.identifier("Object"), t.identifier("assign")), | ||
args | ||
); | ||
} | ||
// Converts a JSXAttribute to the equivalent ObjectExpression property | ||
function convertAttributeSpread(node) { | ||
if (t.isJSXSpreadAttribute(node)) { | ||
@@ -19,2 +48,22 @@ return t.spreadElement(node.argument); | ||
const name = getAttributeName(node); | ||
const value = getAttributeValue(node); | ||
return t.inherits(t.objectProperty(name, value), node); | ||
} | ||
// Convert a JSX attribute name to an Object expression property name | ||
function getAttributeName(node) { | ||
if (t.isJSXNamespacedName(node.name)) { | ||
return t.stringLiteral( | ||
node.name.namespace.name + ":" + node.name.name.name | ||
); | ||
} | ||
if (esutils.keyword.isIdentifierNameES6(node.name.name)) { | ||
return t.identifier(node.name.name); | ||
} | ||
return t.stringLiteral(node.name.name); | ||
} | ||
// Convert a JSX attribute value to a JavaScript expression value | ||
function getAttributeValue(node) { | ||
let value = node.value || t.booleanLiteral(true); | ||
@@ -33,20 +82,13 @@ | ||
if (t.isJSXNamespacedName(node.name)) { | ||
node.name = t.stringLiteral( | ||
node.name.namespace.name + ":" + node.name.name.name | ||
); | ||
} else if (esutils.keyword.isIdentifierNameES6(node.name.name)) { | ||
node.name.type = "Identifier"; | ||
} else { | ||
node.name = t.stringLiteral(node.name.name); | ||
} | ||
return t.inherits(t.objectProperty(node.name, value), node); | ||
return value; | ||
} | ||
return { | ||
name: "transform-hoist-tagged-templates", | ||
name: "transform-jsx-spread", | ||
visitor: { | ||
JSXOpeningElement(path) { | ||
const hasSpread = path.node.attributes.some(t.isJSXSpreadAttribute); | ||
JSXOpeningElement(path, state) { | ||
const useSpread = state.opts.useSpread === true; | ||
const hasSpread = path.node.attributes.some(attr => | ||
t.isJSXSpreadAttribute(attr) | ||
); | ||
@@ -56,7 +98,15 @@ // ignore JSX Elements without spread or with lone spread: | ||
path.node.attributes = [ | ||
t.jsxSpreadAttribute( | ||
t.objectExpression(path.node.attributes.map(convertAttribute)) | ||
), | ||
]; | ||
if (useSpread) { | ||
path.node.attributes = [ | ||
t.jsxSpreadAttribute( | ||
t.objectExpression( | ||
path.node.attributes.map(convertAttributeSpread) | ||
) | ||
), | ||
]; | ||
} else { | ||
path.node.attributes = [ | ||
t.jsxSpreadAttribute(convertAttributesAssign(path.node.attributes)), | ||
]; | ||
} | ||
}, | ||
@@ -63,0 +113,0 @@ }, |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
33928
634
160
0