Optimizing @mui/joy package barrel imports does not work with optimizePackageImports and turbopack #75131
Labels
Developer Experience
Issues related to Next.js logs, Error overlay, etc.
Performance
Anything with regards to Next.js performance.
Turbopack
Related to Turbopack with Next.js.
Link to the code that reproduces this issue
https://github.com/PMudra/reproduction-app-optimize-package-imports-joy-ui
To Reproduce
npm run dev -- --turbopack
Current vs. Expected behavior
Example
Current result
All modules of mui/joy seem to be loaded by the browser. Most of its functions / components are unused.
Especially the first bundle contains many unused functions of mui/joy.
Expected behavior
Only the modules that are really needed should be loaded, even though I am importing from the barrel file
@mui/joy
.The result should be similar to importing directly from
@mui/joy/Button
(non-barrel import).In this case, there is no obvious trace of the mui/joy package because it's now way smaller and part of one of the
node_modules
chunks.So when searching for
/joy/
in the Coverage tab, I'd expect to only find some results for theButton
component and its direct dependencies.Provide environment information
Operating System: Platform: linux Arch: x64 Version: #14-Ubuntu SMP PREEMPT_DYNAMIC Sat Nov 30 23:51:51 UTC 2024 Available memory (MB): 63439 Available CPU cores: 16 Binaries: Node: 22.13.0 npm: 10.9.2 Yarn: N/A pnpm: 10.0.0 Relevant Packages: next: 15.2.0-canary.18 // Latest available version is detected (15.2.0-canary.18). eslint-config-next: N/A react: 19.0.0 react-dom: 19.0.0 typescript: 5.7.3 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
Developer Experience, Performance, Turbopack
Which stage(s) are affected? (Select all that apply)
next dev (local)
Additional context
optimizePackageImports
option seems to be working as expected.@mui/joy
inpage.tsx
, which is a Server Component. When importing in a Client Component ("use client"
) only the necessary modules are loaded. But in that case, I don't even have to specifyoptimizePackageImports
as this seems to work out-of-the-box.optimizePackageImports
by default. I am not sure why joy won't work the same way.modularizeImports
option with some success. But this is not documented any more. And a configuration with a mix of named and default imports seems to be complicated.Possible workarounds seem to be:
All of the above are not great. Are there other ones?
Related issues:
optimizePackageImports
is not working withreact-icons
in dev mode #70666Thanks for looking into this and let me know if you need more information.
The text was updated successfully, but these errors were encountered: