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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[base] Ban default exports #38200

Merged
merged 21 commits into from
Aug 3, 2023
Merged

Conversation

michaldudak
Copy link
Member

@michaldudak michaldudak commented Jul 28, 2023

This PR changes the default exports in Base UI to named ones. The name of the previously default export matches the file name.
Along with the changes in code, I created additional logic in API docs builders and a codemod.

Breaking change

All exports from Base UI are named:

- import Button, { buttonClasses } from '@mui/base/Button';
+ import { Button, buttonClasses } from '@mui/base/Button';
- import BaseMenu from '@mui/base/Menu';
+ import { Menu as BaseMenu } from '@mui/base/Menu';

Additionally, the ClassNameGenerator has been moved to the directory matching its name:

- import ClassNameGenerator from '@mui/base/className';
+ import { ClassNameGenerator } from '@mui/base/ClassNameGenerator';

A codemod is provided to help with the migration:

npx @mui/codemod v5.0.0/base-use-named-imports <path>

To reviewers: please review by commit. The codemod was tested by running it on our codebase, but I'd also appreciate it if you could think of any other cases that could be verified in its tests.

Part of #21862 (comment)

@michaldudak michaldudak force-pushed the ban-default-export-base branch from 319e1f7 to 10a9302 Compare July 28, 2023 12:45
@michaldudak michaldudak added the package: base-ui Specific to @mui/base label Jul 28, 2023
@michaldudak michaldudak force-pushed the ban-default-export-base branch 2 times, most recently from dfc6853 to ef4da86 Compare July 28, 2023 15:08
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jul 29, 2023
@michaldudak michaldudak force-pushed the ban-default-export-base branch from cfce889 to 3b6e9b2 Compare July 31, 2023 08:11
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Jul 31, 2023
@michaldudak michaldudak requested a review from mnajdova July 31, 2023 08:32
@michaldudak michaldudak marked this pull request as ready for review July 31, 2023 08:32
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jul 31, 2023
@@ -62,6 +62,22 @@ npx @mui/codemod <transform> <path> --jscodeshift="--printOptions='{\"quote\":\"

### v5.0.0

### `base-use-named-imports`

Base UI default exports were changed to named ones.
Copy link
Member

Choose a reason for hiding this comment

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

It would be great to add a bit of explanation of why it was done.

return null;
}

const baseImportPathMatch = source.match(/@mui\/base\/(?:(?:.*)\/)*([^/]+)/);
Copy link
Member

@mnajdova mnajdova Jul 31, 2023

Choose a reason for hiding this comment

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

Can we simplify the regexp? We don't nest imports more than three times. Or is there other use-case we are trying to capture with it?

import BaseSwitch from '@mui/base/Switch';
import BaseSelect, { selectClasses } from '@mui/base/Select';
import { Menu, MenuItem } from '@mui/base';
import ClassNameConfigurator from '@mui/base/utils/ClassNameConfigurator';
Copy link
Member

Choose a reason for hiding this comment

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

I would say this is not a valid import. We are leaking internal API. By convention we don't allow these imports and consider them private API.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. Excluding this will simplify the codemod code.


export { default as ClickAwayListener } from './ClickAwayListener';

export { default as unstable_composeClasses } from './composeClasses';
Copy link
Member

Choose a reason for hiding this comment

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

This export is missing as far as I can see. We should check the unstable_ exports with the codemod. They are unstable_ but people may use them.

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 codemod isn't responsible for this part, it only covers exports from @mui/base, not local.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, but we should add back the export, right?

@mnajdova
Copy link
Member

We should check all frozen sandboxes we have in case they have default Base UI imports. cc @samuelsycamore

@@ -2,7 +2,7 @@ export * from './utils';
export * from './Badge';
export * from './Button';
export * from './ClickAwayListener';
export * from './composeClasses';
export { composeClasses as unstable_composeClasses } from './composeClasses';
Copy link
Member

Choose a reason for hiding this comment

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

We likely need a map for the unstable_ utils.

@@ -8,12 +8,13 @@ import {
unstable_useForkRef as useForkRef,
unstable_capitalize as capitalize,
} from '@mui/utils';
import composeClasses from '@mui/base/composeClasses';
import useAutocomplete, {
import { composeClasses } from '@mui/base/composeClasses';
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be unstable_composeClasses as composeClasses?


let node: babel.Node = babelPath.node;

if (node.declaration == null) {
Copy link
Member

Choose a reason for hiding this comment

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

Is this going to work with the Unstable_NumberInput?

Copy link
Member Author

Choose a reason for hiding this comment

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

It shouldn't matter as the component is exported as NumberInput from its source file. The "unstable_" prefix is added in its index.ts, which this script doesn't care about.

@mnajdova
Copy link
Member

mnajdova commented Jul 31, 2023

There is an issue with the API imports generation, for non default imports:
image

Looks like there was a different issue even before, for e.g. https://mui.com/base-ui/react-form-control/hooks-api/#use-form-control-context

image

@michaldudak michaldudak force-pushed the ban-default-export-base branch from 3b6e9b2 to 7422f23 Compare August 3, 2023 07:49
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Aug 3, 2023
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Aug 3, 2023
@michaldudak michaldudak force-pushed the ban-default-export-base branch from 4c4672f to 1c7e673 Compare August 3, 2023 10:37
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Aug 3, 2023
Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

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

Looks good 👍

@@ -15,4 +15,4 @@ export { default as useControlled } from './useControlled';
export { default as useEventCallback } from './useEventCallback';
export { default as useForkRef } from './useForkRef';
export { default as useIsFocusVisible } from './useIsFocusVisible';
export { unstable_ClassNameGenerator } from '@mui/base/className';
Copy link
Member

Choose a reason for hiding this comment

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

We should list this in the Breaking changes section.

@michaldudak michaldudak added this to the Base UI: Stable release milestone Aug 3, 2023
@michaldudak michaldudak merged commit 1e418e6 into mui:master Aug 3, 2023
@michaldudak michaldudak deleted the ban-default-export-base branch August 3, 2023 12:34
richbustos pushed a commit that referenced this pull request Aug 4, 2023
richbustos pushed a commit that referenced this pull request Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Recently completed
Development

Successfully merging this pull request may close these issues.

3 participants