-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
[material-ui] Reexport styles from stylesOptimized
to improve TS performance
#47069
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
Draft
siriwatknp
wants to merge
14
commits into
mui:master
Choose a base branch
from
siriwatknp:fix/types-theme-components2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or 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
Netlify deploy previewhttps://deploy-preview-47069--material-ui.netlify.app/ Bundle size report
|
b3d075e
to
e050e50
Compare
1 task
@siriwatknp Nice approach! The solution looks good from what I’ve seen. Could we optimize the sx prop with theme too ( |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
scope: system
The system, the design tokens / styling foundations used across components. eg. @mui/system with MUI
type: enhancement
It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature.
typescript
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
closes #42772
For Reviewer
Theme
tostylesOptimized
to remove cyclic deps and set theme.components to emptyTheme
fromstyles
with augmented theme.components to preserve the behaviorstylesOptimized
tostyles
so that user can switch the import path without breaking change<component>.d.ts
to use from stylesOptimized and export its Theme types for selective augmentationSummary
@mui/material/styles
import with@mui/material/stylesOptimized
including module augmentationTo test the change, checkout this PR:
Then edit the
packages/mui-material/perf-test/test-createTheme.tsx
to import createTheme from@mui/material/styles
and run diagnosis again.Compare the result between the two.
Root Cause
The issue stems from circular TypeScript dependency in the type definitions:
Original definition (
packages/mui-material/src/styles/createThemeNoVars.d.ts
):The circular path:
ThemeOptions.components
→Components<Omit<Theme, 'components'>>
Theme
interfaceTheme
extendsBaseTheme
andCssVarsProperties
, inlining all their type definitionsTheme
is referenced back inThemeOptions
→ circular dependencyWhy exponential type computation:
The
Components<Theme>
interface is massive - for each of 80+ MUI components, it references:When TypeScript resolves
Components<Omit<Theme, 'components'>>
:ComponentsOverrides
andComponentsVariants
uses theTheme
generic with complexInterpolation
types@mui/material
Memory spike: From ~460MB to ~2.2GB (4× increase), causing OOM errors in CI/CD
User Journey:
Solution
Created alternative entry point
stylesOptimized
that breaks circular dependency by moving completeTheme
definition there, makingcreateThemeNoVars.d.ts
reference it instead of defining inline.Key Changes
1. New optimized entry point (
packages/mui-material/src/stylesOptimized/createTheme.d.ts
):2. Mirror exports (
packages/mui-material/src/stylesOptimized/index.ts
):3. Update original definition (
packages/mui-material/src/styles/createThemeNoVars.d.ts
):Why This Works
Breaks circular dependency:
stylesOptimized/createTheme.d.ts
definesTheme
with simplecomponents?: ThemeComponents
(no generics, no circular references)styles/createThemeNoVars.d.ts
extendsThemeOptimized
instead of defining inlineThemeOptimized
once (fromstylesOptimized
), avoiding repeated instantiationsNon-breaking for v7:
import { ThemeOptions } from '@mui/material/styles'
as beforeTheme
interface still usesComponents<T>
generic for backward compatibility@mui/material/stylesOptimized
for better build performancePerformance impact (from analysis):
Usage for Library Authors
To benefit from improved TypeScript performance, replace ALL imports from
@mui/material/styles
with@mui/material/stylesOptimized
:Important:
@mui/material/styles
will work but with higher memory usagestylesOptimized
for CI/CD stabilityResult
Before:
After: