@@ -26,21 +26,19 @@ export interface Options {
2626 labelFormat ?: string
2727 autoInject ?: boolean
2828 customModules ?: ModuleConfig [ ]
29+ jsxFactory ?: string
30+ jsxImportSource ?: string
2931}
3032
3133const defaultEmotionModules : ModuleConfig [ ] = [
32- {
33- moduleName : 'emotion' ,
34- exportedNames : [ 'css' , 'keyframes' , 'injectGlobal' , 'cx' , 'merge' ] ,
35- } ,
3634 {
3735 moduleName : '@emotion/styled' ,
3836 exportedNames : [ 'styled' ] ,
3937 hasDefaultExport : true ,
4038 styledName : 'styled' ,
4139 } ,
4240 {
43- moduleName : '@emotion/core ' ,
41+ moduleName : '@emotion/react ' ,
4442 exportedNames : [ 'css' ] ,
4543 } ,
4644]
@@ -56,20 +54,24 @@ const getPackageRootPath = memoize((filename: string) => findRoot(filename))
5654const hashArray = ( arr : Array < string > ) => hashString ( arr . join ( '' ) )
5755
5856const createImportJSXAst = memoize ( ( propertyName : string | undefined ) => {
59- const importClause = ts . createImportClause (
57+ const importClause = ts . factory . createImportClause (
58+ false ,
6059 undefined ,
61- ts . createNamedImports ( [
60+ ts . factory . createNamedImports ( [
6261 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 ) ,
6665 )
67- : ts . createImportSpecifier ( undefined , ts . createIdentifier ( 'jsx' ) ) ,
66+ : ts . factory . createImportSpecifier (
67+ undefined ,
68+ ts . factory . createIdentifier ( 'jsx' ) ,
69+ ) ,
6870 ] ) ,
6971 )
70- const moduleSpecifier = ts . createStringLiteral ( '@emotion/core ' )
72+ const moduleSpecifier = ts . factory . createStringLiteral ( '@emotion/react ' )
7173
72- return ts . createImportDeclaration (
74+ return ts . factory . createImportDeclaration (
7375 undefined ,
7476 undefined ,
7577 importClause ,
@@ -162,20 +164,21 @@ export const createEmotionPlugin = (pluginOptions?: Options) => {
162164 let sourceFile : ts . SourceFile
163165 let inserted = false
164166 const visitor : ts . Visitor = ( node ) => {
165- if ( ts . isSourceFile ( node ) ) {
166- inserted = false
167- return ts . visitEachChild ( node , visitor , context )
168- }
169167 if ( ts . isImportDeclaration ( node ) ) {
170168 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
172170 if (
173171 ! inserted &&
174172 options . autoInject &&
175173 ( < ts . StringLiteral > node . moduleSpecifier ) . text === 'react'
176174 ) {
177175 inserted = true
178- return [ createImportJSXAst ( compilerOptions . jsxFactory ) , node ]
176+ return [
177+ createImportJSXAst (
178+ options ?. jsxFactory ?? compilerOptions . jsxFactory ,
179+ ) ,
180+ node ,
181+ ]
179182 }
180183 return node
181184 }
@@ -208,10 +211,10 @@ export const createEmotionPlugin = (pluginOptions?: Options) => {
208211 )
209212 } )
210213 if ( info ) {
211- expression = ts . createCall (
214+ expression = ts . factory . createCallExpression (
212215 expression . expression ,
213216 [ ] ,
214- [ ts . createStringLiteral ( expression . name . text ) ] ,
217+ [ ts . factory . createStringLiteral ( expression . name . text ) ] ,
215218 )
216219 }
217220 }
@@ -248,28 +251,28 @@ export const createEmotionPlugin = (pluginOptions?: Options) => {
248251 stuffToHash ,
249252 ) } ${ positionInFile } `
250253 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 ) ,
254257 )
255258 const args = [ el ]
256259 args . push (
257- ts . createObjectLiteral (
260+ ts . factory . createObjectLiteralExpression (
258261 opts && ts . isObjectLiteralExpression ( opts )
259262 ? opts . properties . concat ( targetAssignment )
260263 : [ targetAssignment ] ,
261264 true ,
262265 ) ,
263266 )
264267
265- const updatedCall = ts . updateCall (
268+ const updatedCall = ts . factory . updateCallExpression (
266269 exp ,
267270 exp . expression ,
268271 exp . typeArguments ,
269272 args ,
270273 )
271274
272- return ts . updateCall (
275+ return ts . factory . updateCallExpression (
273276 transformedNode ,
274277 updatedCall ,
275278 transformedNode . typeArguments ,
@@ -300,12 +303,12 @@ export const createEmotionPlugin = (pluginOptions?: Options) => {
300303 if ( localNameNode && ts . isIdentifier ( localNameNode ) ) {
301304 const local = localNameNode . text
302305 const fileName = basename ( rawPath , extname ( rawPath ) )
303- transformedNode = ts . updateCall (
306+ transformedNode = ts . factory . updateCallExpression (
304307 transformedNode ,
305308 transformedNode . expression ,
306309 transformedNode . typeArguments ,
307310 transformedNode . arguments . concat ( [
308- ts . createStringLiteral (
311+ ts . factory . createStringLiteral (
309312 `label:${ options
310313 . labelFormat ! . replace ( '[local]' , local )
311314 . replace ( '[filename]' , fileName ) } ;`,
@@ -345,12 +348,12 @@ export const createEmotionPlugin = (pluginOptions?: Options) => {
345348 const comment = convert
346349 . fromObject ( sourcemapGenerator )
347350 . toComment ( { multiline : true } )
348- transformedNode = ts . updateCall (
351+ transformedNode = ts . factory . updateCallExpression (
349352 transformedNode ,
350353 transformedNode . expression ,
351354 transformedNode . typeArguments ,
352355 transformedNode . arguments . concat ( [
353- ts . createStringLiteral ( comment ) ,
356+ ts . factory . createStringLiteral ( comment ) ,
354357 ] ) ,
355358 )
356359 }
@@ -375,7 +378,19 @@ export const createEmotionPlugin = (pluginOptions?: Options) => {
375378 sourceRoot : '' ,
376379 } )
377380 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+ }
378391 importCalls = [ ]
392+ inserted = false
393+ emotionTargetClassNameCount = 0
379394 return distNode
380395 }
381396 }
0 commit comments