nextui icon indicating copy to clipboard operation
nextui copied to clipboard

[Feature Request] Support for turbo pack

Open rharkor opened this issue 11 months ago • 8 comments

Is your feature request related to a problem? Please describe.

When you want to use turbopack you always get the error "Error: createContext only works in Client Components."

Describe the solution you'd like

Find a solution to use turbopack

Describe alternatives you've considered

Not using it

Screenshots or Videos

No response

rharkor avatar Mar 09 '24 10:03 rharkor

Hi @rharkor, can you elaborate on the issue a bit more? I've been using Turbopack just fine in multiple projects without error (Next 14.1.0). Can you post what version of Next.js / Turbopack you're using?

image

These projects utilize server actions as well, so something reproducible would help a lot.

cmedina-dev avatar Mar 13 '24 15:03 cmedina-dev

Finally I found how to reproduce, @cmedina-dev The problem occurs when you use @nextui-org/react instead of individual packages like @nextui-org/button.

How to reproduce:

  1. Get the boilerplate: npx create-next-app -e https://github.com/nextui-org/next-app-template
  2. Add --turbo to the dev script in package.json. So it looks like this "dev": "next dev --turbo", Please not that at this point if you run the dev script everything work
  3. Install @nextui-org/react
  4. Replace any import of the main page with the global library Example: import { Link } from "@nextui-org/link"; to import { Link } from "@nextui-org/react";

rharkor avatar Mar 13 '24 16:03 rharkor

Ah, this isn't an issue with Turbopack. NextUI does use client-side hooks (createContext) and by default Next.js pages are server components which will cause this error. You need to add 'use client' at the top of your page.tsx (or wherever client-side hooks are used) for pages to render properly.

There are other workarounds such as creating an import to allow the rest of page.tsx to remain a server component, see: https://stackoverflow.com/a/76181667

cmedina-dev avatar Mar 13 '24 18:03 cmedina-dev

Thank's that is a workaround but can't nextui do this by default ?

rharkor avatar Mar 13 '24 18:03 rharkor

The maintainers could mark the specific components that utilize the client-side hooks with 'use client' but I'm not sure if there's reasoning for leaving them as server components so I think they'd better be able to answer.

cmedina-dev avatar Mar 13 '24 19:03 cmedina-dev

I'm also experiencing some turbo-related issues, specifically with compilation of the tailwind plugin:

./src/styles/globals.css:850:30
Parsing css source code failed
  848 |   --background: ({ opacityVariable, opacityValue }) => {
  849 |           if (!isNaN(+opacityValue)) {
> 850 |             return `hsl(var(${nextuiColorVariable}) / ${opacityValue})`;
      |                              ^
  851 |           }
  852 |           if (opacityVariable) {
  853 |             return `hsl(var(${nextuiColorVariable}) / var(${nextuiOpacityVariable}, var(${opacityVariable})))`;

Unexpected token Delim('$') at [project]/src/styles/globals.css:849:29
  • "tailwindcss": "^3.4.3"
  • "next": "14.3.0-canary.28"
  • "@nextui-org/react": "^2.3.6"

It works with the default/babel next dev command.

connorblack avatar Apr 27 '24 21:04 connorblack

I'm also experiencing some turbo-related issues, specifically with compilation of the tailwind plugin:

./src/styles/globals.css:850:30
Parsing css source code failed
  848 |   --background: ({ opacityVariable, opacityValue }) => {
  849 |           if (!isNaN(+opacityValue)) {
> 850 |             return `hsl(var(${nextuiColorVariable}) / ${opacityValue})`;
      |                              ^
  851 |           }
  852 |           if (opacityVariable) {
  853 |             return `hsl(var(${nextuiColorVariable}) / var(${nextuiOpacityVariable}, var(${opacityVariable})))`;

Unexpected token Delim('$') at [project]/src/styles/globals.css:849:29
  • "tailwindcss": "^3.4.3"
  • "next": "14.3.0-canary.28"
  • "@nextui-org/react": "^2.3.6"

It works with the default/babel next dev command.

i'm facing the same exact error with turbo, everything works with [email protected], any newer versions causes this error.

Elmosh avatar May 01 '24 12:05 Elmosh

Next 14.2.3 - issue still here, as others said.

Forsect avatar May 24 '24 15:05 Forsect

It turned out this issue wasn't cased by nextui but by a custom tailwind plugin:

function addVariablesForColors({ addBase, theme }) {
  let allColors = flattenColorPalette(theme("colors"));
  let newVars = Object.fromEntries(
    Object.entries(allColors).map(([key, val]) => [`--${key}`, val]),
  );

  addBase({
    ":root": newVars,
  });
}

Once removed then all worked out fine with turbo & latest Next & nextui

Elmosh avatar May 29 '24 02:05 Elmosh