@@ -26,21 +26,19 @@ export interface Options {
26
26
labelFormat ?: string
27
27
autoInject ?: boolean
28
28
customModules ?: ModuleConfig [ ]
29
+ jsxFactory ?: string
30
+ jsxImportSource ?: string
29
31
}
30
32
31
33
const defaultEmotionModules : ModuleConfig [ ] = [
32
- {
33
- moduleName : 'emotion' ,
34
- exportedNames : [ 'css' , 'keyframes' , 'injectGlobal' , 'cx' , 'merge' ] ,
35
- } ,
36
34
{
37
35
moduleName : '@emotion/styled' ,
38
36
exportedNames : [ 'styled' ] ,
39
37
hasDefaultExport : true ,
40
38
styledName : 'styled' ,
41
39
} ,
42
40
{
43
- moduleName : '@emotion/core ' ,
41
+ moduleName : '@emotion/react ' ,
44
42
exportedNames : [ 'css' ] ,
45
43
} ,
46
44
]
@@ -56,20 +54,24 @@ const getPackageRootPath = memoize((filename: string) => findRoot(filename))
56
54
const hashArray = ( arr : Array < string > ) => hashString ( arr . join ( '' ) )
57
55
58
56
const createImportJSXAst = memoize ( ( propertyName : string | undefined ) => {
59
- const importClause = ts . createImportClause (
57
+ const importClause = ts . factory . createImportClause (
58
+ false ,
60
59
undefined ,
61
- ts . createNamedImports ( [
60
+ ts . factory . createNamedImports ( [
62
61
propertyName
63
- ? ts . createImportSpecifier (
64
- ts . createIdentifier ( 'jsx' ) ,
65
- ts . createIdentifier ( propertyName ) ,
62
+ ? ts . factory . createImportSpecifier (
63
+ ts . factory . createIdentifier ( 'jsx' ) ,
64
+ ts . factory . createIdentifier ( propertyName ) ,
66
65
)
67
- : ts . createImportSpecifier ( undefined , ts . createIdentifier ( 'jsx' ) ) ,
66
+ : ts . factory . createImportSpecifier (
67
+ undefined ,
68
+ ts . factory . createIdentifier ( 'jsx' ) ,
69
+ ) ,
68
70
] ) ,
69
71
)
70
- const moduleSpecifier = ts . createStringLiteral ( '@emotion/core ' )
72
+ const moduleSpecifier = ts . factory . createStringLiteral ( '@emotion/react ' )
71
73
72
- return ts . createImportDeclaration (
74
+ return ts . factory . createImportDeclaration (
73
75
undefined ,
74
76
undefined ,
75
77
importClause ,
@@ -162,20 +164,21 @@ export const createEmotionPlugin = (pluginOptions?: Options) => {
162
164
let sourceFile : ts . SourceFile
163
165
let inserted = false
164
166
const visitor : ts . Visitor = ( node ) => {
165
- if ( ts . isSourceFile ( node ) ) {
166
- inserted = false
167
- return ts . visitEachChild ( node , visitor , context )
168
- }
169
167
if ( ts . isImportDeclaration ( node ) ) {
170
168
importCalls = importCalls . concat ( getImportCalls ( node , compilerOptions ) )
171
- // insert import { jsx [as jsxFactory] } from '@emotion/core ' behind the react import declaration
169
+ // insert import { jsx [as jsxFactory] } from '@emotion/react ' behind the react import declaration
172
170
if (
173
171
! inserted &&
174
172
options . autoInject &&
175
173
( < ts . StringLiteral > node . moduleSpecifier ) . text === 'react'
176
174
) {
177
175
inserted = true
178
- return [ createImportJSXAst ( compilerOptions . jsxFactory ) , node ]
176
+ return [
177
+ createImportJSXAst (
178
+ options ?. jsxFactory ?? compilerOptions . jsxFactory ,
179
+ ) ,
180
+ node ,
181
+ ]
179
182
}
180
183
return node
181
184
}
@@ -208,10 +211,10 @@ export const createEmotionPlugin = (pluginOptions?: Options) => {
208
211
)
209
212
} )
210
213
if ( info ) {
211
- expression = ts . createCall (
214
+ expression = ts . factory . createCallExpression (
212
215
expression . expression ,
213
216
[ ] ,
214
- [ ts . createStringLiteral ( expression . name . text ) ] ,
217
+ [ ts . factory . createStringLiteral ( expression . name . text ) ] ,
215
218
)
216
219
}
217
220
}
@@ -248,28 +251,28 @@ export const createEmotionPlugin = (pluginOptions?: Options) => {
248
251
stuffToHash ,
249
252
) } ${ positionInFile } `
250
253
const [ el , opts ] = exp . arguments
251
- const targetAssignment = ts . createPropertyAssignment (
252
- ts . createIdentifier ( 'target' ) ,
253
- ts . createStringLiteral ( stableClassName ) ,
254
+ const targetAssignment = ts . factory . createPropertyAssignment (
255
+ ts . factory . createIdentifier ( 'target' ) ,
256
+ ts . factory . createStringLiteral ( stableClassName ) ,
254
257
)
255
258
const args = [ el ]
256
259
args . push (
257
- ts . createObjectLiteral (
260
+ ts . factory . createObjectLiteralExpression (
258
261
opts && ts . isObjectLiteralExpression ( opts )
259
262
? opts . properties . concat ( targetAssignment )
260
263
: [ targetAssignment ] ,
261
264
true ,
262
265
) ,
263
266
)
264
267
265
- const updatedCall = ts . updateCall (
268
+ const updatedCall = ts . factory . updateCallExpression (
266
269
exp ,
267
270
exp . expression ,
268
271
exp . typeArguments ,
269
272
args ,
270
273
)
271
274
272
- return ts . updateCall (
275
+ return ts . factory . updateCallExpression (
273
276
transformedNode ,
274
277
updatedCall ,
275
278
transformedNode . typeArguments ,
@@ -300,12 +303,12 @@ export const createEmotionPlugin = (pluginOptions?: Options) => {
300
303
if ( localNameNode && ts . isIdentifier ( localNameNode ) ) {
301
304
const local = localNameNode . text
302
305
const fileName = basename ( rawPath , extname ( rawPath ) )
303
- transformedNode = ts . updateCall (
306
+ transformedNode = ts . factory . updateCallExpression (
304
307
transformedNode ,
305
308
transformedNode . expression ,
306
309
transformedNode . typeArguments ,
307
310
transformedNode . arguments . concat ( [
308
- ts . createStringLiteral (
311
+ ts . factory . createStringLiteral (
309
312
`label:${ options
310
313
. labelFormat ! . replace ( '[local]' , local )
311
314
. replace ( '[filename]' , fileName ) } ;`,
@@ -345,12 +348,12 @@ export const createEmotionPlugin = (pluginOptions?: Options) => {
345
348
const comment = convert
346
349
. fromObject ( sourcemapGenerator )
347
350
. toComment ( { multiline : true } )
348
- transformedNode = ts . updateCall (
351
+ transformedNode = ts . factory . updateCallExpression (
349
352
transformedNode ,
350
353
transformedNode . expression ,
351
354
transformedNode . typeArguments ,
352
355
transformedNode . arguments . concat ( [
353
- ts . createStringLiteral ( comment ) ,
356
+ ts . factory . createStringLiteral ( comment ) ,
354
357
] ) ,
355
358
)
356
359
}
@@ -375,7 +378,19 @@ export const createEmotionPlugin = (pluginOptions?: Options) => {
375
378
sourceRoot : '' ,
376
379
} )
377
380
const distNode = ts . visitNode ( node , visitor )
381
+ if ( inserted && options . jsxImportSource && distNode . statements . length ) {
382
+ // fIXME
383
+ // typeScript private API https://github.com/microsoft/TypeScript/pull/39199/files#diff-1516c8349f7a625a2e4a2aa60f6bbe84e4b1a499128e8705d3087d893e01d367R5974
384
+ // @ts -expect-error
385
+ distNode . pragmas . set ( 'jsximportsource' , {
386
+ arguments : {
387
+ factory : options . jsxImportSource ,
388
+ } ,
389
+ } )
390
+ }
378
391
importCalls = [ ]
392
+ inserted = false
393
+ emotionTargetClassNameCount = 0
379
394
return distNode
380
395
}
381
396
}
0 commit comments