Replies: 3 comments 7 replies
-
There might be an issue with detecting these imports as not having any side-effects. Tree shaking combines export identification and dead code elimination where side-effects are not present. I don't have a straight up answer, but read through https://webpack.js.org/guides/tree-shaking/ and see if there's anything you can apply when it comes to side-effects. WebPack must understand in development mode that your ui library has no side-effects. Side-effects in this context means code that ought to be imported, or executed, at any given time, in order to enable functionality on another module, roughly speaking anyway, it is a bit of a complex subject in JavaScript. |
Beta Was this translation helpful? Give feedback.
-
@codart1 the issue is not with While the link above has details explaining the issue, I can reiterate here. Webpack must traverse all imports when running in either dev or prod, as it does not know how to / can't optimize at the code level. It is only when terser (a webpack default plugin) minifies bundles that you end up with the "tree shaken" version. Pointing to your barrel file, your development server will have all of those modules loaded in memory while it's running. This also happens when building, but it is more hidden since you aren't running an HMR server at the same time. The best solution, which I also recommended @mui to do internally, is to configure either a babel or swc plugin to transform your imports. @mui ironically has docs on that here. This will allow you to keep the DX you want while significantly speeding up your webpack builds. |
Beta Was this translation helpful? Give feedback.
-
Does anyone use react-aria-components? They don't have a setup like MUI. I have detailed my issue here: #60206 It works on Vite by default so I assume is a problem from NextJS. |
Beta Was this translation helpful? Give feedback.
-
I know this one is a well-known issue but it does impact significantly my team's DX.
![image](https://user-images.githubusercontent.com/1023519/199468606-c4c93749-dcc3-44df-98e3-ee3a8a4c3a3b.png)
This is a minimal reproduction:
https://codesandbox.io/p/sandbox/next-js-swc-no-tree-shaking-6siw3h
Observe that
/small-bundle
and/big-bundle
both only render a<Box/>
from@chakra-ui
but they did compile different numbers of modules (216 modules vs 748 modules).The above example just scratches the surface. In our real app, it produces this:
After a few fast refreshes, my laptop becomes overhead and ends up with memory heap overflow:
Our app is a big monorepo with an
ui
internal package that re-exportschakra-ui
component alongside our own component via a barrel file. something like this:And in a file inside our nextjs app, say
app/test/page.tsx
. We only importimport { Box } from 'ui'
then we got the whole library for this page.I see that when I build my project, It got tree shaken just fine. Only while running
next dev
then we get this issue. I guess when building, NextJS use webpack so it knows how to tree shake but fordev
it uses SWC which has this limitation 🤔 Correct me if I'm wrong.Please, I don't want to sacrifice the DX (not using the barrel file). How can I solve this dilemma? 😅
Beta Was this translation helpful? Give feedback.
All reactions