Skip to content

Commit c1ecbd7

Browse files
committed
feat(babel): add performant destructuring
Per CRA, we add transform-destructuring with loose set to false. However, for hooks, we turn selectiveLoose.
1 parent e55528d commit c1ecbd7

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

packages/babel-preset-base/__tests__/preset.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('preset in module', () => {
7373
noJsonStrings: true,
7474
noRuntime: true,
7575
});
76-
expect(plugins).toHaveLength(0);
76+
expect(plugins).toHaveLength(1);
7777
});
7878

7979
describe('for @babel/preset-env', () => {

packages/babel-preset-base/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@babel/plugin-proposal-json-strings": "^7.2.0",
2323
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
2424
"@babel/plugin-syntax-import-meta": "^7.2.0",
25+
"@babel/plugin-transform-destructuring": "^7.4.4",
2526
"@babel/plugin-transform-runtime": "^7.4.4",
2627
"@babel/preset-env": "^7.4.4",
2728
"@babel/preset-react": "^7.0.0",

packages/babel-preset-base/src/preset.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,30 @@ export const preset = (opts: PresetOptions | null = {}) => {
101101
}
102102
});
103103

104+
// Necessary to include regardless of the environment because
105+
// in practice some other transforms (such as object-rest-spread)
106+
// don't work without it: https://github.com/babel/babel/issues/7215
107+
plugins.push([
108+
'@babel/plugin-transform-destructuring',
109+
{
110+
// Use loose mode for performance:
111+
// https://github.com/facebook/create-react-app/issues/5602
112+
loose: false,
113+
selectiveLoose: [
114+
'useState',
115+
'useEffect',
116+
'useContext',
117+
'useReducer',
118+
'useCallback',
119+
'useMemo',
120+
'useRef',
121+
'useImperativeHandle',
122+
'useLayoutEffect',
123+
'useDebugValue',
124+
],
125+
},
126+
]);
127+
104128
// Return the preset and some of stage-3 plugins
105129
// We will remove them, once it becomes stage-4, i.e included in preset-env
106130
return {

0 commit comments

Comments
 (0)