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,23 @@
# @svgr/babel-plugin-transform-svg-component
## Install
```
npm install --save-dev @svgr/babel-plugin-transform-svg-component
```
## Usage
**.babelrc**
```json
{
"plugins": [
["@svgr/babel-plugin-transform-svg-component", { "titleProp": true }]
]
}
```
## License
MIT

View File

@@ -0,0 +1,53 @@
import { types, ConfigAPI, NodePath } from '@babel/core';
import { TemplateBuilder } from '@babel/template';
interface TemplateVariables {
componentName: string;
interfaces: types.TSInterfaceDeclaration[];
props: (types.ObjectPattern | types.Identifier)[];
imports: types.ImportDeclaration[];
exports: (types.VariableDeclaration | types.ExportDeclaration | types.Statement)[];
jsx: types.JSXElement;
}
interface TemplateContext {
options: Options;
tpl: TemplateBuilder<types.Statement | types.Statement[]>['ast'];
}
interface Template {
(variables: TemplateVariables, context: TemplateContext): types.Statement | types.Statement[];
}
interface State {
componentName: string;
caller?: {
previousExport?: string | null;
};
}
interface JSXRuntimeImport {
source: string;
namespace?: string;
specifiers?: string[];
}
interface Options {
typescript?: boolean;
titleProp?: boolean;
descProp?: boolean;
expandProps?: boolean | 'start' | 'end';
ref?: boolean;
template?: Template;
state: State;
native?: boolean;
memo?: boolean;
exportType?: 'named' | 'default';
namedExport?: string;
jsxRuntime?: 'automatic' | 'classic';
jsxRuntimeImport?: JSXRuntimeImport;
importSource?: string;
}
declare const plugin: (_: ConfigAPI, opts: Options) => {
visitor: {
Program(path: NodePath<types.Program>): void;
};
};
export { Options, Template, plugin as default };

View File

@@ -0,0 +1,295 @@
'use strict';
var core = require('@babel/core');
const defaultTemplate = (variables, { tpl }) => {
return tpl`
${variables.imports};
${variables.interfaces};
const ${variables.componentName} = (${variables.props}) => (
${variables.jsx}
);
${variables.exports};
`;
};
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
const tsOptionalPropertySignature = (...args) => {
return __spreadProps(__spreadValues({}, core.types.tsPropertySignature(...args)), {
optional: true
});
};
const getOrCreateImport = ({ imports }, sourceValue) => {
const existing = imports.find(
(imp2) => imp2.source.value === sourceValue && !imp2.specifiers.some(
(specifier) => specifier.type === "ImportNamespaceSpecifier"
)
);
if (existing)
return existing;
const imp = core.types.importDeclaration([], core.types.stringLiteral(sourceValue));
imports.push(imp);
return imp;
};
const tsTypeReferenceSVGProps = (ctx) => {
if (ctx.opts.native) {
const identifier2 = core.types.identifier("SvgProps");
getOrCreateImport(ctx, "react-native-svg").specifiers.push(
core.types.importSpecifier(identifier2, identifier2)
);
return core.types.tsTypeReference(identifier2);
}
const identifier = core.types.identifier("SVGProps");
getOrCreateImport(ctx, ctx.importSource).specifiers.push(
core.types.importSpecifier(identifier, identifier)
);
return core.types.tsTypeReference(
identifier,
core.types.tsTypeParameterInstantiation([
core.types.tsTypeReference(core.types.identifier("SVGSVGElement"))
])
);
};
const tsTypeReferenceSVGRef = (ctx) => {
const identifier = core.types.identifier("Ref");
getOrCreateImport(ctx, ctx.importSource).specifiers.push(
core.types.importSpecifier(identifier, identifier)
);
return core.types.tsTypeReference(
identifier,
core.types.tsTypeParameterInstantiation([
core.types.tsTypeReference(core.types.identifier("SVGSVGElement"))
])
);
};
const getJsxRuntimeImport = (cfg) => {
const specifiers = (() => {
if (cfg.namespace)
return [core.types.importNamespaceSpecifier(core.types.identifier(cfg.namespace))];
if (cfg.specifiers)
return cfg.specifiers.map((specifier) => {
const identifier = core.types.identifier(specifier);
return core.types.importSpecifier(identifier, identifier);
});
throw new Error(
`Specify either "namespace" or "specifiers" in "jsxRuntimeImport" option`
);
})();
return core.types.importDeclaration(specifiers, core.types.stringLiteral(cfg.source));
};
const defaultJsxRuntimeImport = {
source: "react",
namespace: "React"
};
const defaultImportSource = "react";
const getVariables = ({
opts,
jsx
}) => {
var _a, _b, _c, _d;
const interfaces = [];
const props = [];
const imports = [];
const exports = [];
const ctx = {
importSource: (_a = opts.importSource) != null ? _a : defaultImportSource,
exportIdentifier: core.types.identifier(opts.state.componentName),
opts,
interfaces,
props,
imports,
exports
};
if (opts.jsxRuntime !== "automatic") {
imports.push(
getJsxRuntimeImport((_b = opts.jsxRuntimeImport) != null ? _b : defaultJsxRuntimeImport)
);
}
if (opts.native) {
getOrCreateImport(ctx, "react-native-svg").specifiers.push(
core.types.importDefaultSpecifier(core.types.identifier("Svg"))
);
}
if (opts.titleProp || opts.descProp) {
const properties = [];
const propertySignatures = [];
const createProperty = (attr) => {
return core.types.objectProperty(
core.types.identifier(attr),
core.types.identifier(attr),
false,
true
);
};
const createSignature = (attr) => {
return tsOptionalPropertySignature(
core.types.identifier(attr),
core.types.tsTypeAnnotation(core.types.tsStringKeyword())
);
};
if (opts.titleProp) {
properties.push(createProperty("title"), createProperty("titleId"));
if (opts.typescript) {
propertySignatures.push(
createSignature("title"),
createSignature("titleId")
);
}
}
if (opts.descProp) {
properties.push(createProperty("desc"), createProperty("descId"));
if (opts.typescript) {
propertySignatures.push(
createSignature("desc"),
createSignature("descId")
);
}
}
const prop = core.types.objectPattern(properties);
props.push(prop);
if (opts.typescript) {
interfaces.push(
core.types.tsInterfaceDeclaration(
core.types.identifier("SVGRProps"),
null,
null,
core.types.tSInterfaceBody(propertySignatures)
)
);
prop.typeAnnotation = core.types.tsTypeAnnotation(
core.types.tsTypeReference(core.types.identifier("SVGRProps"))
);
}
}
if (opts.expandProps) {
const identifier = core.types.identifier("props");
if (core.types.isObjectPattern(props[0])) {
props[0].properties.push(core.types.restElement(identifier));
if (opts.typescript) {
props[0].typeAnnotation = core.types.tsTypeAnnotation(
core.types.tsIntersectionType([
tsTypeReferenceSVGProps(ctx),
props[0].typeAnnotation.typeAnnotation
])
);
}
} else {
props.push(identifier);
if (opts.typescript) {
identifier.typeAnnotation = core.types.tsTypeAnnotation(
tsTypeReferenceSVGProps(ctx)
);
}
}
}
if (opts.ref) {
if (props.length === 0) {
props.push(core.types.identifier("_"));
}
const prop = core.types.identifier("ref");
props.push(prop);
if (opts.typescript) {
prop.typeAnnotation = core.types.tsTypeAnnotation(tsTypeReferenceSVGRef(ctx));
}
const forwardRef = core.types.identifier("forwardRef");
const ForwardRef = core.types.identifier("ForwardRef");
getOrCreateImport(ctx, ctx.importSource).specifiers.push(
core.types.importSpecifier(forwardRef, forwardRef)
);
exports.push(
core.types.variableDeclaration("const", [
core.types.variableDeclarator(
ForwardRef,
core.types.callExpression(forwardRef, [ctx.exportIdentifier])
)
])
);
ctx.exportIdentifier = ForwardRef;
}
if (opts.memo) {
const memo = core.types.identifier("memo");
const Memo = core.types.identifier("Memo");
getOrCreateImport(ctx, ctx.importSource).specifiers.push(
core.types.importSpecifier(memo, memo)
);
exports.push(
core.types.variableDeclaration("const", [
core.types.variableDeclarator(
Memo,
core.types.callExpression(memo, [ctx.exportIdentifier])
)
])
);
ctx.exportIdentifier = Memo;
}
if (((_c = opts.state.caller) == null ? void 0 : _c.previousExport) || opts.exportType === "named") {
if (!opts.namedExport) {
throw new Error(`"namedExport" not specified`);
}
exports.push(
core.types.exportNamedDeclaration(null, [
core.types.exportSpecifier(ctx.exportIdentifier, core.types.identifier(opts.namedExport))
])
);
if ((_d = opts.state.caller) == null ? void 0 : _d.previousExport) {
const previousExportAst = core.template.ast(opts.state.caller.previousExport);
exports.push(
...Array.isArray(previousExportAst) ? previousExportAst : [previousExportAst]
);
}
} else {
exports.push(core.types.exportDefaultDeclaration(ctx.exportIdentifier));
}
return {
componentName: opts.state.componentName,
props,
interfaces,
imports,
exports,
jsx
};
};
const plugin = (_, opts) => {
const template = opts.template || defaultTemplate;
const plugins = opts.typescript ? ["jsx", "typescript"] : ["jsx"];
const tpl = core.template.smart({ plugins, preserveComments: true }).ast;
return {
visitor: {
Program(path) {
const jsx = path.node.body[0].expression;
const variables = getVariables({
opts,
jsx
});
const body = template(variables, { options: opts, tpl });
path.node.body = Array.isArray(body) ? body : [body];
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-transform-svg-component",
"description": "Transform SVG into component",
"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-transform-svg-component",
"author": "Greg Bergé <berge.greg@gmail.com>",
"publishConfig": {
"access": "public"
},
"keywords": [
"babel-plugin"
],
"engines": {
"node": ">=12"
},
"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"
}