Skip to content
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

12 #13584

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

12 #13584

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13,210 changes: 10,962 additions & 2,248 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/babel-plugin-optimize-react/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sandbox.js
21 changes: 21 additions & 0 deletions packages/babel-plugin-optimize-react/LICENSE
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.
58 changes: 58 additions & 0 deletions packages/babel-plugin-optimize-react/README.md
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),
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');
}
```

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\\");
}"
`;
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;"
`;
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();
});
});
Loading