Introducing a new way to review your PRs.

Read the announcement

generaltranslation / gt

Full-stack localization for every framework

View on GitHub

22 documents · Updated Nov 23, 2026

docs / bundle-analysis

Bundle analysis tool

The bundle analysis tool measures the JavaScript cost of General Translation packages in real framework apps. It builds each example with source maps, links emitted bytes back to their package and source file, and presents the result in a live treemap or a JSON API. It is a child of translation operations and developer tools and complements the translation API client, GTX command-line workflows, Locadex operations, Sanity CMS integration, and session recording localization.

Product purpose

The tool gives engineers a common way to answer two questions: how much code does a framework integration add, and which files explain a change between builds. It measures published workspace dist output inside representative apps, so the result reflects the package boundary that an app uses rather than an isolated library build.

Analysis workspace

Workspace package discovery

The analysis server discovers packages from the repository packages workspace and reads each package's published name. It uses this set to recognize General Translation packages in source maps and to refresh measurements when a package's dist output changes. References: tools/…/workspace.ts• L5-27, tools/…/main.ts• L32-40

Example registration

Each bundled example is a named measurement target with a framework label, an owning GT package, and a description of the bundles that its framework can emit. The registration also tells the analyzer where to find client, server, or edge JavaScript after a build. References: tools/…/examples.ts• L6-17, tools/…/examples.ts• L60-121

The examples use workspace:* links to the packages under analysis. The tool therefore measures local package output directly, without packing or copying a registry artifact first. References: tools/…/README.md• L48-55, tools/…/package.json• L12-24, tools/…/package.json• L12-23

Build and sourcemap analysis

Production build

Opening an example starts its production build when no current measurement exists or when its sources are stale. The build can use minification and tree shaking, and those settings become part of the measurement so that comparisons use the same build conditions. References: tools/…/builder.ts• L15-16, tools/…/builder.ts• L264-285, tools/…/types.ts• L7-10

Source map attribution

The analyzer assigns the bytes of each emitted JavaScript file to the nearest preceding source-map segment on the same line. It resolves package, app, bundler, and unmapped sources into stable owners, then rolls the result into module and package totals. This makes the report explain where bundle size comes from instead of only reporting a file total. References: tools/…/sourcemap.ts• L5-10, tools/…/analyze.ts• L18-27, tools/…/workspace.ts• L30-36

Bundle measurements

Each report records emitted file size, gzip size, total JavaScript bytes, and the module-level attribution that adds up to the total. CSS is outside the measurement. A framework contributes only the bundle kinds that its build emits, such as client and server output or an optional edge output. References: tools/…/analyze.ts• L39-77, tools/…/types.ts• L21-40, tools/…/README.md• L68-75

The build and analysis flow moves from a registered example to a production build, then through source maps to a report that the UI and API can consume.

flowchart LR
    E[Example definition] --> B[Production build]
    B --> J[Emitted JavaScript]
    J --> M[Source maps]
    M --> A[Module and package attribution]
    A --> R[Current report]
    R --> U[UI treemap]
    R --> API[JSON API]

Reports and live updates

Report summaries

The report layer turns module measurements into product-facing summaries. It exposes total and gzip bytes, GT package bytes and share, the largest packages, available bundle kinds, build time, and whether the current result is stale. References: tools/…/report.ts• L17-79, tools/…/guide.ts• L5-20

Build comparisons

The tool keeps the prior distinct measurement for each example. It compares current and prior module sizes to show total change, GT change, and the files with the largest growth or reduction. Repeated builds with identical measurements do not erase the comparison point. References: tools/…/report.ts• L114-155, tools/…/builder.ts• L217-231, tools/…/tree.ts• L59-75

Watch loop

The server watches package dist output and example source. A quiet period groups related writes, marks affected measurements stale, and rebuilds examples that are open in the UI. The browser receives the full example state over a server-sent event stream, so size changes and build status appear without a page refresh. References: tools/…/main.ts• L49-97, tools/…/main.ts• L229-246, tools/…/stream.ts• L1-49

Agent API

The same measurements are available without the browser through /api and /llms.txt. Consumers can list examples, request a fresh summary, inspect package or file detail, read a diff, start a build, or change build settings. References: tools/…/guide.ts• L3-40, tools/…/main.ts• L194-205, tools/…/main.ts• L249-343

Safety boundaries

Local request boundary

The server listens on loopback and accepts requests only through a localhost address. API requests marked as coming from another site are refused, and state-changing requests accept JSON only. These checks matter because a page in the same browser could otherwise start builds or change settings. References: tools/…/main.ts• L398-420, tools/…/guard.ts• L1-20, tools/…/guard.ts• L23-63

Build state protection

The build manager treats a failed or interrupted build as stale rather than as a fresh measurement. A source change during a build queues a follow-up pass, and a superseded build is stopped before a newer one takes its place. This keeps the displayed result tied to the latest known sources. References: tools/…/builder.ts• L18-35, tools/…/builder.ts• L138-205, tools/…/builder.ts• L286-329

Output safeguards

The analyzer treats missing source maps as unmapped bytes instead of silently assigning them to a package. It also retries once when a build output directory changes while it is being read. These safeguards expose incomplete measurements and reduce false results during concurrent builds. References: tools/…/analyze.ts• L49-67, tools/…/builder.ts• L232-253

Interactive treemap UI

Example selection

The home page presents each framework example with its package name, preview, and latest client size when available. Selecting a card opens a live analysis page for that example. References: tools/…/Home.tsx• L51-101, tools/…/main.tsx• L1-20

Bundle exploration

The analysis page lets an engineer switch between the bundle kinds emitted by the selected framework. It turns module data into a hierarchy of package, folder, and file, then sizes each tile by its byte contribution. Users can zoom into groups, search file paths, focus tiles with the keyboard, and inspect size and change details in a tooltip. References: tools/…/Analysis.tsx• L25-51, tools/…/tree.ts• L1-20, tools/…/Treemap.tsx• L14-50, tools/…/Treemap.tsx• L97-121

Build controls

The sidebar shows bundle size, GT bytes, GT share, and the change from the prior build. Minification and tree shaking controls trigger a new measurement, which lets engineers see how build policy affects package cost. References: tools/…/Analysis.tsx• L260-357, tools/…/main.ts• L302-343

Framework examples

The bundled examples cover the main framework paths supported by the repository. They are small reference apps with committed translation data, so analysis does not need a translation service or API key.

ExampleGT packageFrameworkBundle view
next-appgt-nextNext.js App Router with Turbopackclient, server, edge when emitted
tanstack-startgt-tanstack-startTanStack Start with Viteclient and server
vite-reactgt-reactReact single-page app with Viteclient
vite-vuegt-vueVue app with Viteclient

The examples share analysis switches for source maps, minification, and tree shaking. The Next.js target also maps its framework-specific source paths back to the example root so its server and client output can use the same ownership model. References: tools/…/examples.ts• L60-121, tools/…/vite.config.ts• L4-20, tools/…/next.config.ts• L5-31, tools/…/README.md• L57-75

Change checklist

When these files or sections are changed, remember to consider these:

  • When adding or renaming an example, update its registration, emitted-bundle collectors, package link, and optional preview together.
  • When changing source-map or bundler settings, confirm that package bytes still resolve to published dist paths and that framework-specific virtual paths remain classifiable.
  • When changing report fields or bundle kinds, update the shared types, API summaries, UI state stream, and agent guide as one contract.
  • When changing watch or cache behavior, preserve the stale-state and previous-measurement rules that drive live deltas.
  • When changing request guards, check both browser UI requests and direct agent requests because they use different headers.

Important failure modes

TriggerConsequenceGuard
An example emits a bundle kind that its registration does not collectThat output is absent from the report and UIKeep the example collector and BundleKind registration aligned
A build finishes without usable source-map coverageBytes appear as (unmapped) and package share can be understatedAnalyze only with source maps and inspect unmapped output
Package or example files change during analysisA report can describe an older output directoryRetry a missing-output read and keep the example stale until a successful build
A source changes while a build is runningThe first result can be out of dateQueue a follow-up build and publish only the settled state
A cross-site page sends an API requestAn unwanted build or setting change could run in the local workspaceRequire localhost and reject cross-site API use
A requested bundle does not exist for the frameworkThe detail request cannot produce a reportReturn the emitted bundle list and a clear not-found response

Key files

FileWhy to read it
tools/…/README.mdProduct purpose, local workflow, measurement rules, and example extension path
tools/…/main.tsServer lifecycle, watch loop, API routes, event stream, and local server boundary
tools/…/examples.tsSupported framework targets and bundle discovery rules
tools/…/builder.tsBuild freshness, settings, caching, retries, and comparisons
tools/…/analyze.tsConversion from emitted JavaScript and source maps to bundle measurements
tools/…/sourcemap.tsSource-map loading, path resolution, and byte attribution model
tools/…/report.tsAPI summaries, package detail, and build diffs
tools/…/Analysis.tsxAnalysis page behavior, bundle controls, search, and live status
tools/…/Treemap.tsxVisual size map, change marks, hover details, and keyboard interaction
tools/…/types.tsShared contract between the analysis server and browser UI
tools/…/next.config.tsNext.js analysis settings and source-map behavior
tools/…/vite.config.tsShared Vite analysis switches in a client-only example