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

Offen
#25,266 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
35/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Aktiv
Tech-Stack
next.js, typescript, webpack
Bereich
backend, performance

Rechercherichtung

Start with the DEBUG_BUILD export in @sentry/core/build/esm/debug-build.js and the client entry point @sentry/nextjs/build/esm/client/index.js; reproduce with the provided experiment using next build --webpack and excludeDebugStatements: true. Check the webpack stats bailout and search client chunks for Integration installed. Done means debug log code is removed from webpack client bundles, with CDN bundle size effects measured across packages.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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 getsentry/sentry-javascript#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.

Vorherrschende Sprache
TypeScript
Sterne
8.8k
Forks
1.9k
Ø Merge
1 T. 8 Std.
Gemergte PRs (30 T.)
744

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus getsentry/sentry-javascript

Alle Issues in getsentry/sentry-javascript

Ähnliche Issues

Weitere Issues zu TypeScript

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.