diff --git a/packages/babel-plugin-transform-svg-component/src/__snapshots__/index.test.ts.snap b/packages/babel-plugin-transform-svg-component/src/__snapshots__/index.test.ts.snap index 93ded8d4..29057829 100644 --- a/packages/babel-plugin-transform-svg-component/src/__snapshots__/index.test.ts.snap +++ b/packages/babel-plugin-transform-svg-component/src/__snapshots__/index.test.ts.snap @@ -332,7 +332,7 @@ export default SvgComponent;" exports[`plugin typescript with "descProp" and "expandProps" adds "desc", "descId" props and expands props 1`] = ` "import * as React from "react"; -import { SVGProps } from "react"; +import type { SVGProps } from "react"; interface SVGRProps { desc?: string; descId?: string; @@ -347,7 +347,7 @@ export default SvgComponent;" exports[`plugin typescript with "expandProps" add props 1`] = ` "import * as React from "react"; -import { SVGProps } from "react"; +import type { SVGProps } from "react"; const SvgComponent = (props: SVGProps) => ; export default SvgComponent;" `; @@ -377,7 +377,8 @@ export default img;" exports[`plugin typescript with "native" and "expandProps" option adds import from "react-native-svg" and adds props 1`] = ` "import * as React from "react"; -import Svg, { SvgProps } from "react-native-svg"; +import Svg from "react-native-svg"; +import type { SvgProps } from "react-native-svg"; const SvgComponent = (props: SvgProps) => ; export default SvgComponent;" `; @@ -391,7 +392,8 @@ export default SvgComponent;" exports[`plugin typescript with "native", "ref" and "expandProps" option adds import from "react-native-svg" and adds props and adds ForwardRef component 1`] = ` "import * as React from "react"; -import Svg, { SvgProps } from "react-native-svg"; +import Svg from "react-native-svg"; +import type { SvgProps } from "react-native-svg"; import { Ref, forwardRef } from "react"; const SvgComponent = (props: SvgProps, ref: Ref) => ; const ForwardRef = forwardRef(SvgComponent); @@ -409,7 +411,8 @@ export default ForwardRef;" exports[`plugin typescript with "ref" and "expandProps" option expands props 1`] = ` "import * as React from "react"; -import { SVGProps, Ref, forwardRef } from "react"; +import type { SVGProps } from "react"; +import { Ref, forwardRef } from "react"; const SvgComponent = (props: SVGProps, ref: Ref) => ; const ForwardRef = forwardRef(SvgComponent); export default ForwardRef;" @@ -425,7 +428,7 @@ export default ForwardRef;" exports[`plugin typescript with "titleProp" "descProp" and "expandProps" adds "title", "titleId", "desc", "descId" props and expands props 1`] = ` "import * as React from "react"; -import { SVGProps } from "react"; +import type { SVGProps } from "react"; interface SVGRProps { title?: string; titleId?: string; @@ -474,7 +477,7 @@ export default SvgComponent;" exports[`plugin typescript with "titleProp" and "expandProps" adds "title", "titleId" props and expands props 1`] = ` "import * as React from "react"; -import { SVGProps } from "react"; +import type { SVGProps } from "react"; interface SVGRProps { title?: string; titleId?: string; diff --git a/packages/babel-plugin-transform-svg-component/src/variables.ts b/packages/babel-plugin-transform-svg-component/src/variables.ts index e597b390..90129701 100644 --- a/packages/babel-plugin-transform-svg-component/src/variables.ts +++ b/packages/babel-plugin-transform-svg-component/src/variables.ts @@ -1,5 +1,6 @@ import { types as t, template } from '@babel/core' import type { Options, TemplateVariables, JSXRuntimeImport } from './types' +import type { ImportDeclaration } from '@babel/types' const tsOptionalPropertySignature = ( ...args: Parameters @@ -18,16 +19,24 @@ interface Context { importSource: string } -const getOrCreateImport = ({ imports }: Context, sourceValue: string) => { +const getOrCreateImport = ( + { imports }: Context, + sourceValue: string, + importKind: ImportDeclaration['importKind'] = undefined, +) => { const existing = imports.find( (imp) => imp.source.value === sourceValue && + imp.importKind === importKind && !imp.specifiers.some( (specifier) => specifier.type === 'ImportNamespaceSpecifier', ), ) if (existing) return existing const imp = t.importDeclaration([], t.stringLiteral(sourceValue)) + if (importKind !== undefined) { + imp.importKind = importKind + } imports.push(imp) return imp } @@ -35,13 +44,13 @@ const getOrCreateImport = ({ imports }: Context, sourceValue: string) => { const tsTypeReferenceSVGProps = (ctx: Context) => { if (ctx.opts.native) { const identifier = t.identifier('SvgProps') - getOrCreateImport(ctx, 'react-native-svg').specifiers.push( + getOrCreateImport(ctx, 'react-native-svg', 'type').specifiers.push( t.importSpecifier(identifier, identifier), ) return t.tsTypeReference(identifier) } const identifier = t.identifier('SVGProps') - getOrCreateImport(ctx, ctx.importSource).specifiers.push( + getOrCreateImport(ctx, ctx.importSource, 'type').specifiers.push( t.importSpecifier(identifier, identifier), ) return t.tsTypeReference( diff --git a/packages/cli/src/__snapshots__/index.test.ts.snap b/packages/cli/src/__snapshots__/index.test.ts.snap index 1024a06d..17d9a4af 100644 --- a/packages/cli/src/__snapshots__/index.test.ts.snap +++ b/packages/cli/src/__snapshots__/index.test.ts.snap @@ -468,7 +468,8 @@ export default SvgFile exports[`cli should support various args: --typescript --ref --desc-prop 1`] = ` "import * as React from 'react' -import { SVGProps, Ref, forwardRef } from 'react' +import type { SVGProps } from 'react' +import { Ref, forwardRef } from 'react' interface SVGRProps { desc?: string; descId?: string; @@ -497,7 +498,8 @@ export default ForwardRef exports[`cli should support various args: --typescript --ref --title-prop 1`] = ` "import * as React from 'react' -import { SVGProps, Ref, forwardRef } from 'react' +import type { SVGProps } from 'react' +import { Ref, forwardRef } from 'react' interface SVGRProps { title?: string; titleId?: string; @@ -526,7 +528,8 @@ export default ForwardRef exports[`cli should support various args: --typescript --ref 1`] = ` "import * as React from 'react' -import { SVGProps, Ref, forwardRef } from 'react' +import type { SVGProps } from 'react' +import { Ref, forwardRef } from 'react' const SvgFile = (props: SVGProps, ref: Ref) => ( ) => (