66 */
77
88import type * as BabelCore from '@babel/core' ;
9- import { compileProgram , parsePluginOptions } from '../Entrypoint' ;
9+ import { compileProgram , Logger , parsePluginOptions } from '../Entrypoint' ;
1010import {
1111 injectReanimatedFlag ,
1212 pipelineUsesReanimatedPlugin ,
1313} from '../Entrypoint/Reanimated' ;
1414
15+ const ENABLE_REACT_COMPILER_TIMINGS =
16+ process . env [ 'ENABLE_REACT_COMPILER_TIMINGS' ] === '1' ;
17+
1518/*
1619 * The React Forget Babel Plugin
1720 * @param {* } _babel
@@ -28,35 +31,65 @@ export default function BabelPluginReactCompiler(
2831 * prior to B, if A does not have a Program visitor and B does, B will run first. We always
2932 * want Forget to run true to source as possible.
3033 */
31- Program ( prog , pass ) : void {
32- let opts = parsePluginOptions ( pass . opts ) ;
33- const isDev =
34- ( typeof __DEV__ !== 'undefined' && __DEV__ === true ) ||
35- process . env [ 'NODE_ENV' ] === 'development' ;
36- if (
37- opts . enableReanimatedCheck === true &&
38- pipelineUsesReanimatedPlugin ( pass . file . opts . plugins )
39- ) {
40- opts = injectReanimatedFlag ( opts ) ;
41- }
42- if (
43- opts . environment . enableResetCacheOnSourceFileChanges !== false &&
44- isDev
45- ) {
46- opts = {
47- ...opts ,
48- environment : {
49- ...opts . environment ,
50- enableResetCacheOnSourceFileChanges : true ,
51- } ,
52- } ;
53- }
54- compileProgram ( prog , {
55- opts,
56- filename : pass . filename ?? null ,
57- comments : pass . file . ast . comments ?? [ ] ,
58- code : pass . file . code ,
59- } ) ;
34+ Program : {
35+ enter ( prog , pass ) : void {
36+ const filename = pass . filename ?? 'unknown' ;
37+ if ( ENABLE_REACT_COMPILER_TIMINGS === true ) {
38+ performance . mark ( `${ filename } :start` , {
39+ detail : 'BabelPlugin:Program:start' ,
40+ } ) ;
41+ }
42+ let opts = parsePluginOptions ( pass . opts ) ;
43+ const isDev =
44+ ( typeof __DEV__ !== 'undefined' && __DEV__ === true ) ||
45+ process . env [ 'NODE_ENV' ] === 'development' ;
46+ if (
47+ opts . enableReanimatedCheck === true &&
48+ pipelineUsesReanimatedPlugin ( pass . file . opts . plugins )
49+ ) {
50+ opts = injectReanimatedFlag ( opts ) ;
51+ }
52+ if (
53+ opts . environment . enableResetCacheOnSourceFileChanges !== false &&
54+ isDev
55+ ) {
56+ opts = {
57+ ...opts ,
58+ environment : {
59+ ...opts . environment ,
60+ enableResetCacheOnSourceFileChanges : true ,
61+ } ,
62+ } ;
63+ }
64+ compileProgram ( prog , {
65+ opts,
66+ filename : pass . filename ?? null ,
67+ comments : pass . file . ast . comments ?? [ ] ,
68+ code : pass . file . code ,
69+ } ) ;
70+ if ( ENABLE_REACT_COMPILER_TIMINGS === true ) {
71+ performance . mark ( `${ filename } :end` , {
72+ detail : 'BabelPlugin:Program:end' ,
73+ } ) ;
74+ }
75+ } ,
76+ exit ( _ , pass ) : void {
77+ if ( ENABLE_REACT_COMPILER_TIMINGS === true ) {
78+ const filename = pass . filename ?? 'unknown' ;
79+ const measurement = performance . measure ( filename , {
80+ start : `${ filename } :start` ,
81+ end : `${ filename } :end` ,
82+ detail : 'BabelPlugin:Program' ,
83+ } ) ;
84+ if ( 'logger' in pass . opts && pass . opts . logger != null ) {
85+ const logger : Logger = pass . opts . logger as Logger ;
86+ logger . logEvent ( filename , {
87+ kind : 'Timing' ,
88+ measurement,
89+ } ) ;
90+ }
91+ }
92+ } ,
6093 } ,
6194 } ,
6295 } ;
0 commit comments