Skip to content

Commit

Permalink
feat(babel): add performant destructuring
Browse files Browse the repository at this point in the history
Per CRA, we add transform-destructuring with loose set to false.
However, for hooks, we turn selectiveLoose.
  • Loading branch information
swashata committed May 4, 2019
1 parent e55528d commit c1ecbd7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/babel-preset-base/__tests__/preset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('preset in module', () => {
noJsonStrings: true,
noRuntime: true,
});
expect(plugins).toHaveLength(0);
expect(plugins).toHaveLength(1);
});

describe('for @babel/preset-env', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/babel-preset-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@babel/plugin-proposal-json-strings": "^7.2.0",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-syntax-import-meta": "^7.2.0",
"@babel/plugin-transform-destructuring": "^7.4.4",
"@babel/plugin-transform-runtime": "^7.4.4",
"@babel/preset-env": "^7.4.4",
"@babel/preset-react": "^7.0.0",
Expand Down
24 changes: 24 additions & 0 deletions packages/babel-preset-base/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ export const preset = (opts: PresetOptions | null = {}) => {
}
});

// Necessary to include regardless of the environment because
// in practice some other transforms (such as object-rest-spread)
// don't work without it: https://github.com/babel/babel/issues/7215
plugins.push([
'@babel/plugin-transform-destructuring',
{
// Use loose mode for performance:
// https://github.com/facebook/create-react-app/issues/5602
loose: false,
selectiveLoose: [
'useState',
'useEffect',
'useContext',
'useReducer',
'useCallback',
'useMemo',
'useRef',
'useImperativeHandle',
'useLayoutEffect',
'useDebugValue',
],
},
]);

// Return the preset and some of stage-3 plugins
// We will remove them, once it becomes stage-4, i.e included in preset-env
return {
Expand Down

0 comments on commit c1ecbd7

Please sign in to comment.