Skip to content
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

[material-ui] recommended babel-plugin-import causes type and runtime mismatch for useTheme location #41597

Closed
JuanCaicedo opened this issue Mar 21, 2024 · 3 comments
Assignees
Labels
customization: theme Centered around the theming features package: material-ui Specific to @mui/material status: waiting for author Issue with insufficient information

Comments

@JuanCaicedo
Copy link

JuanCaicedo commented Mar 21, 2024

Steps to reproduce

I'm sorry not to include this, but I really struggled to find a way to get either CodeSandbox or StackBlitz to support a custom babelrc and typescript.

Current behavior

I tried setting up the babel-plugin-import optimization described here.

After doing that, I'm importing useTheme like so

import { useTheme } from '@mui/material';

And my typescript compiler shows no errors.

However at runtime I get the following error

    Cannot find module '@mui/material/useTheme' from 'src/my-file.tsx'

If I remove the plugin, both type checking and the runtime are fine, which leads me to think this is a problem with the babel setup.

I see that it's possible to change my import to this

import { useTheme } from '@mui/material/styles';

But I feel like it would be preferable to change my babel set up.

Any ideas how I could configure it differently?

Expected behavior

Importing useTheme at runtime should find the correct function

Context

My full babelrc

{
    "presets": ["next/babel", "@babel/preset-typescript"],
    "plugins": [
        "const-enum",
        "@babel/plugin-transform-shorthand-properties",
        [
            "babel-plugin-import",
            {
                "libraryName": "@mui/material",
                "libraryDirectory": "",
                "camel2DashComponentName": false
            },
            "core"
        ],
        [
            "babel-plugin-import",
            {
                "libraryName": "@mui/icons-material",
                "libraryDirectory": "",
                "camel2DashComponentName": false
            },
            "icons"
        ]
    ]
}

Your environment

  System:
    OS: macOS 14.2.1
  Binaries:
    Node: 20.11.0 - ~/.asdf/installs/nodejs/20.11.0/bin/node
    npm: 10.2.4 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: Not Found
  Browsers:
    Chrome: 122.0.6261.129
    Edge: 122.0.2365.92
    Safari: 17.2.1
  npmPackages:
    @emotion/react: ^11.11.1 => 11.11.1 
    @emotion/styled: ^11.10.4 => 11.10.6 
    @mui/base:  5.0.0-beta.40 
    @mui/core-downloads-tracker:  5.15.14 
    @mui/icons-material: ^5.10.9 => 5.11.16 
    @mui/material: ^5.10.10 => 5.15.14 
    @mui/private-theming:  5.15.14 
    @mui/styled-engine:  5.15.14 
    @mui/system:  5.15.14 
    @mui/types:  7.2.14 
    @mui/utils:  5.15.14 
    @mui/x-date-pickers: ^6.3.1 => 6.3.1 
    @types/react: ^18.0.21 => 18.0.35 
    react: ^18.2.0 => 18.2.0 
    react-dom: ^18.2.0 => 18.2.0 
    typescript: ^5.0.4 => 5.0.4 

Search keywords: useTheme, type, module, babel-plugin-import

@JuanCaicedo JuanCaicedo added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Mar 21, 2024
@JuanCaicedo
Copy link
Author

It seems to me like the suggestion of resolving everything to @mui/material/* isn't sufficient. I say that because looking at my node_modules/@mui/material/index.js, it seems like there's a lot of things that are export * there, and any of those could also be exporting things from arbitrarily nested places.

Am I missing something about how to set up this plugin for a better optimization?

@zannager zannager added package: material-ui Specific to @mui/material customization: theme Centered around the theming features labels Mar 22, 2024
@ZeeshanTamboli
Copy link
Member

@JuanCaicedo Can you please create a minimal GitHub repository that demonstrates the issue and share the link with us? This will allow us to investigate the problem more effectively.

Could you try with adding another entry in the plugin by having the library directory pointed to styles:

{
    "presets": ["next/babel", "@babel/preset-typescript"],
    "plugins": [
        "const-enum",
        "@babel/plugin-transform-shorthand-properties",
        [
            "babel-plugin-import",
            {
                "libraryName": "@mui/material",
                "libraryDirectory": "",
                "camel2DashComponentName": false
            },
            "core"
        ],
        [
            "babel-plugin-import",
            {
                "libraryName": "@mui/material",
                "libraryDirectory": "styles", // Note the library directory
                "camel2DashComponentName": false
            },
            "core"
        ],
        [
            "babel-plugin-import",
            {
                "libraryName": "@mui/icons-material",
                "libraryDirectory": "",
                "camel2DashComponentName": false
            },
            "icons"
        ]
    ]
}

@ZeeshanTamboli ZeeshanTamboli added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Mar 29, 2024
Copy link

github-actions bot commented Apr 5, 2024

Since the issue is missing key information and has been inactive for 7 days, it has been automatically closed. If you wish to see the issue reopened, please provide the missing information.

@github-actions github-actions bot closed this as completed Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customization: theme Centered around the theming features package: material-ui Specific to @mui/material status: waiting for author Issue with insufficient information
Projects
None yet
Development

No branches or pull requests

4 participants