babel-plugin-react-svg
Advanced tools
Comparing version 2.1.0 to 2.1.1-alpha.36
@@ -8,6 +8,5 @@ "use strict"; | ||
exports.namespaceToCamel = namespaceToCamel; | ||
function hyphenToCamel(name) { | ||
return name.replace(/-([a-z])/g, function (g) { | ||
return g[1].toUpperCase(); | ||
}); | ||
return name.replace(/-([a-z])/g, g => g[1].toUpperCase()); | ||
} | ||
@@ -14,0 +13,0 @@ |
@@ -7,15 +7,19 @@ "use strict"; | ||
exports.default = cssToObj; | ||
function cssToObj(css) { | ||
var o = {}; | ||
var elements = css.split(";"); | ||
elements.filter(function (el) { | ||
return !!el; | ||
}).map(function (el) { | ||
var s = el.split(":"), | ||
key = s.shift().trim(), | ||
value = s.join(":").trim(); | ||
o[key] = value; | ||
}); | ||
let o = {}; | ||
if (typeof css !== "undefined") { | ||
let elements = css.split(";"); | ||
elements.filter(el => !!el).map(el => { | ||
let s = el.split(":"), | ||
key = s.shift().trim(), | ||
value = s.join(":").trim(); | ||
o[key] = value; | ||
}); | ||
} | ||
return o; | ||
} | ||
module.exports = exports["default"]; | ||
module.exports = exports.default; |
106
lib/index.js
@@ -6,17 +6,26 @@ "use strict"; | ||
}); | ||
exports.default = _default; | ||
exports.default = function (babel) { | ||
var t = babel.types; | ||
var _cssToObj = _interopRequireDefault(require("./css-to-obj")); | ||
var createClass = function createClass(className) { | ||
return t.logicalExpression("||", t.memberExpression( | ||
/* object = */t.identifier("styles"), | ||
/* property = */t.stringLiteral(className), | ||
/* computed = */true), t.stringLiteral(className)); | ||
}; | ||
var _camelize = require("./camelize"); | ||
var attrVisitor = { | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _default(babel) { | ||
const t = babel.types; | ||
const restElement = t.restElement ? t.restElement : t.restProperty; | ||
const createClass = className => t.logicalExpression("||", t.memberExpression( | ||
/* object = */ | ||
t.identifier("styles"), | ||
/* property = */ | ||
t.stringLiteral(className), | ||
/* computed = */ | ||
true), t.stringLiteral(className)); | ||
const attrVisitor = { | ||
JSXAttribute(path) { | ||
var name = path.get("name"); | ||
var value = path.get("value"); | ||
const name = path.get("name"); | ||
const value = path.get("value"); | ||
@@ -35,39 +44,36 @@ if (name.isJSXNamespacedName()) { | ||
// <tag className="blah blah1"/> | ||
name.replaceWith(t.jSXIdentifier("className")); | ||
// converts | ||
name.replaceWith(t.jSXIdentifier("className")); // converts | ||
// className="foo bar" | ||
// to | ||
// className={(styles["foo"] || "foo") + " " + (styles["bar"] || "bar")} | ||
var classes = value.node.value.split(/\s/); | ||
let classes = value.node.value.split(/\s/); | ||
if (classes.length > 0) { | ||
var expr = createClass(classes[0]); | ||
for (var i = 1; i < classes.length; i++) { | ||
expr = t.binaryExpression("+", | ||
// (props.styles["foo"] || "foo") + " " | ||
t.binaryExpression("+", expr, t.stringLiteral(" ")), | ||
// (props.styles["bar"] || "bar") | ||
let expr = createClass(classes[0]); | ||
for (let i = 1; i < classes.length; i++) { | ||
expr = t.binaryExpression("+", // (props.styles["foo"] || "foo") + " " | ||
t.binaryExpression("+", expr, t.stringLiteral(" ")), // (props.styles["bar"] || "bar") | ||
createClass(classes[i])); | ||
} | ||
value.replaceWith(t.jSXExpressionContainer(expr)); | ||
} | ||
} | ||
// converts | ||
} // converts | ||
// <tag style="text-align: center; width: 50px"> | ||
// to | ||
// <tag style={{textAlign: 'center', width: '50px'}}> | ||
if (name.node.name === "style") { | ||
var csso = (0, _cssToObj2.default)(value.node.value); | ||
var properties = Object.keys(csso).map(function (prop) { | ||
return t.objectProperty(t.identifier((0, _camelize.hyphenToCamel)(prop)), t.stringLiteral(csso[prop])); | ||
}); | ||
let csso = (0, _cssToObj.default)(value.node.value); | ||
let properties = Object.keys(csso).map(prop => t.objectProperty(t.identifier((0, _camelize.hyphenToCamel)(prop)), t.stringLiteral(csso[prop]))); | ||
value.replaceWith(t.jSXExpressionContainer(t.objectExpression(properties))); | ||
} | ||
// converts | ||
} // converts | ||
// <svg stroke-width="5" data-x="0" aria-label="foo"> | ||
// to | ||
// <svg strokeWidth="5" data-x="0" aria-label="foo"> | ||
if (!/^(data-|aria-)/.test(name.node.name)) { | ||
@@ -78,11 +84,9 @@ name.replaceWith(t.jSXIdentifier((0, _camelize.hyphenToCamel)(path.node.name.name))); | ||
} | ||
}; | ||
// returns | ||
}; // returns | ||
// export default (props) => ${input_svg_node} | ||
var getExport = function getExport(svg) { | ||
return t.exportDefaultDeclaration(t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier("styles"), t.assignmentPattern(t.identifier("styles"), t.objectExpression([])), false, true), t.restProperty(t.identifier("props"))])], svg)); | ||
}; | ||
// converts | ||
const getExport = function (svg) { | ||
return t.exportDefaultDeclaration(t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier("styles"), t.assignmentPattern(t.identifier("styles"), t.objectExpression([])), false, true), restElement(t.identifier("props"))])], svg)); | ||
}; // converts | ||
// <svg> | ||
@@ -92,3 +96,5 @@ // to | ||
// after passing through attributes visitors | ||
var svgVisitor = { | ||
const svgVisitor = { | ||
JSXOpeningElement(path) { | ||
@@ -100,5 +106,4 @@ if (path.node.name.name.toLowerCase() === "svg") { | ||
} | ||
}; | ||
// converts | ||
}; // converts | ||
// <svg/> | ||
@@ -109,3 +114,4 @@ // to | ||
// after passing through other visitors | ||
var svgExpressionVisitor = { | ||
const svgExpressionVisitor = { | ||
ExpressionStatement(path) { | ||
@@ -116,5 +122,5 @@ if (!path.get("expression").isJSXElement()) return; | ||
} | ||
}; | ||
var programVisitor = { | ||
const programVisitor = { | ||
Program(path) { | ||
@@ -124,17 +130,9 @@ // add import react statement | ||
} | ||
}; | ||
return { | ||
visitor: Object.assign({}, programVisitor, svgExpressionVisitor, svgVisitor, attrVisitor) | ||
}; | ||
}; | ||
} | ||
var _cssToObj = require("./css-to-obj"); | ||
var _cssToObj2 = _interopRequireDefault(_cssToObj); | ||
var _camelize = require("./camelize"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
module.exports = exports["default"]; | ||
module.exports = exports.default; |
{ | ||
"name": "babel-plugin-react-svg", | ||
"version": "2.1.0", | ||
"version": "2.1.1-alpha.36+8d4107a", | ||
"description": "Babel plugin to transform svg to react component", | ||
@@ -18,2 +18,6 @@ "keywords": [ | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/boopathi/react-svg-loader.git" | ||
}, | ||
"license": "MIT", | ||
@@ -26,9 +30,9 @@ "author": "boopathi", | ||
"main": "lib/index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/boopathi/react-svg-loader.git" | ||
"peerDependencies": { | ||
"@babel/plugin-syntax-jsx": "^7.2.0" | ||
}, | ||
"peerDependencies": { | ||
"babel-plugin-syntax-jsx": "^6.18.0" | ||
} | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"gitHead": "8d4107a7c6c031e0aaf081a056ca8e9fa0ec5876" | ||
} |
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
9641
6
133
1
1