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

Mantine Component Library / dev mode / Error when evaluating SSR module #421

Open
Tracked by #423
aheissenberger opened this issue Jan 23, 2024 · 85 comments
Open
Tracked by #423
Labels
help wanted Extra attention is needed

Comments

@aheissenberger
Copy link
Contributor

aheissenberger commented Jan 23, 2024

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;
};
@dai-shi
Copy link
Owner

dai-shi commented Jan 24, 2024

Thanks for reporting.
Hm, we had some issues which work on build but not on dev, previously.
But, this and #418 are errors on build. So, it feels like there are some issues in the build process.

Can anyone investigate it? It seems the error is different from others.

@dai-shi dai-shi added the help wanted Extra attention is needed label Jan 24, 2024
@dai-shi
Copy link
Owner

dai-shi commented Jan 24, 2024

To confirm, does waku build work?

@aheissenberger
Copy link
Contributor Author

To confirm, does waku build work?

Yes - waku build --with-ssr is working and the result is working with waku start --with-ssr

The problem exists only in dev mode

@aheissenberger aheissenberger changed the title Mantine Component Library / Error when evaluating SSR module Mantine Component Library / dev mode / Error when evaluating SSR module Jan 24, 2024
@dai-shi
Copy link
Owner

dai-shi commented Jan 24, 2024

Oh, I misread the description.

If it's CJS compatibility, it's a known limitation #110 and the workaround is something like this.

...(mode === 'development' && {
ssr: {
external: ['@stylexjs/stylex', 'classnames'],
},
}),

@aheissenberger
Copy link
Contributor Author

Thanks - this fixed it - if you tell me where in the docs I can write a short text to help others:

  1. pnpm add -D vite
  2. add vite.config.ts:
import { defineConfig } from 'vite';
export default defineConfig(({ mode }) => ({
  ...(mode === 'development' && {
    ssr: {
      external: ['@mantine/core'],
    },
  }),
}));

@dai-shi
Copy link
Owner

dai-shi commented Jan 24, 2024

Great to hear. For now, how about ./docs/commonjs-guide.mdx?

@aheissenberger
Copy link
Contributor Author

I still have a Problem with all Components using the MantineProvider. They all have 'use client' in their code and I wrapped them in a 'use client' component but still get this error which is produced by the server in dev mode.

This is the wrapper component:
https://github.com/aheissenberger/waku-mantine-broken/blob/master/src/components/MantineTestComponet.tsx

repo to reproduce the problem: https://github.com/aheissenberger/waku-mantine-broken

waku dev --with-ssr the error:

Error: @mantine/core: MantineProvider was not found in component tree, make sure you have it in your app
    at useMantineTheme (file:///waku-mantine/node_modules/.pnpm/@mantine+core@7.4.2_@mantine+hooks@7.4.2_@types+react@18.2.48_react-dom@18.3.0-canary-b300304_xuwk43wtjddfw4wbpcyehaekvu/node_modules/@mantine/core/esm/core/MantineProvider/MantineThemeProvider/MantineThemeProvider.mjs:11:11)
    at useProps (file:///waku-mantine/node_modules/.pnpm/@mantine+core@7.4.2_@mantine+hooks@7.4.2_@types+react@18.2.48_react-dom@18.3.0-canary-b300304_xuwk43wtjddfw4wbpcyehaekvu/node_modules/@mantine/core/esm/core/MantineProvider/use-props/use-props.mjs:7:17)
    at CopyButton (file:///waku-mantine/node_modules/.pnpm/@mantine+core@7.4.2_@mantine+hooks@7.4.2_@types+react@18.2.48_react-dom@18.3.0-canary-b300304_xuwk43wtjddfw4wbpcyehaekvu/node_modules/@mantine/core/esm/components/CopyButton/CopyButton.mjs:17:51)
    at renderWithHooks (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9532:16)
    at renderIndeterminateComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9611:15)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9881:7)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10163:13)
    at finishFunctionComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9705:5)
    at renderIndeterminateComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9651:5)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9881:7)
    at renderLazyComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9858:3)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9977:11)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10163:13)
    at renderNode (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10507:14)
    at renderChildrenArray (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10386:5)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10190:7)
    at renderNode (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10507:14)
    at renderHostElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9513:5)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9887:5)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10163:13)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10184:11)
    at renderContextProvider (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9840:3)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9965:11)
    at renderMemo (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9783:3)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9959:11)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10163:13)
    at finishFunctionComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9705:5)
    at renderIndeterminateComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9651:5)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9881:7)
    at renderLazyComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9858:3)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9977:11)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10163:13)
    at finishFunctionComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9705:5)
    at renderIndeterminateComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9651:5)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9881:7)
    at renderLazyComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9858:3)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9977:11)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10163:13)
    at renderNode (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10507:14)
    at renderHostElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9513:5)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9887:5)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10163:13)
    at renderNode (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10507:14)
    at renderChildrenArray (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10386:5)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10190:7)
    at renderNode (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10507:14)
    at renderChildrenArray (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10386:5)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10190:7)
    at renderContextProvider (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9840:3)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9965:11)
Cannot render RSC Error: @mantine/core: MantineProvider was not found in component tree, make sure you have it in your app
    at useMantineTheme (file:///waku-mantine/node_modules/.pnpm/@mantine+core@7.4.2_@mantine+hooks@7.4.2_@types+react@18.2.48_react-dom@18.3.0-canary-b300304_xuwk43wtjddfw4wbpcyehaekvu/node_modules/@mantine/core/esm/core/MantineProvider/MantineThemeProvider/MantineThemeProvider.mjs:11:11)
    at useProps (file:///waku-mantine/node_modules/.pnpm/@mantine+core@7.4.2_@mantine+hooks@7.4.2_@types+react@18.2.48_react-dom@18.3.0-canary-b300304_xuwk43wtjddfw4wbpcyehaekvu/node_modules/@mantine/core/esm/core/MantineProvider/use-props/use-props.mjs:7:17)
    at CopyButton (file:///waku-mantine/node_modules/.pnpm/@mantine+core@7.4.2_@mantine+hooks@7.4.2_@types+react@18.2.48_react-dom@18.3.0-canary-b300304_xuwk43wtjddfw4wbpcyehaekvu/node_modules/@mantine/core/esm/components/CopyButton/CopyButton.mjs:17:51)
    at renderWithHooks (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9532:16)
    at renderIndeterminateComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9611:15)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9881:7)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10163:13)
    at finishFunctionComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9705:5)
    at renderIndeterminateComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9651:5)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9881:7)
    at renderLazyComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9858:3)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9977:11)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10163:13)
    at renderNode (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10507:14)
    at renderChildrenArray (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10386:5)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10190:7)
    at renderNode (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10507:14)
    at renderHostElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9513:5)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9887:5)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10163:13)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10184:11)
    at renderContextProvider (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9840:3)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9965:11)
    at renderMemo (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9783:3)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9959:11)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10163:13)
    at finishFunctionComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9705:5)
    at renderIndeterminateComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9651:5)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9881:7)
    at renderLazyComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9858:3)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9977:11)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10163:13)
    at finishFunctionComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9705:5)
    at renderIndeterminateComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9651:5)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9881:7)
    at renderLazyComponent (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9858:3)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9977:11)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10163:13)
    at renderNode (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10507:14)
    at renderHostElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9513:5)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9887:5)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10163:13)
    at renderNode (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10507:14)
    at renderChildrenArray (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10386:5)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10190:7)
    at renderNode (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10507:14)
    at renderChildrenArray (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10386:5)
    at renderNodeDestructive (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:10190:7)
    at renderContextProvider (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9840:3)
    at renderElement (/waku-mantine/node_modules/.pnpm/react-dom@18.3.0-canary-b30030471-20240117_react@18.3.0-canary-b30030471-20240117/node_modules/react-dom/cjs/react-dom-server.edge.development.js:9965:11)
9:51:55 PM [vite] page reload src/templates/home-page.tsx

waku build --with-ssr - error:

dist/public/assets/rd-server.js                                 162.42 kB │ gzip: 48.70 kB
dist/public/assets/index-elMCYdM9.js                            177.23 kB │ gzip: 55.26 kB
✓ built in 1.67s
ReferenceError: document is not defined
    at file:///TST/waku-mantine/dist/public/assets/rsc263-ce06dd315.js:1:4830
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^

ReferenceError: document is not defined
    at file:///TST/waku-mantine/dist/public/assets/rsc263-ce06dd315.js:1:4830
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)

Node.js v21.6.1

@aheissenberger
Copy link
Contributor Author

aheissenberger commented Jan 26, 2024

Update on my tests:

  • I tried everything from this Mantine Support Doc including installing packages mit npm, yarn,...
  • I removed the <MantineProvider> from the root-layout and wrapped it directly around the component - still get the error:
    <MantineProvider>
    {/* Error: @mantine/core: MantineProvider was not found in component tree, make sure you have it in your app */}
     <DateInput
       defaultValue={"2021-01-01"}
       //onChange={setValue}
       label="Date input"
       placeholder="Date input"
     />
    </MantineProvider>
  • It works with simple Mantine Components without a problem:
    <MantineProvider>
      <Button variant="filled">Button</Button>
    </MantineProvider>
  • removing the <MantineProvider> from the button example will throw the error as expected

@dai-shi Please give me some advice where I can further investigate to fix this problem. It looks like it is related to the SSR done with react-dom-server.edge.development.js.

@aheissenberger
Copy link
Contributor Author

@dai-shi I need help - I have debugged this multiple times and still do not understand why the context ist lost.

Here is a repository which only tries to access the Mantine Theme Context and always fails.
I created my own Context HOC which works without any flaws.

The repo to reproduce the problem:
https://github.com/aheissenberger/waku-context-broken

This is the Mantine Context Provider which fails:
https://github.com/mantinedev/mantine/blob/master/packages/%40mantine/core/src/core/MantineProvider/MantineThemeProvider/MantineThemeProvider.tsx

@dai-shi
Copy link
Owner

dai-shi commented Jan 28, 2024

Hi, sorry for the delay.

Confirmed the use client directive: https://unpkg.com/browse/@mantine/core@7.5.0/esm/core/MantineProvider/MantineProvider.mjs

So, I think we need to sit down and investigate it deeply.
A few points to start:

  • Can you confirm again if this is DEV-only issue and/or --with-ssr-only issue?
  • And, hm, the next thing is if it's Mantine specific.

I created my own Context HOC which works without any flaws.

Oh, you already tried it. Then, I'd add Mantine code into your own Context one by one, until it reproduces the error.

@aheissenberger
Copy link
Contributor Author

Render Static

createPage({
    render: 'static',
    path: '/mantine',
    component: MantinePage,
  });

Error: @mantine/core: MantineProvider was not found in component tree exists:

  • pnpm waku dev and open http://localhost:3000/mantine
  • pnpm waku dev --with-ssr and open http://localhost:3000/mantine

Works

  • pnpm waku build && pnpm waku start and open http://localhost:8080/mantine

Error build with SSR pnpm waku build --with-ssr

ReferenceError: document is not defined
    at file:///Users/ah/SVN-Checkouts/TST/waku-context-broken/dist/public/assets/rsc262-cf5b1cd47.js:1:4830
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^

ReferenceError: document is not defined
    at file:///Users/ah/SVN-Checkouts/TST/waku-context-broken/dist/public/assets/rsc262-cf5b1cd47.js:1:4830
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)

Node.js v21.6.1

Render Dynamic

  createPage({
    render: 'dynamic',
    path: '/mantine',
    component: MantinePage,
  });

Error: @mantine/core: MantineProvider was not found in component tree exists:

  • pnpm waku dev and open http://localhost:3000/mantine
  • pnpm waku dev --with-ssr and open http://localhost:3000/mantine

Works:

  • pnpm waku build --with-ssr

Error start with SSR pnpm waku start --with-ssr

ReferenceError: document is not defined
    at file:///Users/ah/SVN-Checkouts/TST/waku-context-broken/dist/public/assets/rsc263-cf5b1cd47.js:1:4830
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
Cannot render RSC ReferenceError: document is not defined
    at file:///Users/ah/SVN-Checkouts/TST/waku-context-broken/dist/public/assets/rsc263-cf5b1cd47.js:1:4830
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)

@aheissenberger
Copy link
Contributor Author

aheissenberger commented Jan 28, 2024

add ReferenceError: document is not defined

I was able to debug and isolate the Problem - it is related to the library react-textarea-autosize a dependency of Mantine:

// react-textarea-autosize.browser.esm.js
// line 104
var isIE = !!document.documentElement.currentStyle ;

The problem is that waku is importing the browser build react-textarea-autosize.browser.esm.js of this library which is wrong. The waku builder should provide the worker condition to select the react-textarea-autosize.worker.esm.js of the library.
see feedback in this closed issue: Andarist/react-textarea-autosize#335

You can reproduce the problem with:
pnpm add @mantine/core

create a page with:

import {  MantineProvider } from '@mantine/core';
import { Textarea } from '@mantine/core';

export const TextareaPage = async () => {
  return (
    <div>
      <title>TextAreaPage</title>
      <h1>TextAreaPage</h1>
      <MantineProvider>
         <Textarea autosize={true}/>
      </MantineProvider>
    </div>
  );
};

run pnpm waku dev --with-ssr

vite v5.0.12 building SSR bundle for production...
transforming...
✓ 686 modules transformed.
rendering chunks...
✓ built in 1.18s
vite v5.0.12 building SSR bundle for production...
transforming...
✓ 667 modules transformed.
rendering chunks...
dist/assets/rsc1-8e67dcaea.js     0.23 kB
...
dist/assets/rsc251-35582c347.js   3.06 kB
dist/entries.js                  13.96 kB
dist/rsdw-server.js              50.98 kB
✓ built in 586ms
vite v5.0.12 building for production...
transforming...
✓ 702 modules transformed.
rendering chunks...
computing gzip size...
dist/public/index.html                                   0.94 kB │ gzip:  0.41 kB
dist/public/assets/rsc14-3c4d10758.js                    0.03 kB │ gzip:  0.05 kB
...
dist/public/assets/rsc207-f0592c8d9.js                  10.74 kB │ gzip:  4.07 kB
dist/public/assets/rsdw-client.js                       13.20 kB │ gzip:  4.92 kB
dist/public/assets/waku-client.js                       13.23 kB │ gzip:  5.06 kB
dist/public/assets/react-number-format.es-GxG7-1q5.js   14.58 kB │ gzip:  5.67 kB
dist/public/assets/floating-ui.react.esm-yXkTxJPR.js    16.86 kB │ gzip:  6.13 kB
dist/public/assets/floating-ui.dom-IQvWPWYn.js          19.05 kB │ gzip:  7.45 kB
dist/public/assets/rd-server.js                        162.42 kB │ gzip: 48.70 kB
dist/public/assets/index-elMCYdM9.js                   177.23 kB │ gzip: 55.26 kB
✓ built in 1.69s

ReferenceError: document is not defined
    at file:///Users/ah/SVN-Checkouts/TST/waku-context-broken/dist/public/assets/rsc260-cf5b1cd47.js:1:4791
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^

ReferenceError: document is not defined
    at file:///Users/ah/SVN-Checkouts/TST/waku-context-broken/dist/public/assets/rsc260-cf5b1cd47.js:1:4791
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)

Node.js v21.6.1

some more curiosities

This will build pnpm waku build --with-ssr - the Mantine component is in a file without 'use client'

textinput-page.tsx:

import {  MantineProvider } from '@mantine/core';
import { TextInput} from '@mantine/core'

export const TextinputPage = async () => {
  return (
    <div>
      <title> TextinputPage </title>
      <h1> TextinputPage </h1>
      <MantineProvider>
         <TextInput />
      </MantineProvider>
    </div>
  );
};

If I extract the <TextInput /> to it's own component TextInputComponent.tsx with 'use client' in the first Line the build will break again with the same error:

TextInputComponent.tsx:

'use client'

import { TextInput} from '@mantine/core'

export const TextInputComponent = () => {
    return (<div><TextInput/></div>);
}

textinput-page.tsx:

import {  MantineProvider } from '@mantine/core';
import { TextInputComponent } from '../components/TextInputComponent.js';

export const TextinputPage = async () => {
  return (
    <div>
      <title> TextinputPage </title>
      <h1> TextinputPage </h1>
      <MantineProvider>
         <TextInputComponent />
      </MantineProvider>
    </div>
  );
};

@dai-shi
Copy link
Owner

dai-shi commented Jan 28, 2024

The problem is that waku is importing the browser build react-textarea-autosize.browser.esm.js of this library which is wrong.

I wonder how Waku imports react-textarea-autosize.browser.esm.js.
Maybe Vite adds browser condition.

https://unpkg.com/browse/react-textarea-autosize@8.5.3/package.json
I wonder whether adding "worker" or removing "browser" is the right solution.

"worker" isn't listed in https://runtime-keys.proposal.wintercg.org/ nor in https://github.com/browserslist/browserslist#browsers, and I'm not sure if it means "webworker" or "cloudflare workers". We should probably avoid it.

Related #393 by @himself65

@dai-shi
Copy link
Owner

dai-shi commented Jan 29, 2024

This will build pnpm waku build --with-ssr - the Mantine component is in a file without 'use client'

That's interesting and nice finding for a hint.
@Aslemammad I wonder if you are interested in taking a closer look at this.

@Aslemammad
Copy link
Contributor

I can do that in the next few days!

@aheissenberger
Copy link
Contributor Author

The problem is that waku is importing the browser build react-textarea-autosize.browser.esm.js of this library which is wrong.

I wonder how Waku imports react-textarea-autosize.browser.esm.js. Maybe Vite adds browser condition.

https://unpkg.com/browse/react-textarea-autosize@8.5.3/package.json I wonder whether adding "worker" or removing "browser" is the right solution.

I am not an expert on package.json import statement but for me it looks like we do not need to add the condition "worker" as the "default": "./dist/react-textarea-autosize.cjs.js" points to the non browser version.

Some how the condition "browser" is set and loads the wrong package for rendering on the server. Maybe too related to 'use client'.

"exports": {
        ".": {
            "types": {
                "import": "./dist/react-textarea-autosize.cjs.mjs",
                "default": "./dist/react-textarea-autosize.cjs.js"
            },
            "development": {...},
            "worker": {
                "module": "./dist/react-textarea-autosize.worker.esm.js",
                "import": "./dist/react-textarea-autosize.worker.cjs.mjs",
                "default": "./dist/react-textarea-autosize.worker.cjs.js"
            },
            "browser": {
                "module": "./dist/react-textarea-autosize.browser.esm.js",
                "import": "./dist/react-textarea-autosize.browser.cjs.mjs",
                "default": "./dist/react-textarea-autosize.browser.cjs.js"
            },
            "module": "./dist/react-textarea-autosize.esm.js",
            "import": "./dist/react-textarea-autosize.cjs.mjs",
            "default": "./dist/react-textarea-autosize.cjs.js"
        },
        "./package.json": "./package.json"
    },

@aheissenberger
Copy link
Contributor Author

@dai-shi what I do not understand is why the rendering (SSR on server during build) emitHtmlFiles will load the Modules (loadModule) created correctly for the browser in public/assets from /dist/entries.js - how can this work?:

export function loadModule(id) {
  switch (id) {
    case 'rsdw-server':
      return import('./rsdw-server.js');

    case 'client/react':
      return import('./public/assets/react.js');
  
    case 'client/rd-server':
      return import('./public/assets/rd-server.js');
  
    case 'client/rsdw-client':
      return import('./public/assets/rsdw-client.js');
  
    case 'client/waku-client':
      return import('./public/assets/waku-client.js');
  
    case 'public/assets/waku-client.js':
      return import('./public/assets/waku-client.js');


    case 'public/assets/rsc0-47a606723.js':
      return import('./public/assets/rsc0-47a606723.js');
...

This is the reason for the ReferenceError: document is not defined when building the static html files as the code in dist/public/assets/rsc258-cf5b1cd47.js is executed.

@dai-shi
Copy link
Owner

dai-shi commented Jan 29, 2024

SSR is basically traditional client React rather than RSC. So, it runs with client bundles. Otherwise, we can't render client components on server.

I'm not sure if this answers to your question all. Feel free to have follow-up questions.

@dai-shi
Copy link
Owner

dai-shi commented Jan 29, 2024

Some how the condition "browser" is set

Yeah, I think removing the "browser" condition will solve this. But, not sure if it breaks other cases as a side effect.
In the Waku source code, we don't add "browser", so it's probably Vite's one.

@aheissenberger
Copy link
Contributor Author

Some how the condition "browser" is set

Yeah, I think removing the "browser" condition will solve this. But, not sure if it breaks other cases as a side effect. In the Waku source code, we don't add "browser", so it's probably Vite's one.

I think you will need to bundle a different set of modules for SSR at build time without the condition "browser". The existing modules in the public/assets folder need to be bundled with condition "browser" as they will run in the browser.

  • Browser => public/assets
  • SSR (optimized for local build environment - eg. Node 20.0.0) => ssr/assets - this is missing
  • Server (optimized for server environment - e.g. Node 18 AWS Lambda or Cloudflare Worker) => server/assets

@himself65
Copy link
Contributor

"worker" condition looks like an undocumented name. There are two kinds of workers: node worker and web worker, and maybe more: deno worker... anyway they are all just Web Worker + JS host environment

@aheissenberger
Copy link
Contributor Author

"worker" condition looks like an undocumented name. There are two kinds of workers: node worker and web worker, and maybe more: deno worker... anyway they are all just Web Worker + JS host environment

In my option - see above - the bundle conditions required for SSR during the build step can leave it to the package.json import configuration to choose the right package. As long as there is no "browser" condition, the default in the import section of packages.json is pointing to a version of the code which does not need a browser environment.

@dai-shi
Copy link
Owner

dai-shi commented Jan 29, 2024

I think you will need to bundle a different set of modules for SSR at build time without the condition "browser".

I'm a bit skeptical about it. Our implementation shouldn't be that complicated, and it basically leads to more "hydration mismatch" errors.

Can anyone investigate how Next.js solves it?

@aheissenberger
Copy link
Contributor Author

Can anyone investigate how Next.js solves it?

I have no idea how Next.js works but I use Solid Start which uses vinxi from @nksaraf. The internal output folder .vinxi contains three different bundles .

I did some research based on the official Vite SSR & SSG Example code and my findings are:

  • The Mantine Component Library has no problems with SSR and SSG in node
  • The generated html with SSR and SSG had not hydration error in the client browser
  • The target for the code rendering on the server should be the server (e.g. node or webworker) - see Vite Documentation

Here 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:

  • browser - loaded by the client
  • node/webworker (depends on the server the code is deployed)
  • node - needed for the SSR process during the build process - should not become part of the final output folder

maybe have a look at vinxi - specifically the existing deploy adapter from the Nuxt ecosystems Nitro Stack are amazing.

@dai-shi
Copy link
Owner

dai-shi commented Jan 31, 2024

Okay, if we were to split bundles to three types, it would require re-architecture to some extent and might be able to only come after v0.20. There's an issue with Vite, as I see, because we use Vite's SSR mode for RSC, it's conflicting. We could work it around with two vite configs, but I wish Vite would support RSC (a new server mode) natively. Otherwise, our code base won't be fairly maintainable.

That said, I'm not sure if I understand the issue and the community standard (especially worker condition isn't very clear), so I think we need more time to investigate.

Meanwhile, I would like to try two workarounds:

  • remove browser condition (configurable?)
  • mock document on SSR.

dai-shi added a commit that referenced this issue Jul 5, 2024
Hmm, this is going to be a big problem with ESM/CJS compat...

Maybe related: #421, #428
@jkhaui
Copy link

jkhaui commented Jul 7, 2024

SOLVED IT! FINALLY.

Just when this entire thing seemed impossible to understand and I was about to give up... Here's the required config, I will post an explanation of my findings later:

  1. To overcome the legacy peer deps issue when running npm i, add this to your package.json (note that I've only tried this with npm so far, not pnpm):
  "overrides": {
    "react": "$react",
    "react-dom": {
      ".": "$react-dom",
      "react": "$react"
    },
    "react-server-dom-webpack": {
      ".": "$react-server-dom-webpack",
      "react": "$react",
      "react-dom": "$react-dom"
    },
    "@floating-ui/react": {
      "react": "$react",
      "react-dom": "$react-dom",
      "@floating-ui/react-dom": {
        "react": "$react",
        "react-dom": "$react-dom",
        "@floating-ui/dom": {
          "react": "$react",
          "react-dom": "$react-dom"
        }
      }
    },
    "waku": {
      ".": "$waku",
      "react": "$react",
      "react-dom": "$react-dom",
      "react-server-dom-webpack": "$react-server-dom-webpack",
      "@vitejs/plugin-react": {
        "react": "$react",
        "react-dom": "$react-dom",
        "react-refresh": {
          "react": "$react",
          "react-dom": "$react-dom"
        }
      }
    },
    "@mantine/core": {
      ".": "$@mantine/core",
      "react": "$react",
      "react-dom": "$react-dom",
      "react-number-format": {
        "react": "$react",
        "react-dom": "$react-dom"
      },
      "react-textarea-autosize": {
        "react": "$react",
        "react-dom": "$react-dom",
        "use-compose-ref": {
          "react": "$react",
          "react-dom": "$react-dom"
        },
        "use-latest": {
          "react": "$react",
          "react-dom": "$react-dom",
          "use-isomorphic-layout-effect": {
            "react": "$react",
            "react-dom": "$react-dom"
          }
        }
      },
      "react-remove-scroll": {
        "react": "$react",
        "react-dom": "$react-dom",
        "react-remove-scroll-bar": {
          "react": "$react",
          "react-dom": "$react-dom"
        },
        "react-style-singleton": {
          "react": "$react",
          "react-dom": "$react-dom"
        },
        "use-callback-ref": {
          "react": "$react",
          "react-dom": "$react-dom"
        },
        "use-sidecar": {
          "react": "$react",
          "react-dom": "$react-dom"
        }
      },
      "@mantine-tests/core": {
        "react": "$react",
        "react-dom": "$react-dom"
      },
      "@floating-ui/react": {
        "react": "$react",
        "react-dom": "$react-dom",
        "@floating-ui/react-dom": {
          "react": "$react",
          "react-dom": "$react-dom",
          "@floating-ui/dom": {
            "react": "$react",
            "react-dom": "$react-dom"
          }
        }
      }
    },
    "@mantine/hooks": {
      ".": "$@mantine/hooks",
      "react": "$react"
    }
  }

Also note that you don't have to put this on your package.json. You can instead add the --legacy-peer-deps flag when running npm i. I personally find it slightly less hacky to add the overrides.

  1. Create a vite.config.ts file with the following:
import { defineConfig } from "vite";

export default defineConfig({
    optimizeDeps: {
        exclude: ["@mantine/core", "@mantine/hooks"]
    },
    ssr: {
        noExternal: ["@mantine/core", "@mantine/hooks"]
    }
});

Mantine should work fine in dev mode now

@dai-shi
Copy link
Owner

dai-shi commented Jul 7, 2024

So, it's basically this is the trick? I think noExternal is already applied.

    optimizeDeps: {
        exclude: ["@mantine/core", "@mantine/hooks"]
    },

@jkhaui
Copy link

jkhaui commented Jul 7, 2024

So, it's basically this is the trick? I think noExternal is already applied.

    optimizeDeps: {

        exclude: ["@mantine/core", "@mantine/hooks"]

    },

I'll check again tomorrow (not on my computer), but I don't think ssr.noExternal is automatically applied.

First I added mantine to ssr.noExternal and it fixed the issue of <MantineProvider/> (imported via a custom provider component used in the root layout) throwing an error during SSR.

But still the app would then immediately crash clientside (from the same error) during hydration. It looks like optimizeDeps exclude fixed this

So yeah, it's strange - looks like ssr noExternal tells vite "fix this package on the server" & optimizeDeps exclude tells vite "fix this on the client"

@jkhaui
Copy link

jkhaui commented Jul 8, 2024

Follow up:

I'm happy I managed to fix this for my own use-case but I think it's far more important we use this opportunity to nail down what the root cause is.
We all know from first-hand experience that debugging issues with Vite's bundling of 3rd-party deps can be a nightmare. So let's try and understand once and for all how each of Vite's configuration options affects the bundling process, rather than blindly tweaking things until it works


Before & After Comparison

"Broken" setup

vite.config.ts

import { defineConfig } from "vite";

export default defineConfig({
    // optimizeDeps: {
    //     exclude: ["@mantine/core", "@mantine/hooks"]
    // },
    ssr: {
        noExternal: ["@mantine/core", "@mantine/hooks"]
    }
});

Contents of node_modules > .vite > waku-dev-server inspected from browser sources:

Screenshot 2024-07-08 104434

Working setup

vite.config.ts

import { defineConfig } from "vite";

export default defineConfig({
    optimizeDeps: {
        exclude: ["@mantine/core", "@mantine/hooks"]
    },
    ssr: {
        noExternal: ["@mantine/core", "@mantine/hooks"]
    }
});

Contents of node_modules > .vite > waku-dev-server inspected from browser sources:

Screenshot 2024-07-08 104238

Observations & open questions

  1. Appears that Vite has a separate bundling mechanism for SSR and another for modules sent to the browser.

This is observed by adding @mantine/core to ssr.noExternal, which fixes the missing context provider error on the server and allows the initial SSRd HTML to render in the browser.
However, during hydration in the browser, the client crashes as it re-executes the same logic but it's not using the corrected module graph used on the server.

The answer to this is adding @mantine/core to optimizeDeps.exclude, which seems to instruct Vite to also leave Mantine and its dependencies in their raw unbundled ESM format on the client.

  1. We've already suspected that the "missing context provider" triggered by Mantine is caused by multiple versions of react and/or @mantine/core in the module graph. The before & after screenshots confirm this.

  2. Why is the default behaviour of Waku/Vite in this scenario to include @mantine/core twice in the module graph? This can clearly be seen in the "before" screenshot where @Mantine is both in Vite's optimised deps under waku-dev-server as well as the root node_modules. This double-inclusion is the root cause and we need to figure out: is this a Waku-specific problem? Vite-specific? Is it related to React v19 rc being used? Is there a problem with Mantine itself?

@dai-shi
Copy link
Owner

dai-shi commented Jul 8, 2024

@Aslemammad Can you help here?

@jkhaui btw, there's ssr.optimizeDeps.exclude instead of optimizeDeps.exclude. It's not documented though.

himself65 pushed a commit to himself65/waku that referenced this issue Jul 10, 2024
…hi#779)

Hmm, this is going to be a big problem with ESM/CJS compat...

Maybe related: dai-shi#421, dai-shi#428
@aheissenberger
Copy link
Contributor Author

aheissenberger commented Aug 9, 2024

@jkhaui great work finding a working setup - but did you find a solution to bundle the matine packages?
I get roughly 149 http requests (assets) and they are grouped into 4 waves download after each other :-(
Look like tree shaking is not applied and all components of Mantine UI are included in each request

@aheissenberger
Copy link
Contributor Author

still breaks with @mantine/dates even when added to vite.config.ts

@dai-shi
Copy link
Owner

dai-shi commented Aug 14, 2024

Hmm. Can you update the repro with the latest waku@next?

@jkhaui
Copy link

jkhaui commented Aug 15, 2024

@jkhaui great work finding a working setup - but did you find a solution to bundle the matine packages?

I get roughly 149 http requests (assets) and they are grouped into 4 waves download after each other :-(

Look like tree shaking is not applied and all components of Mantine UI are included in each request

Sorry I didn't see this (started a new job so have limited time to experiment with waku atm ☹️)

Hmm yeah you're right. I never investigated deeper as I didn't experience any noticeable perf issues, but this explains why there's like 150 tiny RSC chunks in the build output!

Hopefully I can help look into this soon. I've been comparing Waku's build pipeline with Astro + react 19. I find it odd that there doesn't appear to be significant difference between how either framework uses vite in the build process. However, Waku has a lot more trouble bundling certain libs.

My guess is that the need for a separate environment to bundle RSCs might be the reason

@aheissenberger
Copy link
Contributor Author

Hmm. Can you update the repro with the latest waku@next?

I always test with the lastest commits in waku@next - this was tested with commit d0fae28

@dai-shi
Copy link
Owner

dai-shi commented Aug 15, 2024

Okay. I mean if I can test your repo. https://github.com/aheissenberger/waku-mantine-broken isn't updated.

@aheissenberger
Copy link
Contributor Author

You can find a test in the examples/xx_mantine in the branch "mantine-test" of my forked repo of WAKU:
https://github.com/aheissenberger/waku/tree/mantine-test

@dai-shi
Copy link
Owner

dai-shi commented Aug 17, 2024

Thanks. You are not using the latest version. It has to be the same waku version in package json or use the workspace protocol.

Here's the fix:

diff --git a/examples/xx_mantine/package.json b/examples/xx_mantine/package.json
index 8f01461d..24033eff 100644
--- a/examples/xx_mantine/package.json
+++ b/examples/xx_mantine/package.json
@@ -16,10 +16,10 @@
     "@mantine/dates": "^7.12.1",
     "@mantine/hooks": "^7.12.1",
     "dayjs": "^1.11.12",
-    "react": "19.0.0-rc.0",
-    "react-dom": "19.0.0-rc.0",
-    "react-server-dom-webpack": "19.0.0-rc.0",
-    "waku": "0.21.0-beta.0"
+    "react": "19.0.0-rc-68dbd84b-20240812",
+    "react-dom": "19.0.0-rc-68dbd84b-20240812",
+    "react-server-dom-webpack": "19.0.0-rc-68dbd84b-20240812",
+    "waku": "0.21.0-beta.5"
   },
   "devDependencies": {
     "@types/react": "18.3.3",
diff --git a/examples/xx_mantine/src/pages/_layout.tsx b/examples/xx_mantine/src/pages/_layout.tsx
index a3bb4224..a5686f6f 100644
--- a/examples/xx_mantine/src/pages/_layout.tsx
+++ b/examples/xx_mantine/src/pages/_layout.tsx
@@ -3,7 +3,7 @@ import '@mantine/core/styles.css';
 
 import type { ReactNode } from 'react';
 import { MantineProvider } from '@mantine/core';
-import {theme} from '../theme';
+import { theme } from '../theme';
 
 type RootLayoutProps = { children: ReactNode };
 
@@ -11,10 +11,14 @@ export default async function RootLayout({ children }: RootLayoutProps) {
   const data = await getData();
 
   return (
-    <MantineProvider  theme={theme} withNormalizeCSS>
-    {children}
-    </MantineProvider>
-    
+    <html>
+      <head></head>
+      <body>
+        <MantineProvider theme={theme} withNormalizeCSS>
+          {children}
+        </MantineProvider>
+      </body>
+    </html>
   );
 }

@aheissenberger
Copy link
Contributor Author

aheissenberger commented Aug 17, 2024

@dai-shi sorry for using an older version, but this did not fix the error I get with waku dev when using the <MyDate /> date component examples/xx_mantine/src/pages/index.tsx of Mantine:

10:32:54 AM [vite] Error when evaluating SSR module /@fs/Users/ah/SVN-Checkouts/DEV/waku/node_modules/.pnpm/@mantine+dates@7.12.1_@mantine+core@7.12.1_@mantine+hooks@7.12.1_react@19.0.0-rc-68dbd84b-202_44x75gxv2oqotuitlhasvn5g6e/node_modules/@mantine/dates/esm/utils/get-timezone-offset.mjs:
|- TypeError: Cannot read properties of undefined (reading 'extend')
    at eval (/Users/ah/SVN-Checkouts/DEV/waku/node_modules/.pnpm/@mantine+dates@7.12.1_@mantine+core@7.12.1_@mantine+hooks@7.12.1_react@19.0.0-rc-68dbd84b-202_44x75gxv2oqotuitlhasvn5g6e/node_modules/@mantine/dates/esm/utils/get-timezone-offset.mjs:11:31)
    at async instantiateModule (file:///Users/ah/SVN-Checkouts/DEV/waku/node_modules/.pnpm/vite@5.4.0_@types+node@22.4.0_sugarss@4.0.1_postcss@8.4.41__terser@5.31.6/node_modules/vite/dist/node/chunks/dep-NjL7WTE1.js:52844:5)

10:32:54 AM [vite] Error when evaluating SSR module /@fs/Users/ah/SVN-Checkouts/DEV/waku/node_modules/.pnpm/@mantine+dates@7.12.1_@mantine+core@7.12.1_@mantine+hooks@7.12.1_react@19.0.0-rc-68dbd84b-202_44x75gxv2oqotuitlhasvn5g6e/node_modules/@mantine/dates/esm/utils/shift-timezone.mjs:
|- TypeError: Cannot read properties of undefined (reading 'extend')
    at eval (/Users/ah/SVN-Checkouts/DEV/waku/node_modules/.pnpm/@mantine+dates@7.12.1_@mantine+core@7.12.1_@mantine+hooks@7.12.1_react@19.0.0-rc-68dbd84b-202_44x75gxv2oqotuitlhasvn5g6e/node_modules/@mantine/dates/esm/utils/get-timezone-offset.mjs:11:31)
    at async instantiateModule (file:///Users/ah/SVN-Checkouts/DEV/waku/node_modules/.pnpm/vite@5.4.0_@types+node@22.4.0_sugarss@4.0.1_postcss@8.4.41__terser@5.31.6/node_modules/vite/dist/node/chunks/dep-NjL7WTE1.js:52844:5)

10:32:54 AM [vite] Error when evaluating SSR module /@fs/Users/ah/SVN-Checkouts/DEV/waku/node_modules/.pnpm/@mantine+dates@7.12.1_@mantine+core@7.12.1_@mantine+hooks@7.12.1_react@19.0.0-rc-68dbd84b-202_44x75gxv2oqotuitlhasvn5g6e/node_modules/@mantine/dates/esm/utils/get-default-clamped-date.mjs:
|- TypeError: Cannot read properties of undefined (reading 'extend')
    at eval (/Users/ah/SVN-Checkouts/DEV/waku/node_modules/.pnpm/@mantine+dates@7.12.1_@mantine+core@7.12.1_@mantine+hooks@7.12.1_react@19.0.0-rc-68dbd84b-202_44x75gxv2oqotuitlhasvn5g6e/node_modules/@mantine/dates/esm/utils/get-timezone-offset.mjs:11:31)
    at async instantiateModule (file:///Users/ah/SVN-Checkouts/DEV/waku/node_modules/.pnpm/vite@5.4.0_@types+node@22.4.0_sugarss@4.0.1_postcss@8.4.41__terser@5.31.6/node_modules/vite/dist/node/chunks/dep-NjL7WTE1.js:52844:5)

10:32:54 AM [vite] Error when evaluating SSR module /@fs/Users/ah/SVN-Checkouts/DEV/waku/node_modules/.pnpm/@mantine+dates@7.12.1_@mantine+core@7.12.1_@mantine+hooks@7.12.1_react@19.0.0-rc-68dbd84b-202_44x75gxv2oqotuitlhasvn5g6e/node_modules/@mantine/dates/esm/index.mjs:
|- TypeError: Cannot read properties of undefined (reading 'extend')
    at eval (/Users/ah/SVN-Checkouts/DEV/waku/node_modules/.pnpm/@mantine+dates@7.12.1_@mantine+core@7.12.1_@mantine+hooks@7.12.1_react@19.0.0-rc-68dbd84b-202_44x75gxv2oqotuitlhasvn5g6e/node_modules/@mantine/dates/esm/utils/get-timezone-offset.mjs:11:31)
    at async instantiateModule (file:///Users/ah/SVN-Checkouts/DEV/waku/node_modules/.pnpm/vite@5.4.0_@types+node@22.4.0_sugarss@4.0.1_postcss@8.4.41__terser@5.31.6/node_modules/vite/dist/node/chunks/dep-NjL7WTE1.js:52844:5)

10:32:54 AM [vite] Error when evaluating SSR module /src/components/MyDate.tsx:
|- TypeError: Cannot read properties of undefined (reading 'extend')
    at eval (/Users/ah/SVN-Checkouts/DEV/waku/node_modules/.pnpm/@mantine+dates@7.12.1_@mantine+core@7.12.1_@mantine+hooks@7.12.1_react@19.0.0-rc-68dbd84b-202_44x75gxv2oqotuitlhasvn5g6e/node_modules/@mantine/dates/esm/utils/get-timezone-offset.mjs:11:31)
    at async instantiateModule (file:///Users/ah/SVN-Checkouts/DEV/waku/node_modules/.pnpm/vite@5.4.0_@types+node@22.4.0_sugarss@4.0.1_postcss@8.4.41__terser@5.31.6/node_modules/vite/dist/node/chunks/dep-NjL7WTE1.js:52844:5)

@aheissenberger
Copy link
Contributor Author

Treeshaking is not working and all files from Mantine Library are loaded in the browser on each page (143 requests):
pnpm build && pnpm start

SCR-20240817-jtjo

@dai-shi
Copy link
Owner

dai-shi commented Aug 17, 2024

As for dev error, please try this:

diff --git a/examples/xx_mantine/vite.config.ts b/examples/xx_mantine/vite.confi
g.ts
index 39325f79..a94ea43a 100644
--- a/examples/xx_mantine/vite.config.ts
+++ b/examples/xx_mantine/vite.config.ts
@@ -1,8 +1,9 @@
 export default {
   optimizeDeps: {
-    exclude: ["@mantine/core", "@mantine/hooks","@mantine/dates","dayjs"]
+    include: ["dayjs", "dayjs/plugin/*"],
+    exclude: ["@mantine/core", "@mantine/hooks","@mantine/dates"]
   },
   ssr: {
-    noExternal: ["@mantine/core", "@mantine/hooks","@mantine/dates","dayjs"],
+    noExternal: ["@mantine/core", "@mantine/hooks","@mantine/dates"],
   }
 };

@dai-shi
Copy link
Owner

dai-shi commented Aug 17, 2024

I'm not sure about treeshaking not working in PRD.
Can anyone help? cc @jkhaui

@aheissenberger
Copy link
Contributor Author

aheissenberger commented Aug 17, 2024

@dai-shi I update the configuration and tested all other Mantine Libraries and documented the setup or problems in the README.md of my repo

The Mantine Main Libraries including Dates, Forms and most of the Extentions are working with release 0.21.0-beta.5.

There are Problems with:

  • <ColorSchemeScript /> in the head of html in _layout.tsx which creates Hydration failed error
  • Mantine Charts - problems with some sub packages e.g. lodash
  • Extension Carousel - Error: Element type is invalid: expected a string (for built-in components)

@aheissenberger
Copy link
Contributor Author

@dai-shi it works with 0.21.0-beta.5 release but DEV Mode breaks again with Error: @mantine/core: MantineProvider with one of the latest commits d5b19bb

@dai-shi
Copy link
Owner

dai-shi commented Aug 18, 2024

That's weird. This seems to improve it.

diff --git a/examples/xx_mantine/vite.config.ts b/examples/xx_mantine/vite.config.ts
index 7440ddf4..8cd43cfa 100644
--- a/examples/xx_mantine/vite.config.ts
+++ b/examples/xx_mantine/vite.config.ts
@@ -10,10 +10,4 @@ export default {
       "@mantine/notifications > prop-types",
     ]
   },
-  ssr: {
-    noExternal: ["@mantine/core", "@mantine/hooks", "@mantine/dates", "@mantine/charts",
-      "@mantine/code-highlight", "@mantine/notifications", "@mantine/spotlight",
-      "@mantine/carousel",
-      "@mantine/dropzone", "@mantine/nprogress", "@mantine/modals", "@mantine/tiptap"],
-  }
 };

What are the remaining problems? (Apart from "tree-shaking not working". Hm, it might be because of prefetching, not related with tree-shaking.)

@jkhaui
Copy link

jkhaui commented Aug 18, 2024

@aheissenberger I just thought to try something which is adding the following to waku.config.ts:

export default {
  preserveModuleDirs: ['pages']
}

This change alone dropped no. of network requests in my minimal app from 144 -> 132. Maybe you can investigate this to see if it also treeshakes more unused code (I don't have much spare time atm)

It had always been on my mind to try this, but initially I thought it didn't work because I got a broken app setting preserveModuleDirs: []. Only just then I realised that only pages dir needs to be kept as is to preserve relative paths with the fs router, otherwise I believe everything else should be left to the bundler to do its thing

@aheissenberger
Copy link
Contributor Author

Updated Example which supports the current WAKU release is here:
https://github.com/aheissenberger/waku/tree/mantine-test/examples/xx_mantine

preserveModuleDirs in waku.config.ts did not change anything related to the number of requests.

@dai-shi
Copy link
Owner

dai-shi commented Sep 21, 2024

Is the remaining issue this #421 (comment) ?

@aheissenberger
Copy link
Contributor Author

Is the remaining issue this #421 (comment) ?

The problem with the not optimized core package can be solved with this in vite.config.ts:

export default {
  build: {
    chunkSizeWarningLimit: 1000,
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes("@mantine/core")) {
            return "@mantine/core";
          }
        },
      },
    },
  },
};

The packages which are still broken are:

  • Charts - could be fixed by using the beta version
  • Carousel

I have documented my status here:
https://github.com/aheissenberger/waku/tree/mantine-test/examples/xx_mantine

@dai-shi
Copy link
Owner

dai-shi commented Sep 24, 2024

The problem with the not optimized core package can be solved with this in vite.config.ts

Ah, then my misunderstanding was incorrect. Great you found a workaround.

@dai-shi
Copy link
Owner

dai-shi commented Sep 24, 2024

The packages which are still broken are:

  • Charts - could be fixed by using the beta version
  • Carousel

I have documented my status here: https://github.com/aheissenberger/waku/tree/mantine-test/examples/xx_mantine

Thanks for the summary. Can anyone help on this??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants