5
5
* LICENSE file in the root directory of this source tree.
6
6
*/
7
7
8
- import { jsx } from '@babel/plugin-syntax-jsx' ;
9
8
import babelJest from 'babel-jest' ;
10
- import { compile } from 'babel-plugin-react-compiler' ;
11
- import { execSync } from 'child_process' ;
12
-
13
- import type { NodePath , Visitor } from '@babel/traverse' ;
14
- import type { CallExpression , FunctionDeclaration } from '@babel/types' ;
15
- import * as t from '@babel/types' ;
16
9
import {
17
- EnvironmentConfig ,
18
10
validateEnvironmentConfig ,
11
+ EnvironmentConfig ,
19
12
} from 'babel-plugin-react-compiler' ;
20
- import { basename } from 'path' ;
13
+ import { execSync } from 'child_process' ;
14
+
15
+ import type { NodePath , Visitor } from '@babel/traverse' ;
16
+ import type { CallExpression } from '@babel/types' ;
17
+ import BabelPluginReactCompiler from 'babel-plugin-react-compiler' ;
21
18
22
19
/**
23
20
* -- IMPORTANT --
@@ -28,10 +25,19 @@ import {basename} from 'path';
28
25
const e2eTransformerCacheKey = 1 ;
29
26
const forgetOptions : EnvironmentConfig = validateEnvironmentConfig ( {
30
27
enableAssumeHooksFollowRulesOfReact : true ,
31
- enableFunctionOutlining : false ,
32
28
} ) ;
33
29
const debugMode = process . env [ 'DEBUG_FORGET_COMPILER' ] != null ;
34
30
31
+ const compilerCacheKey = execSync (
32
+ 'yarn --silent --cwd ../.. hash packages/babel-plugin-react-compiler/dist' ,
33
+ )
34
+ . toString ( )
35
+ . trim ( ) ;
36
+
37
+ if ( debugMode ) {
38
+ console . log ( 'cachebreaker' , compilerCacheKey ) ;
39
+ }
40
+
35
41
module . exports = ( useForget : boolean ) => {
36
42
function createTransformer ( ) {
37
43
return babelJest . createTransformer ( {
@@ -42,15 +48,14 @@ module.exports = (useForget: boolean) => {
42
48
plugins : [
43
49
useForget
44
50
? [
45
- ReactForgetFunctionTransform ,
51
+ BabelPluginReactCompiler ,
46
52
{
53
+ environment : forgetOptions ,
47
54
/*
48
55
* Jest hashes the babel config as a cache breaker.
49
56
* (see https://github.com/jestjs/jest/blob/v29.6.2/packages/babel-jest/src/index.ts#L84)
50
57
*/
51
- compilerCacheKey : execSync (
52
- 'yarn --silent --cwd ../.. hash packages/babel-plugin-react-compiler/dist' ,
53
- ) . toString ( ) ,
58
+ compilerCacheKey,
54
59
transformOptionsCacheKey : forgetOptions ,
55
60
e2eTransformerCacheKey,
56
61
} ,
@@ -105,104 +110,3 @@ module.exports = (useForget: boolean) => {
105
110
createTransformer,
106
111
} ;
107
112
} ;
108
-
109
- // Mostly copied from react/scripts/babel/transform-forget.js
110
- function isReactComponentLike ( fn : NodePath < FunctionDeclaration > ) : boolean {
111
- let isReactComponent = false ;
112
- let hasNoUseForgetDirective = false ;
113
-
114
- /*
115
- * React components start with an upper case letter,
116
- * React hooks start with `use`
117
- */
118
- if (
119
- fn . node . id == null ||
120
- ( fn . node . id . name [ 0 ] . toUpperCase ( ) !== fn . node . id . name [ 0 ] &&
121
- ! / ^ u s e [ A - Z 0 - 9 ] / . test ( fn . node . id . name ) )
122
- ) {
123
- return false ;
124
- }
125
-
126
- fn . traverse ( {
127
- DirectiveLiteral ( path ) {
128
- if ( path . node . value === 'use no forget' ) {
129
- hasNoUseForgetDirective = true ;
130
- }
131
- } ,
132
-
133
- JSX ( path ) {
134
- // Is there is a JSX node created in the current function context?
135
- if ( path . scope . getFunctionParent ( ) ?. path . node === fn . node ) {
136
- isReactComponent = true ;
137
- }
138
- } ,
139
-
140
- CallExpression ( path ) {
141
- // Is there hook usage?
142
- if (
143
- path . node . callee . type === 'Identifier' &&
144
- ! / ^ u s e [ A - Z 0 - 9 ] / . test ( path . node . callee . name )
145
- ) {
146
- isReactComponent = true ;
147
- }
148
- } ,
149
- } ) ;
150
-
151
- if ( hasNoUseForgetDirective ) {
152
- return false ;
153
- }
154
-
155
- return isReactComponent ;
156
- }
157
-
158
- function ReactForgetFunctionTransform ( ) {
159
- const compiledFns = new Set ( ) ;
160
- const visitor = {
161
- FunctionDeclaration ( fn : NodePath < FunctionDeclaration > , state : any ) : void {
162
- if ( compiledFns . has ( fn . node ) ) {
163
- return ;
164
- }
165
-
166
- if ( ! isReactComponentLike ( fn ) ) {
167
- return ;
168
- }
169
- if ( debugMode ) {
170
- const filename = basename ( state . file . opts . filename ) ;
171
- if ( fn . node . loc && fn . node . id ) {
172
- console . log (
173
- ` Compiling ${ filename } :${ fn . node . loc . start . line } :${ fn . node . loc . start . column } ${ fn . node . id . name } ` ,
174
- ) ;
175
- } else {
176
- console . log ( ` Compiling ${ filename } ${ fn . node . id ?. name } ` ) ;
177
- }
178
- }
179
-
180
- const compiled = compile (
181
- fn ,
182
- forgetOptions ,
183
- 'Other' ,
184
- 'all_features' ,
185
- '_c' ,
186
- null ,
187
- null ,
188
- null ,
189
- ) ;
190
- compiledFns . add ( compiled ) ;
191
-
192
- const fun = t . functionDeclaration (
193
- compiled . id ,
194
- compiled . params ,
195
- compiled . body ,
196
- compiled . generator ,
197
- compiled . async ,
198
- ) ;
199
- fn . replaceWith ( fun ) ;
200
- fn . skip ( ) ;
201
- } ,
202
- } ;
203
- return {
204
- name : 'react-forget-e2e' ,
205
- inherits : jsx ,
206
- visitor,
207
- } ;
208
- }
0 commit comments