Skip to content

Conversation

@siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented Oct 1, 2025

closes #46908

The root cause the the options type of the createTheme is mixing between createThemeNoVars and createThemeWithVars options causing complex type inferences for webpack as demonstrated by https://github.com/nvrtmd/mui-v7-memory-issue-repro

Solution

  • remove cyclic dependency between both internal createTheme files by introduce a new file to store shared types.
  • simplify the ThemeOptions by relying on the createThemeWithVars options (already extending the createThemeNoVars).

The fix also enhancing the ThemeOptions to be a complete version for users who import the type to build up theme options outside of the createTheme.

Test

  1. Clone
  2. Replace package.json with
"@mui/material": "https://pkg.pr.new/mui/material-ui/@mui/material@3e539c5",
"@mui/system": "https://pkg.pr.new/mui/material-ui/@mui/system@3e539c5",
"@mui/utils": "https://pkg.pr.new/mui/material-ui/@mui/utils@3e539c5",
  1. Run npm run build:create-theme
  2. The heapUsed is ~200MB

@siriwatknp siriwatknp added performance typescript type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. labels Oct 1, 2025
@mui-bot
Copy link

mui-bot commented Oct 1, 2025

Netlify deploy preview

https://deploy-preview-47007--material-ui.netlify.app/

Bundle size report

Bundle Parsed size Gzip size
@mui/material 0B(0.00%) 0B(0.00%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes

Generated by 🚫 dangerJS against 3e539c5

@siriwatknp siriwatknp changed the title [WIP] Fix createTheme* cyclic dependency [material-ui] Fix ThemeOptions and createTheme* cyclic dependency Oct 1, 2025
@siriwatknp siriwatknp added the package: material-ui Specific to Material UI. label Oct 1, 2025
@siriwatknp siriwatknp requested a review from sai6855 October 1, 2025 09:54
@siriwatknp siriwatknp marked this pull request as ready for review October 1, 2025 09:54
@siriwatknp
Copy link
Member Author

@sai6855 Can you check this PR on your side.

@sai6855
Copy link
Contributor

sai6855 commented Oct 1, 2025

I can see both memory and compile time got reduced

Before

image

After

image

@nvrtmd
Copy link

nvrtmd commented Oct 1, 2025

I followed your comment, modified my design system's package.json, and can confirm that the heapUsed is now back to a normal level like below. I really appreciate you resolving this significant issue.

[mem] beforeRun rss=92.2MB heapUsed=28.5MB ext=1.7MB
[mem] compile rss=92.5MB heapUsed=28.6MB ext=1.7MB
[mem] afterCompile rss=528.7MB heapUsed=458.5MB ext=2.9MB
[mem] emit rss=528.8MB heapUsed=458.5MB ext=2.9MB
[mem] done rss=535.3MB heapUsed=454.3MB ext=4.2MB

As I was studying the problem, I wanted to ensure I fully understood the root cause. Could you please confirm if my understanding of the original cyclic dependency is correct?

  • The CssVarsThemeOptions type in createThemeWithVars.d.ts depended on ThemeOptions from createThemeNoVars.d.ts.
  • Conversely, the ThemeOptions type in createThemeNoVars.d.ts depended on ColorSystemOptions from createThemeWithVars.d.ts.

This effectively created a circular reference where each file required a type from the other. Is this an accurate summary?

Thank you again for your time and the excellent fix.

@connorshea
Copy link
Contributor

This is great, thank you for the investigation and fix!

@siriwatknp siriwatknp merged commit 139eaec into mui:master Oct 2, 2025
18 of 19 checks passed
@siriwatknp
Copy link
Member Author

This effectively created a circular reference where each file required a type from the other. Is this an accurate summary?

I am 99% sure that's the root cause. Thank you so much for the reproduction repo, it helps a ton.

@chybisov
Copy link
Contributor

chybisov commented Oct 6, 2025

@siriwatknp hey, looks like these changes broke augmentation for custom theme option overrides. This is now produces errors:

image
declare module '@mui/material/styles' {
  interface Shape {
    borderRadius: number
    borderRadiusSecondary: number // custom prop
  }
  interface Theme {
    shape: Shape
  }
  interface ThemeOptions {
    shape?: Partial<Shape>
  }
}

@plevavas
Copy link

@chybisov, did you find a solution?

@chybisov
Copy link
Contributor

@plevavas nope, still on the previous version before this change.

@siriwatknp could you please advise here? 🙏 Thank you!

@siriwatknp
Copy link
Member Author

@plevavas nope, still on the previous version before this change.

@siriwatknp could you please advise here? 🙏 Thank you!

I'm looking into it

@siriwatknp
Copy link
Member Author

siriwatknp commented Nov 4, 2025

@chybisov Can you try this build

// package.json
"@mui/material": "https://pkg.pr.new/mui/material-ui/@mui/material@63cf433"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

package: material-ui Specific to Material UI. performance 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@mui/material v7: createTheme() triggers extreme Webpack memory usage during library build

7 participants