babel-plugin-react-svg
Advanced tools
Comparing version 3.0.3 to 3.0.4-alpha.3
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.hyphenToCamel = hyphenToCamel; | ||
exports.namespaceToCamel = namespaceToCamel; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function hyphenToCamel(name) { | ||
return name.replace(/-([a-z])/g, g => g[1].toUpperCase()); | ||
return name.replace(/-([a-z])/g, g => g[1].toUpperCase()); | ||
} | ||
exports.hyphenToCamel = hyphenToCamel; | ||
function namespaceToCamel(namespace, name) { | ||
return namespace + name.charAt(0).toUpperCase() + name.slice(1); | ||
} | ||
return namespace + name.charAt(0).toUpperCase() + name.slice(1); | ||
} | ||
exports.namespaceToCamel = namespaceToCamel; | ||
//# sourceMappingURL=camelize.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = cssToObj; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function cssToObj(css) { | ||
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; | ||
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; | ||
exports.default = cssToObj; | ||
//# sourceMappingURL=css-to-obj.js.map |
240
lib/index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = _default; | ||
var _cssToObj = _interopRequireDefault(require("./css-to-obj")); | ||
var _camelize = require("./camelize"); | ||
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) { | ||
const name = path.get("name"); | ||
const value = path.get("value"); | ||
if (name.isJSXNamespacedName()) { | ||
// converts | ||
// <svg xmlns:xlink="asdf"> | ||
// to | ||
// <svg xmlnsXlink="asdf"> | ||
name.replaceWith(t.jSXIdentifier((0, _camelize.namespaceToCamel)(path.node.name.namespace.name, path.node.name.name.name))); | ||
} else if (name.isJSXIdentifier()) { | ||
if (name.node.name === "class") { | ||
// converts | ||
// <tag class="blah blah1"/> | ||
// to | ||
// <tag className="blah blah1"/> | ||
name.replaceWith(t.jSXIdentifier("className")); // converts | ||
// className="foo bar" | ||
// to | ||
// className={(styles["foo"] || "foo") + " " + (styles["bar"] || "bar")} | ||
let classes = value.node.value.split(/\s/); | ||
if (classes.length > 0) { | ||
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])); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const css_to_obj_1 = __importDefault(require("./css-to-obj")); | ||
const camelize_1 = require("./camelize"); | ||
function default_1(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) { | ||
const name = path.get("name"); | ||
const value = path.get("value"); | ||
if (name.isJSXNamespacedName()) { | ||
// converts | ||
// <svg xmlns:xlink="asdf"> | ||
// to | ||
// <svg xmlnsXlink="asdf"> | ||
name.replaceWith(t.jSXIdentifier(camelize_1.namespaceToCamel(path.node.name.namespace.name, path.node.name.name.name))); | ||
} | ||
value.replaceWith(t.jSXExpressionContainer(expr)); | ||
} | ||
} // converts | ||
// <tag style="text-align: center; width: 50px"> | ||
// to | ||
// <tag style={{textAlign: 'center', width: '50px'}}> | ||
if (name.node.name === "style") { | ||
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 | ||
// <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)) { | ||
name.replaceWith(t.jSXIdentifier((0, _camelize.hyphenToCamel)(path.node.name.name))); | ||
else if (name.isJSXIdentifier()) { | ||
if (name.node.name === "class") { | ||
// converts | ||
// <tag class="blah blah1"/> | ||
// to | ||
// <tag className="blah blah1"/> | ||
name.replaceWith(t.jSXIdentifier("className")); | ||
// converts | ||
// className="foo bar" | ||
// to | ||
// className={(styles["foo"] || "foo") + " " + (styles["bar"] || "bar")} | ||
let classes = value.node.value.split(/\s/); | ||
if (classes.length > 0) { | ||
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 | ||
// <tag style="text-align: center; width: 50px"> | ||
// to | ||
// <tag style={{textAlign: 'center', width: '50px'}}> | ||
if (name.node.name === "style") { | ||
let csso = css_to_obj_1.default(value.node.value); | ||
let properties = Object.keys(csso).map(prop => t.objectProperty(t.identifier(camelize_1.hyphenToCamel(prop)), t.stringLiteral(csso[prop]))); | ||
value.replaceWith(t.jSXExpressionContainer(t.objectExpression(properties))); | ||
} | ||
// 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)) { | ||
name.replaceWith(t.jSXIdentifier(camelize_1.hyphenToCamel(path.node.name.name))); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; // returns | ||
// export default (props) => ${input_svg_node} | ||
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> | ||
// to | ||
// <svg {this.props}> | ||
// after passing through attributes visitors | ||
const svgVisitor = { | ||
JSXOpeningElement(path) { | ||
if (path.node.name.name.toLowerCase() === "svg") { | ||
// add spread props | ||
path.node.attributes.push(t.jSXSpreadAttribute(t.identifier("props"))); | ||
} | ||
} | ||
}; // converts | ||
// <svg/> | ||
// to | ||
// import React from 'react'; | ||
// export default props => <svg {...props}/>; | ||
// after passing through other visitors | ||
const svgExpressionVisitor = { | ||
ExpressionStatement(path) { | ||
if (!path.get("expression").isJSXElement()) return; | ||
if (path.get("expression.openingElement.name").node.name !== "svg") return; | ||
path.replaceWith(getExport(path.get("expression").node)); | ||
} | ||
}; | ||
const programVisitor = { | ||
Program(path) { | ||
// add import react statement | ||
path.node.body.unshift(t.importDeclaration([t.importDefaultSpecifier(t.identifier("React"))], t.stringLiteral("react"))); | ||
} | ||
}; | ||
return { | ||
visitor: Object.assign({}, programVisitor, svgExpressionVisitor, svgVisitor, attrVisitor) | ||
}; | ||
}; | ||
// returns | ||
// export default (props) => ${input_svg_node} | ||
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> | ||
// to | ||
// <svg {this.props}> | ||
// after passing through attributes visitors | ||
const svgVisitor = { | ||
JSXOpeningElement(path) { | ||
if (path.node.name.name.toLowerCase() === "svg") { | ||
// add spread props | ||
path.node.attributes.push(t.jSXSpreadAttribute(t.identifier("props"))); | ||
} | ||
} | ||
}; | ||
// converts | ||
// <svg/> | ||
// to | ||
// import React from 'react'; | ||
// export default props => <svg {...props}/>; | ||
// after passing through other visitors | ||
const svgExpressionVisitor = { | ||
ExpressionStatement(path) { | ||
if (!path.get("expression").isJSXElement()) | ||
return; | ||
if (path.get("expression.openingElement.name").node.name !== "svg") | ||
return; | ||
path.replaceWith(getExport(path.get("expression").node)); | ||
} | ||
}; | ||
const programVisitor = { | ||
Program(path) { | ||
// add import react statement | ||
path.node.body.unshift(t.importDeclaration([t.importDefaultSpecifier(t.identifier("React"))], t.stringLiteral("react"))); | ||
} | ||
}; | ||
return { | ||
visitor: Object.assign({}, programVisitor, svgExpressionVisitor, svgVisitor, attrVisitor) | ||
}; | ||
} | ||
module.exports = exports.default; | ||
exports.default = default_1; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "babel-plugin-react-svg", | ||
"version": "3.0.3", | ||
"version": "3.0.4-alpha.3+0d2beda", | ||
"description": "Babel plugin to transform svg to react component", | ||
@@ -35,3 +35,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "cf2ffcea04be8e008ef01e5710e43d3fc76cfdf2" | ||
"gitHead": "0d2beda61aff7963b4ec34effe274ed6bf006abd" | ||
} |
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
16652
15
150
1
1