This PR adds a Babel 7 plugin that aims to optimize certain React patterns that aren't as optimized as they might be. For example, with this plugin the following output is optimized as shown:
// Originalvar_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 pluginvaruseState=react__WEBPACK_IMPORTED_MODULE_1_.useState;var__ref__0=useState(Math.random());varvalue=__ref__0[0];
Named imports for React get transformed
// OriginalimportReact,{useState}from'react';// With this pluginimportReactfrom'react';const{useState}=React;
Array destructuring transform for React's built-in hooks
// Originalconst[counter,setCounter]=useState(0);// With this pluginconst__ref__0=useState(0);constcounter=__ref__0[0];constsetCounter=__ref__0[1];
React.createElement becomes a hoisted constant
// OriginalimportReactfrom'react';functionMyComponent(){returnReact.createElement('div',null,'Hello world');}// With this pluginimportReactfrom'react';const__reactCreateElement__=React.createElement;functionMyComponent(){return__reactCreateElement__('div',null,'Hello world');}