-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize hook destructuring (#22619)
* Add optimize-hook-destructing package * feat(gatsby): Optimize hook destructuring to improve performance * fix lint * fix builds and clean up tests * Undo specific package * update test * yarn lint:code * format * fix type errors * use interface
- Loading branch information
1 parent
015f821
commit b3343ab
Showing
7 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
{ | ||
"presets": [["babel-preset-gatsby-package"]] | ||
"presets": [["babel-preset-gatsby-package"]], | ||
"overrides": [ | ||
{ | ||
"test": ["**/*.ts", "**/*.tsx"], | ||
"plugins": [["@babel/plugin-transform-typescript", { "isTSX": true }]] | ||
} | ||
] | ||
} |
This file contains 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
This file contains 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
39 changes: 39 additions & 0 deletions
39
packages/babel-preset-gatsby/src/__tests__/optimize-hook-destructuring.js
This file contains 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,39 @@ | ||
import { transform } from "@babel/core" | ||
import preset from "babel-preset-gatsby" | ||
import plugin from "../optimize-hook-destructuring" | ||
|
||
const trim = s => s.join(`\n`).trim().replace(/^\s+/gm, ``) | ||
|
||
const babel = code => | ||
transform(code, { | ||
filename: `noop.js`, | ||
presets: [preset], | ||
plugins: [plugin], | ||
babelrc: false, | ||
configFile: false, | ||
sourceType: `module`, | ||
compact: true, | ||
caller: { | ||
name: `tests`, | ||
supportsStaticESM: true, | ||
}, | ||
}).code | ||
|
||
describe(`optimize-hook-destructuring`, () => { | ||
it(`should transform Array-destructured hook return values use object destructuring`, () => { | ||
const output = babel( | ||
trim` | ||
import { useState } from 'react'; | ||
const [count, setCount] = useState(0); | ||
` | ||
) | ||
|
||
expect(output).toMatch(trim` | ||
\"use strict\";var _react=require(\"react\");const{0:count,1:setCount}=(0,_react.useState)(0); | ||
`) | ||
|
||
expect(output).toMatchInlineSnapshot( | ||
`"\\"use strict\\";var _react=require(\\"react\\");const{0:count,1:setCount}=(0,_react.useState)(0);"` | ||
) | ||
}) | ||
}) |
This file contains 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
71 changes: 71 additions & 0 deletions
71
packages/babel-preset-gatsby/src/optimize-hook-destructuring.ts
This file contains 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,71 @@ | ||
import { NodePath, PluginObj, Visitor } from "@babel/core" | ||
import * as BabelTypes from "@babel/types" | ||
import { Program } from "@babel/types" | ||
|
||
// matches any hook-like (the default) | ||
const isHook = /^use[A-Z]/ | ||
|
||
// matches only built-in hooks provided by React et al | ||
const isBuiltInHook = /^use(Callback|Context|DebugValue|Effect|ImperativeHandle|LayoutEffect|Memo|Reducer|Ref|State)$/ | ||
|
||
interface IState { | ||
opts?: { | ||
onlyBuiltIns?: boolean | ||
lib?: boolean | ||
} | ||
} | ||
|
||
export default function ({ | ||
types: t, | ||
}: { | ||
types: typeof BabelTypes | ||
}): PluginObj<Program> { | ||
const visitor: Visitor = { | ||
CallExpression(path, state: IState): void { | ||
const onlyBuiltIns = state.opts?.onlyBuiltIns || false | ||
|
||
// if specified, options.lib is a list of libraries that provide hook functions | ||
const libs = | ||
state.opts?.lib === true ? [`react`, `preact/hooks`] : [state.opts!.lib] | ||
|
||
// skip function calls that are not the init of a variable declaration: | ||
if (!t.isVariableDeclarator(path.parent)) return | ||
|
||
// skip function calls where the return value is not Array-destructured: | ||
if (!t.isArrayPattern(path.parent.id)) return | ||
|
||
// name of the (hook) function being called: | ||
const hookName = (path.node.callee as BabelTypes.Identifier).name | ||
|
||
if (libs) { | ||
const binding = path.scope.getBinding(hookName) | ||
// not an import | ||
if (!binding || binding.kind !== `module`) return | ||
|
||
const specifier = (binding.path.parent as BabelTypes.ImportDeclaration) | ||
.source.value | ||
// not a match | ||
if (!libs.some(lib => lib === specifier)) return | ||
} | ||
|
||
// only match function calls with names that look like a hook | ||
if (!(onlyBuiltIns ? isBuiltInHook : isHook).test(hookName)) return | ||
|
||
path.parent.id = t.objectPattern( | ||
path.parent.id.elements.map((element, i) => | ||
t.objectProperty(t.numericLiteral(i), element!) | ||
) | ||
) | ||
}, | ||
} | ||
|
||
return { | ||
name: `optimize-hook-destructuring`, | ||
visitor: { | ||
// this is a workaround to run before preset-env destroys destructured assignments | ||
Program<Program>(path: NodePath<Program>, state: any): void { | ||
path.traverse(visitor, state) | ||
}, | ||
}, | ||
} | ||
} |
This file contains 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