@@ -24,19 +24,22 @@ let ignoreRegex;
24
24
export default ( { types : t } ) => ( {
25
25
visitor : {
26
26
Program : {
27
- enter ( { scope, node } ) {
27
+ enter ( { scope, node } , { file } ) {
28
28
if ( ! scope . hasBinding ( 'React' ) ) {
29
29
const reactImportDeclaration = t . importDeclaration ( [
30
30
t . importDefaultSpecifier ( t . identifier ( 'React' ) ) ,
31
31
] , t . stringLiteral ( 'react' ) ) ;
32
32
33
- node . body . unshift ( reactImportDeclaration ) ;
33
+ file . set ( 'ensureReact' , ( ) => { node . body . unshift ( reactImportDeclaration ) ; } ) ;
34
+ } else {
35
+ file . set ( 'ensureReact' , ( ) => { } ) ;
34
36
}
35
37
} ,
36
38
} ,
37
39
ImportDeclaration ( path , state ) {
38
40
const importPath = path . node . source . value ;
39
41
const { ignorePattern, caseSensitive } = state . opts ;
42
+ const { file } = state ;
40
43
if ( ignorePattern ) {
41
44
// Only set the ignoreRegex once:
42
45
ignoreRegex = ignoreRegex || new RegExp ( ignorePattern ) ;
@@ -104,6 +107,7 @@ export default ({ types: t }) => ({
104
107
const svgReplacement = buildSvg ( opts ) ;
105
108
path . replaceWith ( svgReplacement ) ;
106
109
}
110
+ file . get ( 'ensureReact' ) ( ) ;
107
111
}
108
112
} ,
109
113
} ,
0 commit comments