This commit is contained in:
2024-03-22 03:47:51 +05:30
parent 8bcf3d211e
commit 89819f6fe2
28440 changed files with 3211033 additions and 2 deletions

View File

@@ -0,0 +1,7 @@
Copyright 2017 Smooth Code
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,25 @@
# @svgr/babel-plugin-svg-dynamic-title
## Install
```
npm install --save-dev @svgr/babel-plugin-svg-dynamic-title
```
## Usage
**.babelrc**
```json
{
"plugins": ["@svgr/babel-plugin-svg-dynamic-title"]
}
```
## Note
This plugin handles both the titleProp and descProp options. By default, it will handle titleProp only.
## License
MIT

View File

@@ -0,0 +1,16 @@
import { NodePath, types } from '@babel/core';
declare type tag = 'title' | 'desc';
interface Options {
tag: tag | null;
}
interface State {
opts: Options;
}
declare const plugin: () => {
visitor: {
JSXElement(path: NodePath<types.JSXElement>, state: State): void;
};
};
export { Options, plugin as default };

View File

@@ -0,0 +1,101 @@
'use strict';
var core = require('@babel/core');
const elements = ["svg", "Svg"];
const createTagElement = (tag, children = [], attributes = []) => {
const eleName = core.types.jsxIdentifier(tag);
return core.types.jsxElement(
core.types.jsxOpeningElement(eleName, attributes),
core.types.jsxClosingElement(eleName),
children
);
};
const createTagIdAttribute = (tag) => core.types.jsxAttribute(
core.types.jsxIdentifier("id"),
core.types.jsxExpressionContainer(core.types.identifier(`${tag}Id`))
);
const addTagIdAttribute = (tag, attributes) => {
const existingId = attributes.find(
(attribute) => core.types.isJSXAttribute(attribute) && attribute.name.name === "id"
);
if (!existingId) {
return [...attributes, createTagIdAttribute(tag)];
}
existingId.value = core.types.jsxExpressionContainer(
core.types.isStringLiteral(existingId.value) ? core.types.logicalExpression("||", core.types.identifier(`${tag}Id`), existingId.value) : core.types.identifier(`${tag}Id`)
);
return attributes;
};
const plugin = () => ({
visitor: {
JSXElement(path, state) {
const tag = state.opts.tag || "title";
if (!elements.length)
return;
const openingElement = path.get("openingElement");
const openingElementName = openingElement.get("name");
if (!elements.some(
(element) => openingElementName.isJSXIdentifier({ name: element })
)) {
return;
}
const getTagElement = (existingTitle) => {
var _a;
const tagExpression = core.types.identifier(tag);
if (existingTitle) {
existingTitle.openingElement.attributes = addTagIdAttribute(
tag,
existingTitle.openingElement.attributes
);
}
const conditionalTitle = core.types.conditionalExpression(
tagExpression,
createTagElement(
tag,
[core.types.jsxExpressionContainer(tagExpression)],
existingTitle ? existingTitle.openingElement.attributes : [createTagIdAttribute(tag)]
),
core.types.nullLiteral()
);
if ((_a = existingTitle == null ? void 0 : existingTitle.children) == null ? void 0 : _a.length) {
return core.types.jsxExpressionContainer(
core.types.conditionalExpression(
core.types.binaryExpression(
"===",
tagExpression,
core.types.identifier("undefined")
),
existingTitle,
conditionalTitle
)
);
}
return core.types.jsxExpressionContainer(conditionalTitle);
};
let tagElement = null;
const hasTitle = path.get("children").some((childPath) => {
if (childPath.node === tagElement)
return false;
if (!childPath.isJSXElement())
return false;
const name = childPath.get("openingElement").get("name");
if (!name.isJSXIdentifier())
return false;
if (name.node.name !== tag)
return false;
tagElement = getTagElement(childPath.node);
childPath.replaceWith(tagElement);
return true;
});
tagElement = tagElement || getTagElement();
if (!hasTitle) {
path.node.children.unshift(tagElement);
path.replaceWith(path.node);
}
}
}
});
module.exports = plugin;
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,40 @@
{
"name": "@svgr/babel-plugin-svg-dynamic-title",
"description": "Transform SVG by adding a dynamic title element",
"version": "6.5.1",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./package.json": "./package.json"
},
"repository": "https://github.com/gregberge/svgr/tree/main/packages/babel-plugin-svg-dynamic-title",
"author": "Greg Bergé <berge.greg@gmail.com>",
"publishConfig": {
"access": "public"
},
"keywords": [
"babel-plugin"
],
"engines": {
"node": ">=10"
},
"homepage": "https://react-svgr.com",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/gregberge"
},
"license": "MIT",
"peerDependencies": {
"@babel/core": "^7.0.0-0"
},
"scripts": {
"reset": "rm -rf dist",
"build": "rollup -c ../../build/rollup.config.js",
"prepublishOnly": "npm run reset && npm run build"
},
"gitHead": "d5efedd372999692f84d30072e502b5a6b8fe734"
}