mui / material-ui

Material UI: Comprehensive React component library that implements Google's Material Design. Free forever.
https://mui.com/material-ui/
MIT License
93.19k stars 32.08k forks source link

[material-ui] Next.js 14 with NX monorepo build fails on PonyfillGlobal #41871

Open clickspider opened 4 months ago

clickspider commented 4 months ago

Steps to reproduce

Link to live example: (nextjs error repo)

Steps:

  1. npm i
  2. npx nx build nx-nextjs-monorepo

Current behavior

Getting an error:

   Creating an optimized production build ...
Failed to compile.

../../libs/ui/src/lib/client/configs/theme/theme.ts
Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime
Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation

The error was caused by importing '@mui/material/styles/index.js' in '../../libs/ui/src/lib/client/configs/theme/theme.ts'.

Also saw this error when using mui pervious version 5.15.14:

../../node_modules/.pnpm/@mui+utils@5.15.14_@types+react@18.2.33_react@18.2.0/node_modules/@mui/utils/ponyfillGlobal/ponyfillGlobal.js
Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime
Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation

Import trace for requested module:
../../node_modules/.pnpm/@mui+utils@5.15.14_@types+react@18.2.33_react@18.2.0/node_modules/@mui/utils/ponyfillGlobal/ponyfillGlobal.js
../../node_modules/.pnpm/@mui+utils@5.15.14_@types+react@18.2.33_react@18.2.0/node_modules/@mui/utils/ponyfillGlobal/index.js
../../node_modules/.pnpm/@mui+utils@5.15.14_@types+react@18.2.33_react@18.2.0/node_modules/@mui/utils/index.js
../../node_modules/.pnpm/@mui+material@5.15.14_@emotion+react@11.11.4_@emotion+styled@11.11.0_@types+react@18.2.33_react-dom@18.2.0_react@18.2.0/node_modules/@mui/material/className/index.js
../../node_modules/.pnpm/@mui+material@5.15.14_@emotion+react@11.11.4_@emotion+styled@11.11.0_@types+react@18.2.33_react-dom@18.2.0_react@18.2.0/node_modules/@mui/material/Box/Box.js
../../node_modules/.pnpm/@mui+material@5.15.14_@emotion+react@11.11.4_@emotion+styled@11.11.0_@types+react@18.2.33_react-dom@18.2.0_react@18.2.0/node_modules/@mui/material/Box/index.js
__barrel_optimize__?names=Box!=!../../node_modules/.pnpm/@mui+material@5.15.14_@emotion+react@11.11.4_@emotion+styled@11.11.0_@types+react@18.2.33_react-dom@18.2.0_react@18.2.0/node_modules/@mui/material/index.js
../../libs/ui/src/lib/client/components/hero/hero.tsx
../../libs/ui/src/lib/client/components/hero/index.ts
../../libs/ui/src/lib/client/components/index.ts
../../libs/ui/src/lib/client/index.ts
../../libs/ui/src/lib/server/providers/nn-app-provider.tsx
../../libs/ui/src/lib/server/providers/index.ts
../../libs/ui/src/lib/server/index.ts
../../libs/ui/src/server.ts

> Build failed because of webpack errors
Error occurred while trying to run the build command
1

Expected behavior

That error will not appear, and fix the code that causes the issue.

Context

When trying to setup a NX monorepo with NextJS 14 (App Router), I have exported from my libs folder some middleware(s) and then when running the build command I get this error (see above).

After I further investigated I saw that it has something to deal with this file: https://github.com/mui/material-ui/blame/next/packages/mui-utils/src/ponyfillGlobal/ponyfillGlobal.ts

Whenever I changed it locally in my node_modules to just return this instead of Function('return this') the build started to work fine again. I am not sure on the cause of this.

Your environment

npx @mui/envinfo ``` Don't forget to mention which browser you used. Output from `npx @mui/envinfo` goes here. ```

Search keywords: ponyfillglobal

clickspider commented 4 months ago

tagging @Janpot as it he was the last who touched this file, but might not be related to his changes - https://github.com/mui/material-ui/pull/40772

clickspider commented 4 months ago

@zannager thanks for helping out! small update -> it seems like @Janpot just pushed a Prettier update on that file but originally the logic was written here - https://github.com/mui/material-ui/pull/13426 by @TrySound , sorry for the confusion earlier.

clickspider commented 4 months ago

Small update: to workaround this I have split my libs to not have to build NextJS middleware in the same lib as my MUI code. However, this issue still needs to be addressed.