mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Implements constant propagation/constant folding for a conservative subset of the language. The approach is described in detail in the comments in the file itself, a key note here is that this pass currently emits what looks like garbage: ``` // input const x = 1; const y = x + 1; // output const x = 1; 2; // <---- you'll see a bunch of lines like this const y = 2; ``` These useless lines occur where previously there was a temporary getting calculated that was used later (so we saved it until it was used), but now it isn't used later so we just emit it in-place. Dead code elimination (DCE) can eliminate these and other useless statements later. Note that a key motivation for implementing this pass is to reduce memoization blocks to what is strictly required for dynamic computations. Why memoize at runtime when we compute at build time?
test262
@ 83a46bfe0e
React Forget
React Forget is an experimental Babel plugin to automatically memoize React Hooks and Components.
Development
# tsc --watch
$ yarn dev
# in another terminal window
$ yarn test --watch
Notes
An overview of the implementation can be found in the Architecture Overview.
This transform
- needs plugin-syntax-jsx as a dependency to inherit the syntax from.
- should be run before plugin-transform-react-jsx
- assume the enforcement of rules of hooks, i.e.
- only call hooks from React functions
- only call hooks at the top level
- https://www.npmjs.com/package/eslint-plugin-react-hooks
Scaffolding
- https://github.com/facebook/flow/tree/master/packages/babel-plugin-transform-flow-enums
- https://github.com/babel/babel/blob/main/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts
Reference