1
1
import { types as t } from '@babel/core'
2
- import type { Options , TemplateVariables } from './types'
2
+ import type { Options , TemplateVariables , JSXRuntimeImport } from './types'
3
3
4
4
const tsOptionalPropertySignature = (
5
5
...args : Parameters < typeof t . tsPropertySignature >
@@ -15,6 +15,7 @@ interface Context {
15
15
interfaces : t . TSInterfaceDeclaration [ ]
16
16
props : ( t . Identifier | t . ObjectPattern ) [ ]
17
17
imports : t . ImportDeclaration [ ]
18
+ importSource : string
18
19
}
19
20
20
21
const getOrCreateImport = ( { imports } : Context , sourceValue : string ) => {
@@ -40,7 +41,7 @@ const tsTypeReferenceSVGProps = (ctx: Context) => {
40
41
return t . tsTypeReference ( identifier )
41
42
}
42
43
const identifier = t . identifier ( 'SVGProps' )
43
- getOrCreateImport ( ctx , 'react' ) . specifiers . push (
44
+ getOrCreateImport ( ctx , ctx . importSource ) . specifiers . push (
44
45
t . importSpecifier ( identifier , identifier ) ,
45
46
)
46
47
return t . tsTypeReference (
@@ -53,7 +54,7 @@ const tsTypeReferenceSVGProps = (ctx: Context) => {
53
54
54
55
const tsTypeReferenceSVGRef = ( ctx : Context ) => {
55
56
const identifier = t . identifier ( 'Ref' )
56
- getOrCreateImport ( ctx , 'react' ) . specifiers . push (
57
+ getOrCreateImport ( ctx , ctx . importSource ) . specifiers . push (
57
58
t . importSpecifier ( identifier , identifier ) ,
58
59
)
59
60
return t . tsTypeReference (
@@ -64,6 +65,29 @@ const tsTypeReferenceSVGRef = (ctx: Context) => {
64
65
)
65
66
}
66
67
68
+ const getJsxRuntimeImport = ( cfg : JSXRuntimeImport ) => {
69
+ const specifiers = ( ( ) => {
70
+ if ( cfg . namespace )
71
+ return [ t . importNamespaceSpecifier ( t . identifier ( cfg . namespace ) ) ]
72
+ if ( cfg . specifiers )
73
+ return cfg . specifiers . map ( ( specifier ) => {
74
+ const identifier = t . identifier ( specifier )
75
+ return t . importSpecifier ( identifier , identifier )
76
+ } )
77
+ throw new Error (
78
+ `Specify either "namespace" or "specifiers" in "jsxRuntimeImport" option` ,
79
+ )
80
+ } ) ( )
81
+ return t . importDeclaration ( specifiers , t . stringLiteral ( cfg . source ) )
82
+ }
83
+
84
+ const defaultJsxRuntimeImport : JSXRuntimeImport = {
85
+ source : 'react' ,
86
+ namespace : 'React' ,
87
+ }
88
+
89
+ const defaultImportSource = 'react'
90
+
67
91
export const getVariables = ( {
68
92
opts,
69
93
jsx,
@@ -77,6 +101,7 @@ export const getVariables = ({
77
101
const imports : t . ImportDeclaration [ ] = [ ]
78
102
const exports : ( t . VariableDeclaration | t . ExportDeclaration ) [ ] = [ ]
79
103
const ctx = {
104
+ importSource : opts . importSource ?? defaultImportSource ,
80
105
exportIdentifier : componentName ,
81
106
opts,
82
107
interfaces,
@@ -85,12 +110,11 @@ export const getVariables = ({
85
110
exports,
86
111
}
87
112
88
- imports . push (
89
- t . importDeclaration (
90
- [ t . importNamespaceSpecifier ( t . identifier ( 'React' ) ) ] ,
91
- t . stringLiteral ( 'react' ) ,
92
- ) ,
93
- )
113
+ if ( opts . jsxRuntime !== 'automatic' ) {
114
+ imports . push (
115
+ getJsxRuntimeImport ( opts . jsxRuntimeImport ?? defaultJsxRuntimeImport ) ,
116
+ )
117
+ }
94
118
95
119
if ( opts . native ) {
96
120
getOrCreateImport ( ctx , 'react-native-svg' ) . specifiers . push (
@@ -171,7 +195,7 @@ export const getVariables = ({
171
195
}
172
196
const forwardRef = t . identifier ( 'forwardRef' )
173
197
const ForwardRef = t . identifier ( 'ForwardRef' )
174
- getOrCreateImport ( ctx , 'react' ) . specifiers . push (
198
+ getOrCreateImport ( ctx , ctx . importSource ) . specifiers . push (
175
199
t . importSpecifier ( forwardRef , forwardRef ) ,
176
200
)
177
201
exports . push (
@@ -188,7 +212,7 @@ export const getVariables = ({
188
212
if ( opts . memo ) {
189
213
const memo = t . identifier ( 'memo' )
190
214
const Memo = t . identifier ( 'Memo' )
191
- getOrCreateImport ( ctx , 'react' ) . specifiers . push (
215
+ getOrCreateImport ( ctx , ctx . importSource ) . specifiers . push (
192
216
t . importSpecifier ( memo , memo ) ,
193
217
)
194
218
exports . push (
@@ -203,6 +227,9 @@ export const getVariables = ({
203
227
}
204
228
205
229
if ( opts . state . caller ?. previousExport || opts . exportType === 'named' ) {
230
+ if ( ! opts . namedExport ) {
231
+ throw new Error ( `"namedExport" not specified` )
232
+ }
206
233
exports . push (
207
234
t . exportNamedDeclaration ( null , [
208
235
t . exportSpecifier ( ctx . exportIdentifier , t . identifier ( opts . namedExport ) ) ,
1 commit comments
vercel[bot] commentedon Nov 1, 2021
Successfully deployed to the following URLs: