Skip to content

[Bug]: Incorrect chunking in nuxt rolldown-vite with __commonJSMin is not a function #4976

@Q16solver

Description

@Q16solver

Reproduction link or steps

A quick background context, I've tried quite a bit to repro this in stackblitz/in a separate repro, but it seems like this only happens when my application is big enough to cause the chunking to happen, I may be missing some advanced chunking options to make this repro easier, but for now I'll list all my findings and at least a repro-able branch of my app in hopes that it will help

  1. Git clone repo https://github.com/Esposter/Esposter/tree/rolldown_build___commonJSMin_error_repro
  2. Run pnpm i and pnpm build:packages

May need to enable git symlinks

  1. Cd packages/app
  2. Run pnpm build
  3. Observe in packages/app/.output/server/chunks/build/server.mjs file that you get an issue as __commonJSMin is called before it is defined. It seems to have called var require_shared_cjs_prod = __commonJSMin((exports)... before it is defined. Below is a way to look at a "correct" version.

I cannot confirm if the following comparison is "correct", but it seems fine and has not chunked the big part that causes the issue
To actually run the server you would need to follow the read me and install postgres db, but I've not included here as it may be too much effort for maintainer to repro, feel free to just grab the build output and close this issue if the nuxt build output has been checked

Comparisons

  1. To further compare, delete packages/app/app/pages/anime.vue file, which happens to be a good enough size that it doesn't cause the chunking and hoisting of calling __commonJSMin
  2. Run pnpm build and observe packages/app/.output/server/chunks/build/server.mjs file that __commonJSMin does seem to be defined before called now, without anymore chunking into ../_/nitro.mjs.
  3. This does not seem to be an issue anymore as __commonJSMin is properly defined before it is called, this may be a rolldown chunking issue

Extra Observations

  1. It seems like this is due to a csj/esm shared function with nuxt called useFetch that generates the __commonJSMin in the server.mjs file, this is hoisted wrongly, calling the __commonJSMin function before it is defined.

What is expected?

The application should build correct code that properly runs in nuxt.

What is actually happening?

The build seems successful, but does not produce correct runnable code.

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (16) x64 AMD Ryzen 7 3700X 8-Core Processor
    Memory: 10.37 GB / 31.91 GB
  Binaries:
    Node: 24.1.0 - C:\Program Files\nodejs\node.EXE
    npm: 11.3.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 10.12.1 - C:\Program Files\nodejs\pnpm.CMD
    bun: 1.2.16 - ~\.bun\bin\bun.EXE
  Browsers:
    Edge: Chromium (135.0.3179.73)
    Internet Explorer: 11.0.19041.5794

Any additional comments?

This is a bit of a weird issue I've been investigating for a while, will also cross-check with just using vite and see if there are any findings here. At first I thought may be an issue with nuxt/rolldown-vite, but seeing as how my app was working fine with normal vite, and the builds seem to be correct for a certain size until it starts chunking, I suspect it may be a rolldown issue.

May be from related issues:
#4738
#4617

but can't be sure

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions