waku: Mantine Component Library / dev mode / Error when evaluating SSR module
How can I use Mantine with WAKU without getting this Error:
(waku dev --with-ssr or waku dev - no problem with waku build --with-ssr)
repo to replicate the error: https://github.com/aheissenberger/waku-mantine-broken
22:27:44 [vite] Error when evaluating SSR module /node_modules/.pnpm/use-callback-ref@1.3.1_@types+react@18.2.48_react@18.3.0-canary-b30030471-20240117/node_modules/use-callback-ref/dist/es2015/useRef.js: failed to import "react"
|- SyntaxError: [vite] Named export 'useState' not found. The requested module 'react' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'react';
const {useState} = pkg;
at analyzeImportedModDifference (file:///waku-mantine/node_modules/.pnpm/vite@5.0.12/node_modules/vite/dist/node/chunks/dep-9A4-l-43.js:50953:19)
at nodeImport (file:///waku-mantine/node_modules/.pnpm/vite@5.0.12/node_modules/vite/dist/node/chunks/dep-9A4-l-43.js:50904:9)
at async ssrImport (file:///waku-mantine/node_modules/.pnpm/vite@5.0.12/node_modules/vite/dist/node/chunks/dep-9A4-l-43.js:50799:24)
at async eval (/waku-mantine/node_modules/.pnpm/use-callback-ref@1.3.1_@types+react@18.2.48_react@18.3.0-canary-b30030471-20240117/node_modules/use-callback-ref/dist/es2015/useRef.js:3:44)
at async instantiateModule (file:///waku-mantine/node_modules/.pnpm/vite@5.0.12/node_modules/vite/dist/node/chunks/dep-9A4-l-43.js:50861:9)
Cannot render RSC Error: SyntaxError: [vite] Named export 'useState' not found. The requested module 'react' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'react';
const {useState} = pkg;
at file:///waku-mantine/node_modules/.pnpm/file+..+..+DEV+waku+packages+waku+waku-0.19.1.tgz_react-dom@18.3.0-canary-b30030471-20240117__jadhby4bmjtn267th77iortg3m/node_modules/waku/dist/lib/handlers/dev-worker-api.js:148:60
at Worker.<anonymous> (file:///waku-mantine/node_modules/.pnpm/file+..+..+DEV+waku+packages+waku+waku-0.19.1.tgz_react-dom@18.3.0-canary-b30030471-20240117__jadhby4bmjtn267th77iortg3m/node_modules/waku/dist/lib/handlers/dev-worker-api.js:33:52)
at Worker.emit (node:events:531:35)
at MessagePort.<anonymous> (node:internal/worker:263:53)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:822:20)
at exports.emitMessage (node:internal/per_context/messageport:23:28)
Versions:
Mantine: 7.4.2 - works with NextJS App Router and has 'use client' in all components - https://mantine.dev/guides/next/
WAKU: 0.19.1
root-layout.tsx
import '@mantine/core/styles.css';
import { ColorSchemeScript, createTheme, MantineProvider } from '@mantine/core';
import type { ReactNode } from 'react';
import { Header } from '../components/header.js';
import { Footer } from '../components/footer.js';
const theme = createTheme({
/** Put your mantine theme override here */
});
type RootLayoutProps = { children: ReactNode };
export const RootLayout = async ({ children }: RootLayoutProps) => {
const data = await getData();
return (
<div id="__waku" >
<meta property="description" content={data.description} />
<link rel="icon" type="image/png" href={data.icon} />
<ColorSchemeScript />
<MantineProvider theme={theme}>
<Header />
<main >
{children}
</main>
<Footer />
</MantineProvider>
</div>
);
};
const getData = async () => {
const data = {
description: 'An internet website!',
icon: '/images/favicon.png',
};
return data;
};
About this issue
- Original URL
- State: open
- Created 5 months ago
- Comments: 51 (51 by maintainers)
@dai-shi @Aslemammad - you rock!
WAKU git commit ff20bfe538e32d9315f679a5d4d40e0b377aee18 fixed all problems for
build --with-ssrandstart --with-ssrand works without avite.config.tsfile - even the mantine chart library now works pnpm waku build --with-ssr 👍 pnpm waku start --with-ssr 👍Update on DEV MODE problems: pnpm waku dev 👎 pnpm waku dev --with-ssr 👎
Except for the Mantine Charts which fails with an additional Error I can fix the problems in DEV MODE by adding a
vite.config.tswith some special treatments for the libraries. But it would be great to solve this too.Error in DEV MODE: - without ssr the error is thrown in the browser.
I can do that in the next few days!
I am not able to fix the problem and need help @dai-shi @Aslemammad:
I was able to replicate the problem with a simple replication of the
MantineProviderand was able to track down the problem to be related to the bundling vitejs is doing for external packages in DEV mode. It could be related to the problem, that the package is referencing a different react version but there is only one version installed in the node_modules folder.Here is a reference project which shows the problem: https://github.com/aheissenberger/waku-mantine-broken-01
pnpm waku dev- navigate to link MyUI Package (broken) and look at the console. I replaced the Exception with a console.log to keep the react tree. https://github.com/aheissenberger/myui/blob/5715f4ea793fd2662e54acc4f7a0e192d9fb88da/src/MyThemeProvider.mjs#L12I had to add the provider as an external package to get the error: https://github.com/aheissenberger/myui
The same provider local as part of a WAKU project will not loose the context: https://github.com/aheissenberger/waku-mantine-broken-01/blob/b1c25538e7d6e1febd3ec94c205b292ba09c5814/src/components/my/MyThemeProvider.tsx#L11
We haven’t decided the docs structure, so let’s start with something like
./docs/ui-libs/mantine.mdx. We will probably re-structure it eventually.This fixed it.
While investigating, found out that MantineProvider was importing the ESM format of mantine (all modules were seperate), but the CopyButton was receiving the optimized bundled version of mantine (a huge mantine_core.js file, maybe cjs?).
I’m not sure about the reason, but I guess this is either a vite issue, because this happened when the module was being imported later on (e.g. dynamic import through chunk loading in waku). Or it’s a package exports issue from mantine. Because MantineThemeProvider was being imported first as ESM and later, the CopyButton was being imported with a dynamic import, as cjs.
I first doubted that waku was importing modules in two different modes (one main thread, one worker) in the server, but that was not correct, plus this issue was also happening in the client (that’s why we have
optimizeDepsthere)Feel free to close this issue if it resolved the problem.
I have no idea how Next.js works but I use Solid Start which uses vinxi from @nksaraf. The internal output folder
.vinxicontains three different bundles .I did some research based on the official Vite SSR & SSG Example code and my findings are:
Mantine Component Libraryhas no problems with SSR and SSG in nodeHere is my repro which I used to conduct the tests: https://github.com/aheissenberger/vite-ssr-test
I suggest to change the build pipeline to support the following target output:
maybe have a look at vinxi - specifically the existing deploy adapter from the Nuxt ecosystems Nitro Stack are amazing.