Reexport styles from to improve TS performance#47069
Reexport styles from to improve TS performance#47069siriwatknp wants to merge 36 commits intomui:masterfrom
Conversation
Netlify deploy previewBundle size report
|
b3d075e to
e050e50
Compare
|
@siriwatknp Nice approach! The solution looks good from what I’ve seen. Could we optimize the sx prop with theme too ( |
|
Ran the test and can confirm the numbers. If you need some support with some chores around moving from styles to styles optimized and test stuff lmk :) Edit: This fixes a very "weird" Problem with circular Depdencies regarding const theme1 = createTheme({
components: {
MuiButton: {
styleOverrides: {
root: {
backgroundColor: 'primary.main'
}
}
}
}
});
const theme2 = createTheme(theme1)if you create a theme (theme1) with a const theme1Options: ThemeOptions = {
components: {
MuiButton: {
styleOverrides: {
root: {
backgroundColor: 'primary.main'
}
}
}
}
}
const theme1 = createTheme(theme1Options);
const theme2 = createTheme(theme1Options);
const theme3 = createTheme(theme1 as ThemeOptions)I created an issue for the this behavior, to adress it separatley and would suggest to add a warning to the page. |
I would leave the |
Can you tried the latest comment |
yep that fixes the old Is it ok for the ButtonTheme etc. to be exported from the root i.e. being barrel importable? Or should they only be importable from their subdirectory i.e. |
|
@siriwatknp: any update on this? |
stylesOptimized to improve TS performance
closes #42772, closes #47099
For Reviewer
ThemetostylesOptimizedto remove cyclic deps and set theme.components to emptyThemefromstyleswith augmented theme.components to preserve the behaviorstylesOptimizedtostylesso that user can switch the import path without breaking change<component>.d.tsto use from stylesOptimized and export its Theme types for selective augmentationSummary
@mui/material/stylesimport with@mui/material/stylesOptimizedincluding module augmentationTo test the change, checkout this PR:
Then edit the
packages/mui-material/perf-test/test-createTheme.tsxto import createTheme from@mui/material/stylesand 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'>>ThemeinterfaceThemeextendsBaseThemeandCssVarsProperties, inlining all their type definitionsThemeis 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'>>:ComponentsOverridesandComponentsVariantsuses theThemegeneric with complexInterpolationtypes@mui/materialMemory spike: From ~460MB to ~2.2GB (4× increase), causing OOM errors in CI/CD
User Journey:
Solution
Created alternative entry point
stylesOptimizedthat breaks circular dependency by moving completeThemedefinition there, makingcreateThemeNoVars.d.tsreference 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.tsdefinesThemewith simplecomponents?: ThemeComponents(no generics, no circular references)styles/createThemeNoVars.d.tsextendsThemeOptimizedinstead of defining inlineThemeOptimizedonce (fromstylesOptimized), avoiding repeated instantiationsNon-breaking for v7:
import { ThemeOptions } from '@mui/material/styles'as beforeThemeinterface still usesComponents<T>generic for backward compatibility@mui/material/stylesOptimizedfor better build performancePerformance impact (from analysis):
Usage for Library Authors
To benefit from improved TypeScript performance, replace ALL imports from
@mui/material/styleswith@mui/material/stylesOptimized:Important:
@mui/material/styleswill work but with higher memory usagestylesOptimizedfor CI/CD stabilityResult
Before:
After: