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

2.7.0 : MUI named import generate error #8828

Closed
raskyer opened this issue Feb 21, 2024 · 5 comments
Closed

2.7.0 : MUI named import generate error #8828

raskyer opened this issue Feb 21, 2024 · 5 comments

Comments

@raskyer
Copy link

raskyer commented Feb 21, 2024

Reproduction

Clone : https://github.com/raskyer/remix-bug
npm run dev
Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.

System Info

System:
    OS: Windows 11 10.0.22621
    CPU: (12) x64 Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz
    Memory: 16.48 GB / 31.73 GB
  Binaries:
    Node: 20.9.0 - ~\AppData\Local\fnm_multishells\26476_1708451566467\node.EXE
    npm: 10.1.0 - ~\AppData\Local\fnm_multishells\26476_1708451566467\npm.CMD
  Browsers:
    Edge: Chromium (121.0.2277.128)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    @remix-run/dev: ^2.7.1 => 2.7.1
    @remix-run/express: ^2.7.1 => 2.7.1
    @remix-run/node: ^2.7.1 => 2.7.1
    @remix-run/react: ^2.7.1 => 2.7.1
    vite: ^5.1.0 => 5.1.3

Used Package Manager

npm

Expected Behavior

I want to be able to import : import Button from '@mui/material/Button as stated in :
https://mui.com/material-ui/getting-started/usage/
without issue.

Actual Behavior

I have the following error :
Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.

@pcattori
Copy link
Contributor

Looks like @mui/material is not configured properly for ESM. You can workaround this by either:

Note that this is not a Remix specific issue, and will be an issue for any Vite SSR projects.
For more see the ESM/CJS troublshooting section of the docs.

Closing as this isn't something solvable by Remix.

@QzCurious
Copy link

QzCurious commented Mar 5, 2024

Hi @pcattori, I'd love to let you know that ssr.noExternal: ["@mui/material"] won't be working.

image

In addition, I was able to make it work by cjsInterop({ dependencies: ["@mui/material/*"] }). I don't know but the /* IS NECESSARY.

BTW, I'm not any familiar with ESM/CJS problem (too hard for me) but trying to get luck on this issue.

@pcattori
Copy link
Contributor

pcattori commented Mar 5, 2024

@QzCurious could you try bundling @mui/* dependencies only in production as suggested in this template?

@QzCurious
Copy link

QzCurious commented Mar 5, 2024

@QzCurious could you try bundling @mui/* dependencies only in production as suggested in this template?

For production build, it works. And for your convenience, here is a reproduction code I used for testing it.

Curious about the suggestion, why would we only configuring it for production and make it unusable for local dev?

@QzCurious
Copy link

The conditional ssr.noExternal configuration inspired me to try remix-serve ./build/server/index.js for a build bundle. Turns out I need combine both ssr.noExternal, cjsInterop to make both dev and production server working.

Here's how it should be configured:

export default defineConfig({
  // dev: failed; build: success; remix-serve: failed
  // https://github.com/kiliman/remix-vite-mui/blob/aee050114dae1b01d35276a7d7641d5df45fcfba/vite.config.ts#L10
  ssr: {
    noExternal: process.env.NODE_ENV === "production" ? [/^@mui\//] : [],  // or  `['@mui/**']`
  },

  plugins: [
    remix(),
    tsconfigPaths(),
    // dev: success; build: success; remix-serve: failed
    process.env.NODE_ENV === "development" &&
      cjsInterop({
        dependencies: ["@mui/material/*"],
      }),
  ],
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants