forked from facebook/create-react-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Updated README.md #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
BtS-Style
merged 6 commits into
dependabot/npm_and_yarn/cross-fetch-3.1.6
from
add-babel-plugin-optimize-react
Mar 5, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bead342
Adds Babel plugin babel-plugin-optimize-react
trueadm 591ce25
Typo
trueadm 835fcbd
Increase version and added fix for no default import
trueadm f472a2a
Fixed more bugs + added more tests + bumped version
trueadm d335e0b
Adds support for more React imports and fixes a bunch of bugs
trueadm b22c4b3
Updated README.md
trueadm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| sandbox.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2013-present, Facebook, Inc. | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # babel-plugin-optimize-react | ||
|
|
||
| This Babel 7 plugin optimizes React hooks by transforming common patterns into more effecient output when using with tools such as [Create React App](https://github.com/facebook/create-react-app). For example, with this plugin the following output is optimized as shown: | ||
|
|
||
| ```js | ||
| // Original | ||
| var _useState = Object(react__WEBPACK_IMPORTED_MODULE_1_["useState"])(Math.random()), | ||
| _State2 = Object(_Users_gaearon_p_create_rreact_app_node_modules_babel_runtime_helpers_esm_sliceToArray_WEBPACK_IMPORTED_MODULE_0__["default"])(_useState, 1), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Use a consistent variable name in the code example ( Prompt for AI agents |
||
| value = _useState2[0]; | ||
|
|
||
| // With this plugin | ||
| var useState = react__WEBPACK_IMPORTED_MODULE_1_.useState; | ||
| var __ref__0 = useState(Math.random()); | ||
| var value = __ref__0[0]; | ||
| ``` | ||
|
|
||
| ## Named imports for React get transformed | ||
|
|
||
| ```js | ||
| // Original | ||
| import React, {memo, useState} from 'react'; | ||
|
|
||
| // With this plugin | ||
| import React from 'react'; | ||
| const {memo, useState} = React; | ||
| ``` | ||
|
|
||
| ## Array destructuring transform for React's built-in hooks | ||
|
|
||
| ```js | ||
| // Original | ||
| const [counter, setCounter] = useState(0); | ||
|
|
||
| // With this plugin | ||
| const __ref__0 = useState(0); | ||
| const counter = __ref__0[0]; | ||
| const setCounter = __ref__0[1]; | ||
| ``` | ||
|
|
||
| ## React.createElement becomes a hoisted constant | ||
|
|
||
| ```js | ||
| // Original | ||
| import React from 'react'; | ||
|
|
||
| function MyComponent() { | ||
| return React.createElement('div', null, 'Hello world'); | ||
| } | ||
|
|
||
| // With this plugin | ||
| import React from 'react'; | ||
| const __reactCreateElement__ = React.createElement; | ||
|
|
||
| function MyComponent() { | ||
| return __reactCreateElement__('div', null, 'Hello world'); | ||
| } | ||
| ``` | ||
|
|
||
41 changes: 41 additions & 0 deletions
41
packages/babel-plugin-optimize-react/__tests__/__snapshots__/createElement-test.js.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`React createElement transforms should transform React.createElement calls #2 1`] = ` | ||
| "const React = require(\\"react\\"); | ||
|
|
||
| const __reactCreateElement__ = React.createElement; | ||
| export function MyComponent() { | ||
| return __reactCreateElement__(\\"div\\", null, __reactCreateElement__(\\"span\\", null, \\"Hello world!\\")); | ||
| }" | ||
| `; | ||
|
|
||
| exports[`React createElement transforms should transform React.createElement calls #3 1`] = ` | ||
| "const React = require(\\"react\\"); | ||
|
|
||
| const __reactCreateElement__ = React.createElement; | ||
|
|
||
| const node = __reactCreateElement__(\\"div\\", null, __reactCreateElement__(\\"span\\", null, \\"Hello world!\\")); | ||
|
|
||
| export function MyComponent() { | ||
| return node; | ||
| }" | ||
| `; | ||
|
|
||
| exports[`React createElement transforms should transform React.createElement calls #4 1`] = ` | ||
| "import * as React from \\"react\\"; | ||
| const __reactCreateElement__ = React.createElement; | ||
|
|
||
| const node = __reactCreateElement__(\\"div\\", null, __reactCreateElement__(\\"span\\", null, \\"Hello world!\\")); | ||
|
|
||
| export function MyComponent() { | ||
| return node; | ||
| }" | ||
| `; | ||
|
|
||
| exports[`React createElement transforms should transform React.createElement calls 1`] = ` | ||
| "import React from \\"react\\"; | ||
| const __reactCreateElement__ = React.createElement; | ||
| export function MyComponent() { | ||
| return __reactCreateElement__(\\"div\\"); | ||
| }" | ||
| `; |
145 changes: 145 additions & 0 deletions
145
packages/babel-plugin-optimize-react/__tests__/__snapshots__/hooks-test.js.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`React hook transforms should support destructuring hooks from imports #2 1`] = ` | ||
| "import React from \\"react\\"; | ||
| const __reactCreateElement__ = React.createElement; | ||
| const { | ||
| useState | ||
| } = React; | ||
| export function MyComponent() { | ||
| const _ref_0 = useState(0); | ||
|
|
||
| const setCounter = _ref_0[1]; | ||
| const counter = _ref_0[0]; | ||
| return __reactCreateElement__(\\"div\\", null, counter); | ||
| }" | ||
| `; | ||
|
|
||
| exports[`React hook transforms should support destructuring hooks from imports #3 1`] = ` | ||
| "import React from \\"react\\"; | ||
| const __reactCreateElement__ = React.createElement; | ||
| const useState = React.useState; | ||
| export function MyComponent() { | ||
| const _ref_0 = useState(0); | ||
|
|
||
| const setCounter = _ref_0[1]; | ||
| const counter = _ref_0[0]; | ||
| return __reactCreateElement__(\\"div\\", null, counter); | ||
| }" | ||
| `; | ||
|
|
||
| exports[`React hook transforms should support destructuring hooks from imports #4 1`] = ` | ||
| "import React from \\"react\\"; | ||
| const __reactCreateElement__ = React.createElement; | ||
| const foo = React.useState; | ||
| export function MyComponent() { | ||
| const _ref_0 = foo(0); | ||
|
|
||
| const setCounter = _ref_0[1]; | ||
| const counter = _ref_0[0]; | ||
| return __reactCreateElement__(\\"div\\", null, counter); | ||
| }" | ||
| `; | ||
|
|
||
| exports[`React hook transforms should support destructuring hooks from imports #5 1`] = ` | ||
| "import React from \\"react\\"; | ||
| const __reactCreateElement__ = React.createElement; | ||
| const { | ||
| useState: foo | ||
| } = React; | ||
| export function MyComponent() { | ||
| const _ref_0 = foo(0); | ||
|
|
||
| const setCounter = _ref_0[1]; | ||
| const counter = _ref_0[0]; | ||
| return __reactCreateElement__(\\"div\\", null, counter); | ||
| }" | ||
| `; | ||
|
|
||
| exports[`React hook transforms should support destructuring hooks from imports 1`] = ` | ||
| "import React from \\"react\\"; | ||
| const __reactCreateElement__ = React.createElement; | ||
| const { | ||
| useState | ||
| } = React; | ||
| export function MyComponent() { | ||
| const _ref_0 = useState(0); | ||
|
|
||
| const setCounter = _ref_0[1]; | ||
| const counter = _ref_0[0]; | ||
| return __reactCreateElement__(\\"div\\", null, counter); | ||
| }" | ||
| `; | ||
|
|
||
| exports[`React hook transforms should support destructuring hooks from require calls 1`] = ` | ||
| "const React = require(\\"react\\"); | ||
|
|
||
| const __reactCreateElement__ = React.createElement; | ||
| const { | ||
| useState | ||
| } = React; | ||
| export function MyComponent() { | ||
| const _ref_0 = useState(0); | ||
|
|
||
| const setCounter = _ref_0[1]; | ||
| const counter = _ref_0[0]; | ||
| return __reactCreateElement__(\\"div\\", null, counter); | ||
| }" | ||
| `; | ||
|
|
||
| exports[`React hook transforms should support hook CJS require with no default 1`] = ` | ||
| "const { | ||
| useState | ||
| } = require(\\"react\\");" | ||
| `; | ||
|
|
||
| exports[`React hook transforms should support hook imports with aliasing 1`] = ` | ||
| "import React from \\"react\\"; | ||
| const { | ||
| useState: foo | ||
| } = React;" | ||
| `; | ||
|
|
||
| exports[`React hook transforms should support hook imports with no default 1`] = ` | ||
| "import React from \\"react\\"; | ||
| const { | ||
| useState | ||
| } = React;" | ||
| `; | ||
|
|
||
| exports[`React hook transforms should support mixed hook imports 1`] = ` | ||
| "import React from \\"react\\"; | ||
| import \\"react\\"; | ||
| const { | ||
| memo, | ||
| useState | ||
| } = React;" | ||
| `; | ||
|
|
||
| exports[`React hook transforms should support mixed hook imports with no default #2 1`] = ` | ||
| "import React from \\"react\\"; | ||
| const { | ||
| memo, | ||
| useRef, | ||
| useState | ||
| } = React; | ||
| export const Portal = memo(() => null);" | ||
| `; | ||
|
|
||
| exports[`React hook transforms should support mixed hook imports with no default 1`] = ` | ||
| "import React from \\"react\\"; | ||
| const { | ||
| useState | ||
| } = React; | ||
| import \\"react\\"; | ||
| const { | ||
| memo | ||
| } = React;" | ||
| `; | ||
|
|
||
| exports[`React hook transforms should support transform hook imports 1`] = ` | ||
| "import React from \\"react\\"; | ||
| const { | ||
| useState | ||
| } = React;" | ||
| `; |
64 changes: 64 additions & 0 deletions
64
packages/babel-plugin-optimize-react/__tests__/createElement-test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| 'use strict'; | ||
|
|
||
| const plugin = require('../index.js'); | ||
| const babel = require('@babel/core'); | ||
|
|
||
| function transform(code) { | ||
| return babel.transform(code, { | ||
| plugins: [plugin], | ||
| }).code; | ||
| } | ||
|
|
||
| describe('React createElement transforms', () => { | ||
| it('should transform React.createElement calls', () => { | ||
| const test = ` | ||
| import React from "react"; | ||
|
|
||
| export function MyComponent() { | ||
| return React.createElement("div"); | ||
| } | ||
| `; | ||
| const output = transform(test); | ||
| expect(output).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it('should transform React.createElement calls #2', () => { | ||
| const test = ` | ||
| const React = require("react"); | ||
|
|
||
| export function MyComponent() { | ||
| return React.createElement("div", null, React.createElement("span", null, "Hello world!")); | ||
| } | ||
| `; | ||
| const output = transform(test); | ||
| expect(output).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it('should transform React.createElement calls #3', () => { | ||
| const test = ` | ||
| const React = require("react"); | ||
|
|
||
| const node = React.createElement("div", null, React.createElement("span", null, "Hello world!")); | ||
|
|
||
| export function MyComponent() { | ||
| return node; | ||
| } | ||
| `; | ||
| const output = transform(test); | ||
| expect(output).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it('should transform React.createElement calls #4', () => { | ||
| const test = ` | ||
| import * as React from "react"; | ||
|
|
||
| const node = React.createElement("div", null, React.createElement("span", null, "Hello world!")); | ||
|
|
||
| export function MyComponent() { | ||
| return node; | ||
| } | ||
| `; | ||
| const output = transform(test); | ||
| expect(output).toMatchSnapshot(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: Fix the typo in the intro (
effecient→efficient) to keep the README polished and professional.Prompt for AI agents