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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tree-shaking in blade components #1045

Merged
merged 13 commits into from
Mar 23, 2023
27 changes: 17 additions & 10 deletions packages/blade/src/components/ActionList/ActionListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { useDropdown } from '~components/Dropdown/useDropdown';
import type { Feedback } from '~tokens/theme/theme';
import { Text } from '~components/Typography';
import { isReactNative, makeAccessible, makeSize, metaAttribute, MetaConstants } from '~utils';
import type { WithComponentId } from '~utils';
import { Checkbox } from '~components/Checkbox';
import size from '~tokens/global/size';
import type { StringChildrenType } from '~src/_helpers/types';
Expand Down Expand Up @@ -97,11 +96,11 @@ type ActionListSectionProps = {
*/
_hideDivider?: boolean;
};
const ActionListSection: WithComponentId<ActionListSectionProps> = ({
const _ActionListSection = ({
title,
children,
_hideDivider,
}): JSX.Element => {
}: ActionListSectionProps): JSX.Element => {
return (
<BaseBox
{...makeAccessible({
Expand Down Expand Up @@ -130,9 +129,11 @@ const ActionListSection: WithComponentId<ActionListSectionProps> = ({
);
};

ActionListSection.componentId = componentIds.ActionListSection;
const ActionListSection = /*#__PURE__*/ Object.assign(_ActionListSection, {
componentId: componentIds.ActionListSection,
});

const ActionListItemIcon: WithComponentId<{ icon: IconComponent }> = ({ icon }): JSX.Element => {
const _ActionListItemIcon = ({ icon }: { icon: IconComponent }): JSX.Element => {
const Icon = icon;
const { intent, isDisabled } = React.useContext(ActionListItemContext);
return (
Expand All @@ -147,9 +148,11 @@ const ActionListItemIcon: WithComponentId<{ icon: IconComponent }> = ({ icon }):
);
};

ActionListItemIcon.componentId = componentIds.ActionListItemIcon;
const ActionListItemIcon = /*#__PURE__*/ Object.assign(_ActionListItemIcon, {
componentId: componentIds.ActionListItemIcon,
});

const ActionListItemText: WithComponentId<{ children: StringChildrenType }> = ({ children }) => {
const _ActionListItemText = ({ children }: { children: StringChildrenType }): JSX.Element => {
const { isDisabled } = React.useContext(ActionListItemContext);

return (
Expand All @@ -159,7 +162,9 @@ const ActionListItemText: WithComponentId<{ children: StringChildrenType }> = ({
);
};

ActionListItemText.componentId = componentIds.ActionListItemText;
const ActionListItemText = /*#__PURE__*/ Object.assign(_ActionListItemText, {
componentId: componentIds.ActionListItemText,
});

const ActionListCheckboxWrapper = styled(BaseBox)<{ hasDescription: boolean }>((_props) => ({
pointerEvents: 'none',
Expand Down Expand Up @@ -199,7 +204,7 @@ const makeActionListItemClickable = (
* </ActionList>
* ```
*/
const ActionListItem: WithComponentId<ActionListItemProps> = (props): JSX.Element => {
const _ActionListItem = (props: ActionListItemProps): JSX.Element => {
const {
activeIndex,
dropdownBaseId,
Expand Down Expand Up @@ -332,7 +337,9 @@ const ActionListItem: WithComponentId<ActionListItemProps> = (props): JSX.Elemen
);
};

ActionListItem.componentId = componentIds.ActionListItem;
const ActionListItem = /*#__PURE__*/ Object.assign(_ActionListItem, {
componentId: componentIds.ActionListItem,
});

export {
ActionListItem,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ const _BaseButton: React.ForwardRefRenderFunction<BladeElementRef, BaseButtonPro
);
};

const BaseButton = React.forwardRef(_BaseButton);
BaseButton.displayName = 'BaseButton';
const BaseButton = /*#__PURE__*/ Object.assign(React.forwardRef(_BaseButton), {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also add the explanation as comment to one particular component (probably button)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need a lint rule to enforce this, easy to miss out. How did you even figure this out lol 馃樃

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw is there a babel plugin that automatically does this transformation? Seems like a common issue libraries might be running into 馃

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is this babel plugin - https://github.com/mbrowne/babel-plugin-pure-static-props. I tried this first but it didn't work. I think it works when props are on react component and not when they are on refs. Even for react components it didn't work at lots of places for some reason. Couldn't find any other plugin either.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need a lint rule to enforce this, easy to miss out.

Yes definitely! Exploring if it can be eslint rule or babel plugin.

How did you even figure this out lol 馃樃

Lol it was tricky

displayName: 'BaseButton',
});

export default BaseButton;
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ const _StyledBaseButton: React.ForwardRefRenderFunction<BladeElementRef, StyledB
);
};

const StyledBaseButton = React.forwardRef(_StyledBaseButton);
StyledBaseButton.displayName = 'StyledBaseButton';
const StyledBaseButton = /*#__PURE__*/ Object.assign(React.forwardRef(_StyledBaseButton), {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here, what is the main factor?

The PURE comment or the Object.assign? which one does the magic?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PURE comment.

You can also define component inside a function and add prop inside that and return the component with imediate function call and this would probably work.

const StyledBaseButton = /*#__PURE__*/ (() => {
  const MyComponent = () => {};
  MyComponent.displayName = 'MyComponent';
  return MyComponent;
})();

The PURE comment only works on function calls so Object.assign just seems cleaner way compared to any other option.

displayName: 'StyledBaseButton',
});

export default StyledBaseButton;
3 changes: 1 addition & 2 deletions packages/blade/src/components/Button/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ const _Button: React.ForwardRefRenderFunction<BladeElementRef, ButtonProps> = (
);
};

const Button = React.forwardRef(_Button);
Button.displayName = 'Button';
const Button = /*#__PURE__*/ Object.assign(React.forwardRef(_Button), { displayName: 'Button' });

export default Button;
5 changes: 3 additions & 2 deletions packages/blade/src/components/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ const _Checkbox: React.ForwardRefRenderFunction<BladeElementRef, CheckboxProps>
);
};

const Checkbox = React.forwardRef(_Checkbox);
Checkbox.displayName = 'Checkbox';
const Checkbox = /*#__PURE__*/ Object.assign(React.forwardRef(_Checkbox), {
displayName: 'Checkbox',
});

export { Checkbox, CheckboxProps };
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ const _SelectorInput: React.ForwardRefRenderFunction<
);
};

const SelectorInput = React.forwardRef(_SelectorInput);
SelectorInput.displayName = 'SelectorInput';
const SelectorInput = /*#__PURE__*/ Object.assign(React.forwardRef(_SelectorInput), {
displayName: 'SelectorInput',
});

export { SelectorInput };
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ const _PasswordInput: React.ForwardRefRenderFunction<BladeElementRef, PasswordIn
);
};

const PasswordInput = React.forwardRef(_PasswordInput);
// nosemgrep
PasswordInput.displayName = 'PasswordInput';
const PasswordInput = /*#__PURE__*/ Object.assign(React.forwardRef(_PasswordInput), {
displayName: 'PasswordInput',
});

export { PasswordInputProps, PasswordInput };
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ const _SelectInput = (
*
* Checkout {@link https://blade.razorpay.com/?path=/docs/components-dropdown-with-select--with-single-select SelectInput Documentation}.
*/
const SelectInput = React.forwardRef(_SelectInput);
// @ts-expect-error: componentId is our custom attribute
SelectInput.componentId = 'SelectInput';

const SelectInput = /*#__PURE__*/ Object.assign(React.forwardRef(_SelectInput), {
componentId: 'SelectInput',
});

export { SelectInput, SelectInputProps };
5 changes: 3 additions & 2 deletions packages/blade/src/components/Input/TextArea/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ const _TextArea: React.ForwardRefRenderFunction<BladeElementRef, TextAreaProps>
);
};

const TextArea = React.forwardRef(_TextArea);
TextArea.displayName = 'TextArea';
const TextArea = /*#__PURE__*/ Object.assign(React.forwardRef(_TextArea), {
displayName: 'TextArea',
});

export { TextArea, TextAreaProps };
5 changes: 3 additions & 2 deletions packages/blade/src/components/Input/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ const _TextInput: React.ForwardRefRenderFunction<BladeElementRef, TextInputProps
);
};

const TextInput = React.forwardRef(_TextInput);
TextInput.displayName = 'TextInput';
const TextInput = /*#__PURE__*/ Object.assign(React.forwardRef(_TextInput), {
displayName: 'TextInput',
});

export { TextInput, TextInputProps };
3 changes: 1 addition & 2 deletions packages/blade/src/components/Radio/Radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ const _Radio: React.ForwardRefRenderFunction<BladeElementRef, RadioProps> = (
);
};

const Radio = React.forwardRef(_Radio);
Radio.displayName = 'Radio';
const Radio = /*#__PURE__*/ Object.assign(React.forwardRef(_Radio), { displayName: 'Radio' });

export { Radio, RadioProps };