Skip to content

[nextjs] webpack keeps debug log code when excludeDebugStatements is set #25266

Open

Description

@andreiborza

Problem

In Next.js webpack builds, bundleSizeOptimizations.excludeDebugStatements (and the old webpack.treeshake.removeDebugLogging) does not remove the debug log code from the client bundle. The code cannot run, but it stays in the bundle and adds size. Turbopack builds remove it correctly.

Found while testing #24818 on Next 15.5.27 and 16.3.6. Tracing removal works on both bundlers.

Cause

  • The SDK guards debug logs with DEBUG_BUILD, a constant exported from @sentry/core/build/esm/debug-build.js.
  • The define works. In the bundle, that module becomes let n=!1.
  • The minifier can only remove DEBUG_BUILD && debug.log(...) when webpack concatenates the modules into one scope. Webpack does not concatenate here. Stats give this bailout:
    Cannot concat with ./node_modules/@sentry/core/build/esm/debug-build.js: Module ./node_modules/@sentry/nextjs/build/esm/client/index.js is referenced from different chunks by these modules: ./instrumentation-client.ts
  • So the code reads DEBUG_BUILD through a getter at runtime, and the guarded code stays.

The same issue probably affects other webpack apps where the SDK is in a shared chunk, not only Next.js.

Possible fix

Check typeof __SENTRY_DEBUG__ at each use site, so that each module gets the literal from the define and does not import a shared constant. This touches all packages, so measure the size effect on the CDN bundles too.

Repro

~/projects/experiments/nextjs-turbopack-define-booleans (ask @andrei.borza). Build with next build --webpack and bundleSizeOptimizations: { excludeDebugStatements: true }, then search the client chunks for Integration installed.

Activity

  1. linear-code commented on Oct 12, 2026

    @linear-code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions