@svgr/babel-plugin-transform-svg-component
Advanced tools
Comparing version 6.0.0-alpha.1 to 6.0.0-alpha.2
@@ -25,2 +25,7 @@ import { types, ConfigAPI, NodePath } from '@babel/core'; | ||
} | ||
interface JSXRuntimeImport { | ||
source: string; | ||
namespace?: string; | ||
specifiers?: string[]; | ||
} | ||
interface Options { | ||
@@ -36,3 +41,6 @@ typescript?: boolean; | ||
exportType?: 'named' | 'default'; | ||
namedExport: string; | ||
namedExport?: string; | ||
jsxRuntime?: 'automatic' | 'classic'; | ||
jsxRuntimeImport?: JSXRuntimeImport; | ||
importSource?: string; | ||
} | ||
@@ -39,0 +47,0 @@ |
@@ -58,3 +58,3 @@ 'use strict'; | ||
const identifier = core.types.identifier("SVGProps"); | ||
getOrCreateImport(ctx, "react").specifiers.push(core.types.importSpecifier(identifier, identifier)); | ||
getOrCreateImport(ctx, ctx.importSource).specifiers.push(core.types.importSpecifier(identifier, identifier)); | ||
return core.types.tsTypeReference(identifier, core.types.tsTypeParameterInstantiation([ | ||
@@ -66,3 +66,3 @@ core.types.tsTypeReference(core.types.identifier("SVGSVGElement")) | ||
const identifier = core.types.identifier("Ref"); | ||
getOrCreateImport(ctx, "react").specifiers.push(core.types.importSpecifier(identifier, identifier)); | ||
getOrCreateImport(ctx, ctx.importSource).specifiers.push(core.types.importSpecifier(identifier, identifier)); | ||
return core.types.tsTypeReference(identifier, core.types.tsTypeParameterInstantiation([ | ||
@@ -72,2 +72,20 @@ 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 = ({ | ||
@@ -77,3 +95,3 @@ opts, | ||
}) => { | ||
var _a; | ||
var _a, _b, _c; | ||
const componentName = core.types.identifier(opts.state.componentName); | ||
@@ -85,2 +103,3 @@ const interfaces = []; | ||
const ctx = { | ||
importSource: (_a = opts.importSource) != null ? _a : defaultImportSource, | ||
exportIdentifier: componentName, | ||
@@ -93,3 +112,5 @@ opts, | ||
}; | ||
imports.push(core.types.importDeclaration([core.types.importNamespaceSpecifier(core.types.identifier("React"))], core.types.stringLiteral("react"))); | ||
if (opts.jsxRuntime !== "automatic") { | ||
imports.push(getJsxRuntimeImport((_b = opts.jsxRuntimeImport) != null ? _b : defaultJsxRuntimeImport)); | ||
} | ||
if (opts.native) { | ||
@@ -140,3 +161,3 @@ getOrCreateImport(ctx, "react-native-svg").specifiers.push(core.types.importDefaultSpecifier(core.types.identifier("Svg"))); | ||
const ForwardRef = core.types.identifier("ForwardRef"); | ||
getOrCreateImport(ctx, "react").specifiers.push(core.types.importSpecifier(forwardRef, forwardRef)); | ||
getOrCreateImport(ctx, ctx.importSource).specifiers.push(core.types.importSpecifier(forwardRef, forwardRef)); | ||
exports.push(core.types.variableDeclaration("const", [ | ||
@@ -150,3 +171,3 @@ core.types.variableDeclarator(ForwardRef, core.types.callExpression(forwardRef, [ctx.exportIdentifier])) | ||
const Memo = core.types.identifier("Memo"); | ||
getOrCreateImport(ctx, "react").specifiers.push(core.types.importSpecifier(memo, memo)); | ||
getOrCreateImport(ctx, ctx.importSource).specifiers.push(core.types.importSpecifier(memo, memo)); | ||
exports.push(core.types.variableDeclaration("const", [ | ||
@@ -157,3 +178,6 @@ core.types.variableDeclarator(Memo, core.types.callExpression(memo, [ctx.exportIdentifier])) | ||
} | ||
if (((_a = opts.state.caller) == null ? void 0 : _a.previousExport) || opts.exportType === "named") { | ||
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, [ | ||
@@ -160,0 +184,0 @@ core.types.exportSpecifier(ctx.exportIdentifier, core.types.identifier(opts.namedExport)) |
{ | ||
"name": "@svgr/babel-plugin-transform-svg-component", | ||
"description": "Transform SVG into component", | ||
"version": "6.0.0-alpha.1", | ||
"version": "6.0.0-alpha.2", | ||
"main": "./dist/index.js", | ||
@@ -33,3 +33,3 @@ "exports": "./dist/index.js", | ||
}, | ||
"gitHead": "22e2736665552263fff88683e1551b8ad852b150" | ||
"gitHead": "f729efaad6e2ec22a27972b025737f41cf661045" | ||
} |
Sorry, the diff of this file is not supported yet
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
24157
245
0