-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
Don't pull unused @mui modules by using path imports #168
Conversation
❌ Deploy Preview for any-viewer failed.
|
Codecov ReportBase: 87.40% // Head: 87.40% // No change to project coverage 👍
Additional details and impacted files@@ Coverage Diff @@
## main #168 +/- ##
=======================================
Coverage 87.40% 87.40%
=======================================
Files 18 18
Lines 1969 1969
Branches 349 349
=======================================
Hits 1721 1721
Misses 248 248
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
Actually SWC transform-imports plugin is capable of solving our problem. This is the configuration I have tested experimental: {
plugins: config.browser
? []
: [
[
'@swc/plugin-transform-imports',
{
'@mui/material': {
transform: '@mui/material/{{member}}'
}
}
]
]
} |
I just solved them all. Let me make a PR. |
@pionxzh exciting! I'll close this one up. |
Thanks for the research and detailed documentation. Let's go for this 🚀 |
Maybe the error can be resolved converting this project to ESM. |
I thought we already provided ESM output 🤔 |
627b96d
to
200d3e9
Compare
should be good now 👍 |
Nice! Feel free to merge 😁 |
@pionxzh can we merge? |
src/index.tsx
Outdated
ThemeProvider | ||
} from '@mui/material' | ||
import Paper from '@mui/material/Paper' | ||
import { createTheme, ThemeProvider } from '@mui/material/styles' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anthonyalayo is this expected?
maybe I mess it up in the rebasing 🤔
use this instead?
import createTheme from '@mui/material/styles/createTheme'
import ThemeProvider from '@mui/material/styles/ThemeProvider'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pionxzh looks like it's still using cjs for some reason?
10:41:43 PM: /opt/build/repo/node_modules/@mui/material/styles/createTheme.js:1
10:41:43 PM: import _extends from "@babel/runtime/helpers/esm/extends";
10:41:43 PM: ^^^^^^
10:41:43 PM: SyntaxError: Cannot use import statement outside a module
10:41:43 PM: at internalCompileFunction (node:internal/vm:74:18)
10:41:43 PM: at wrapSafe (node:internal/modules/cjs/loader:1141:20)
10:41:43 PM: at Module._compile (node:internal/modules/cjs/loader:1182:27)
10:41:43 PM: at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
10:41:43 PM: at Module.load (node:internal/modules/cjs/loader:1081:32)
10:41:43 PM: at Module._load (node:internal/modules/cjs/loader:922:12)
10:41:43 PM: at Module.require (node:internal/modules/cjs/loader:1105:19)
10:41:43 PM: at require (node:internal/modules/cjs/helpers:103:18)
10:41:43 PM: at 3898 (/opt/build/repo/docs/.next/server/pages/index.js:726:18)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's quite weird... 🤔 still finding the root cause
I think this is more like an issue on Nextra? IDK how to fix it ._. |
@pionxzh yeah I was really confused too, I thought it had to do with something on the rollup side... that's why I made the other PR to attempt to move away from rolling up modules. I have no idea where that problem is coming from |
I think I figured out the bug @pionxzh , 1. Next.js server side currently builds in CJS, not ESM: You can make it use ESM, but then it follows the Node.js definition, which requires .mjs files: This isn't setup/supported around the entire ecosystem yet. The 2. The module resolution strategy: @mui has the following in its
but (this is the part I'm not 100% certain on): when the Next.js build encounters:
it resolves that without looking at Hence:
If that make sense to you (and let me know if you can confirm/deny with a reference the part I wasn't 100% sure on), I think the fix would be:
Hopefully you have an idea of how we can do that with rollup? |
Wow. This is helpful and detailed. If we need to do this kind of path rewriting Maybe it's time to bring back #169 ... 🤣 I will look into it and see how later. Thanks. |
Agreed, I was thinking the same, we just need exceptions for those 2-3 odd ones out. |
67ab1ef
to
da029e9
Compare
I think your assumption 1 is correct. |
Sounds good, closing this one up for that one |
Problem
Webpack only tree shakes during minification, so this package is pulling in everything from @mui when developing. This results in a very large amount of modules and causes a significant slowdown for development.
This is a known problem, and upon browsing the material-ui github, I could see they are importing the same way.
Attempting to fix this downstream by applying production settings in development doesn't seem to work, it just hangs.
On top of that, the attempted fix #169 doesn't reduce the bundle size either. It transforms the import statements but ends up pulling in other imports from those imports. This solution inlines the @mui dependencies so that the package is minimized in development.
Resources
Solution
Use path imports to only pull in necessary dependencies from @mui.
Tested with a next.js boilerplate using version 13.1.2.
Ran
npm link
inside@textea/json-viewer
Ran
npm link @textea/json-viewer
inside the dependent projectCompile time output:
Next.js module trace output:
Previous size:
Explored Alternatives
#169