mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Compiler playground now runs the entire program through `babel-plugin-react-compiler` instead of a custom pipeline which previously duplicated function inference logic from `Program.ts`. In addition, the playground output reflects the tranformed file (instead of a "virtual file" of manually concatenated functions). This helps with the following: - Reduce potential discrepencies between playground and babel plugin behavior. See attached fixture output for an example where we previously diverged. - Let playground users see compiler-inserted imports (e.g. `_c` or `useFire`) This also helps us repurpose playground into a more general tool for compiler-users instead of just for compiler engineers. - imports and other functions are preserved. We differentiate between imports and globals in many cases (e.g. `inferEffectDeps`), so it may be misleading to omit imports in printed output - playground now shows other program-changing behavior like position of outlined functions and hoisted declarations - emitted compiled functions do not need synthetic names --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/31774). * #31809 * __->__ #31774
64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
import type * as BabelCore from '@babel/core';
|
|
import {compileProgram, parsePluginOptions} from '../Entrypoint';
|
|
import {
|
|
injectReanimatedFlag,
|
|
pipelineUsesReanimatedPlugin,
|
|
} from '../Entrypoint/Reanimated';
|
|
|
|
/*
|
|
* The React Forget Babel Plugin
|
|
* @param {*} _babel
|
|
* @returns
|
|
*/
|
|
export default function BabelPluginReactCompiler(
|
|
_babel: typeof BabelCore,
|
|
): BabelCore.PluginObj {
|
|
return {
|
|
name: 'react-forget',
|
|
visitor: {
|
|
/*
|
|
* Note: Babel does some "smart" merging of visitors across plugins, so even if A is inserted
|
|
* prior to B, if A does not have a Program visitor and B does, B will run first. We always
|
|
* want Forget to run true to source as possible.
|
|
*/
|
|
Program(prog, pass): void {
|
|
let opts = parsePluginOptions(pass.opts);
|
|
const isDev =
|
|
(typeof __DEV__ !== 'undefined' && __DEV__ === true) ||
|
|
process.env['NODE_ENV'] === 'development';
|
|
if (
|
|
opts.enableReanimatedCheck === true &&
|
|
pipelineUsesReanimatedPlugin(pass.file.opts.plugins)
|
|
) {
|
|
opts = injectReanimatedFlag(opts);
|
|
}
|
|
if (
|
|
opts.environment.enableResetCacheOnSourceFileChanges !== false &&
|
|
isDev
|
|
) {
|
|
opts = {
|
|
...opts,
|
|
environment: {
|
|
...opts.environment,
|
|
enableResetCacheOnSourceFileChanges: true,
|
|
},
|
|
};
|
|
}
|
|
compileProgram(prog, {
|
|
opts,
|
|
filename: pass.filename ?? null,
|
|
comments: pass.file.ast.comments ?? [],
|
|
code: pass.file.code,
|
|
});
|
|
},
|
|
},
|
|
};
|
|
}
|