Closed vemonet closed 1 year ago
I tracked down the issue to be showing up as soon as an icon from @mui/icons-material
is imported and used, e.g.:
import SearchIcon from '@mui/icons-material/Search';
<SearchIcon />
The error message is Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
Which seems to point at an issue with @mui/icons-material
imports
So I tried to edit the vite build rollup config in astro.config.mjs
to add the commonJS plugin, in case there was issues with @mui/icons-material
importing CJS modules, but I still get the same error
import { defineConfig } from 'astro/config';
import react from "@astrojs/react";
import commonjs from '@rollup/plugin-commonjs';
export default defineConfig({
integrations: [react()],
vite: {
build: {
rollupOptions: {
plugins: [
commonjs(),
],
},
},
}
});
Anyone has an idea how this issue with @mui/icons-material
could be resolved?
I finally found the root of my problem, it is related to issue https://github.com/withastro/astro/issues/4432
There is a proposed workaround to use client:only='react'
for the problematic components (the one using mui icons in my case), it is not ideal since it would be nice to have assets like icons preloaded, but let's give it a try!
Now the yarn build
completes, but if we try to open the built website the components we defined with client:only
are not showing, and we have the following error in the console.log:
Uncaught (in promise) TypeError: cr is not a function
Which can be fixed by completely removing the @mui/icons-material
component and directly using svg icons (it will be better on the long run anyway)
So Astro seems to properly support most of @mui/material
, apart from @mui/icons-material
, which can easily be replaced by directly using CSS and SVG for the icons
I guess there are still some work to do to fully integrate existing popular UI libraries from external frameworks, but what we have already is really promising! Thanks a lot!
I'm having the same issue, using React components from a Vite/TS project containing Material UI icons used as described above (import SearchIcon from '@mui/icons-material/Search'
... <SearchIcon />
).
astro build
fails with the error above if the component is used as anything but client:only
. However, I have components with this issue that I want to use this as a pre-rendered static or client:load
components. It works fine if I remove all Material icons.
Is there any workaround for this that doesn't require using a client:only
component?
I finally found the root of my problem, it is related to issue #4432
There is a proposed workaround to use
client:only='react'
for the problematic components (the one using mui icons in my case), it is not ideal since it would be nice to have assets like icons preloaded, but let's give it a try!Now the
yarn build
completes, but if we try to open the built website the components we defined withclient:only
are not showing, and we have the following error in the console.log:Uncaught (in promise) TypeError: cr is not a function
Which can be fixed by completely removing the
@mui/icons-material
component and directly using svg icons (it will be better on the long run anyway)So Astro seems to properly support most of
@mui/material
, apart from@mui/icons-material
, which can easily be replaced by directly using CSS and SVG for the iconsI guess there are still some work to do to fully integrate existing popular UI libraries from external frameworks, but what we have already is really promising! Thanks a lot!
@ericpedia Hey guys, you can try NoSsr
component from mui like this example:
import ExpandMoreIcon from '@mui/icons-material/ExpandMore'
import NoSsr from '@mui/material/NoSsr'
const icon = (
// prevent to use ssr render
<NoSsr>
<ExpandMoreIcon className="text-white" />
</NoSsr>
)
It's work for me!
I finally found the root of my problem, it is related to issue #4432
There is a proposed workaround to use
client:only='react'
for the problematic components (the one using mui icons in my case), it is not ideal since it would be nice to have assets like icons preloaded, but let's give it a try!Now the
yarn build
completes, but if we try to open the built website the components we defined withclient:only
are not showing, and we have the following error in the console.log:Uncaught (in promise) TypeError: cr is not a function
Which can be fixed by completely removing the
@mui/icons-material
component and directly using svg icons (it will be better on the long run anyway)So Astro seems to properly support most of
@mui/material
, apart from@mui/icons-material
, which can easily be replaced by directly using CSS and SVG for the iconsI guess there are still some work to do to fully integrate existing popular UI libraries from external frameworks, but what we have already is really promising! Thanks a lot!
interop: 'auto',
solved it for me.
check here: https://github.com/vitejs/vite/discussions/16201
What version of
astro
are you using?2.1.0
Are you using an SSR adapter? If so, which one?
None
What package manager are you using?
Yarn 3 usually, but here all of them out of dispair!
What operating system are you using?
Linux Fedora 37
What browser are you using?
Firefox and Chromium, but this is not relevant, error happens at build time
Describe the Bug
Hi, I am trying to build an app with Astro + ReactJS + mui (material UI for react).
I used the
create astro
command to create the repo and add the react integration. I am trying to build a generic static website (with no integration to any provider)How to reproduce
Not sure how to use astro.new and stackblitz tools, but everyone knows well git, so I put the code to reproduce on git! You can find the complete code to reproduce the issue here: https://github.com/MaastrichtU-IDS/knowledge-collaboratory/tree/astro-experiment
I made it really simple and succinct to try to narrow down where the issue comes from: there is just a basic navbar, main page and footer, I also removed the mui theme setup
You can easily try it running those commands,
dev
should work andbuild
should fail:Setup versions
With yarn 3 and node 18
It works well with
yarn dev
and does not show any error or warning in the terminal or consoleBut if I try to build with
yarn build
I am getting an erroremStyled is not a function
Click here to see the full error output
``` 09:47:46 AM [content] No content directory found. Skipping type generation. 09:47:46 AM [build] output target: static 09:47:46 AM [build] Collecting build info... 09:47:46 AM [build] Completed in 203ms. 09:47:46 AM [build] Building static entrypoints... 09:47:49 AM [build] Completed in 2.68s. building client Completed in 1.87s. generating static routes error emStyled is not a function TypeError: emStyled is not a function at styled$3 (file:///home/vemonet/develop/sandbox/astro-toast/dist/chunks/pages/all.b256aa7e.mjs:705:25) at file:///home/vemonet/develop/sandbox/astro-toast/dist/chunks/pages/all.b256aa7e.mjs:2027:35 at file:///home/vemonet/develop/sandbox/astro-toast/dist/chunks/pages/all.b256aa7e.mjs:2378:38 at ModuleJob.run (node:internal/modules/esm/module_job:194:25) ```I did some research and unfortunately could not find a lot of issues regarding
emStyled is not a function
. And none of those issues responses were explaining the actual problem (it is usually "just update lib X to version Y to fix it")With npm or yarn 1
Running
dev
works, so I tried running thebuild
script withnpm run
andyarn
v1 (yarn 3 has changed a lot from yarn 1 and seems to do a lot of things differently which is confusing, e.g. the cryptic error message it gave above).I also tried using node 16 and node 18 (cf. below for exact versions used). In this case we get a different error, a bit more explicit, which seems to be related to
react-dom
:This error seems to point that an import is not done properly in the
react-dom
module. But I never usereact-dom
in my code, all the explicit imports to react areimport React from "react";
or importing@mui/material
componentsWith pnpm
How convenient that we have so many npm package managers! With
pnpm
the error comes directly when runningpnpm dev
(nice consistency)On node 16 pnpm embarassingly fails to do basic maths as it tells me that 16.17.1 is not superior or equal to 16.12.0
Then on node 18 pnpm forces me to install an unused dependency of a dependency, and then fails to find it (for a dependency which does not cause issues with
npm
andyarn
anyway).Click here to see the full error output
```bash > astro dev Error: Your application tried to access vscode-textmate, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound. Required package: vscode-textmate Required by: /home/vemonet/develop/translator/knowledge-collaboratory/node_modules/.pnpm/shiki@0.11.1/node_modules/shiki/dist/ Require stack: - /home/vemonet/develop/translator/knowledge-collaboratory/node_modules/.pnpm/shiki@0.11.1/node_modules/shiki/dist/index.js at require$$0.Module._resolveFilename (/home/vemonet/develop/translator/knowledge-collaboratory/.pnp.cjs:15384:13) at require$$0.Module._load (/home/vemonet/develop/translator/knowledge-collaboratory/.pnp.cjs:15234:42) at Module.require (node:internal/modules/cjs/loader:1105:19) at require (node:internal/modules/cjs/helpers:103:18) at Object.Questions
Note that
yarn build
was working fine with the template generated bycreate astro
. And from thenpm run build
output it seems like the issue comesreact-dom-server
Anyone has an idea why
astro
fails to build? Why does it shows errors related to the react-dom when I don't use it? How could this be solved? Do I need to add specific configuration to theastro.config.mjs
? Maybe to fix the react-dom configurationThanks a lot for this beautifully crafted tool!
Link to Minimal Reproducible Example
https://github.com/MaastrichtU-IDS/knowledge-collaboratory/tree/astro-experiment
Participation