mantine: Cannot Modularize Imports for Next.js
What package has an issue
Describe the bug
Hey there,
With Next.js >= 13.1, you can make use of their Modularize Imports feature.
Right now there is an open PR on material-ui mentioning it: https://github.com/mui/material-ui/pull/35457/files
Could mantine support that as well? I attempted it, and it fails compilation at import { useMantineTheme } from '@mantine/core';
What version of @mantine/hooks page do you have in package.json?
N/A
If possible, please include a link to a codesandbox with the reproduced problem
No response
Do you know how to fix the issue
None
Are you willing to participate in fixing this issue and create a pull request with the fix
None
Possible fix
No response
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 8
- Comments: 25 (9 by maintainers)
@rtivital I made a demo app using create-react-app to profile this.
Here’s the demo app:
I will showcase the modules loaded by webpack in each import scenario.
Scenario 1: Recommended by docs
Scenario 2: Reaching into the ESM directory manually
Notice 454 modules were loaded with the current approach, and only 20 modules were loaded with the desired approach.
While tree shaking will occur during the minification process, webpack still needs to traverse all of these modules that get imported. If @mantinedev’s build can be restructured so that:
"module": "esm/index.js",is changed into"module": "index.js",Then transformations like Modularize Imports or babel-plugin-import will work, and we could import as we currently are:
while still only pulling in 20 modules.
@rtivital in order to increase page load times, since they can become very slow. @tabler/icons, the dependency for mantine, is also in the middle of it’s v2 branch for the same support: https://github.com/tabler/tabler-icons/issues/359
We are really suffering from this issue.
Thanks for the clarification @anthonyalayo .
IMHO that’s two different things.
import {Button} from '@mantine/core';may becomeimport Button from '@mantine/core/Button';import {useMantineTheme} from '@mantine/core';may becomeimport useMantineTheme from '@mantine/core/useMantineTheme';default exportsisn’t mandatory, so it might be possible to stick withnamed exportsif wanted.Is my understanding correct? So to get this working and stable the file structure must become part of the public API - in my understanding it’s currently not part of it. And it must be consistent with the export namings.
Another idea: Because
next.config.jsis a JS file and not a JSON (or might it be a JSON file?) it may also be possible to utilize some require statements and fetch the transformation configuration directly from the package:This opens up the possibility to keep the file structure out of the public API.
And what about using
package.jsonexports? https://webpack.js.org/guides/package-exports/ https://nodejs.org/api/packages.html#subpath-exportsIs this already usable? For me this looks like the better and more standardized approach.
I’ll put my experience here too: today it is not much about “dev” speed and module loading, but also about “React Server Components” experience, because when I’ll use
@mantine/core, I’ll get a lot of stuff where Next.js complains that I should use “use client”; even when it’s not necessary:Just because of emotion cache from Mantine I’ve to put “use client”; in a place where it does not make much sense…
in my case optimizePackageImports does not help me. i have ben add @mantine/core and also @tabler/icons-react and have the same modules in dev mode. (and same build ms)
Compiled /in 9.2s (6432 modules) (4000+ modules only @tabler/icons-react) but i am use only 10 icons. that a very big problem… modularizeImports have same problem.
@cyantree
Treeshaking vs module loading is different. Everyone agrees here that treeshaking is working, but all modules are being loaded during development. That is what we are looking to fix.
We are already using the public API by using the components the library publicizes. The only request is that the public API becomes consistent in the way that the file layout is structured so that we don’t need to load all modules.
I posted this in the issue description:
Just an update here that
@tabler/tabler-iconsjust merged their support for this as well: https://github.com/tabler/tabler-icons/issues/359So, why do you need tree shaking in development mode?