Skip to content

When using Next.js with Turbopack and SWC, errors occur during compression of third‑party library code. #86568

@branderzhai

Description

@branderzhai

Link to the code that reproduces this issue

https://github.com/branderzhai/bug/tree/main/nextjs_swc_issue

To Reproduce

When building a project with the default Turbopack,SWC will re-compress the code of third‑party libraries during bundling. However, due to a bug in SWC, the compressed code becomes semantically inequivalent to the original.

I extracted the part of the real project where the error occurred, and the same issue can be reproduced. The reproduction steps are as follows.

nextjs_swc_issue.zip
Reproduce Steps

  1. unzip the demo.
  2. pnpm i
  3. pnpm build
  4. pnpm start
  5. clicking the button

Exception thrown, error in console.

Current vs. Expected behavior

As you can see, the original code (or use "pnpm dev" to run up the project) like below

Image

However, after the SWC process, an internal method call was inlined into the main function, but its variable names were not handled correctly.
Image


When I replaced the default build tool with webpack and terser, everything returned to normal.

Provide environment information

Operating System:
Platform: windows
Version: Windows 11

Binaries: 
Node: 20.15.1
npm:10.9.0
pnpm:9.4.0

Relevant Packages:
    "next": "16.0.4",
    "react": "19.2.0",
    "react-dom": "19.2.0"

Which area(s) are affected? (Select all that apply)

Turbopack, SWC

Which stage(s) are affected? (Select all that apply)

next build (local)

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    SWCRelated to minification/transpilation in Next.js.TurbopackRelated to Turbopack with Next.js.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions