Compiler transform pipeline
The @generaltranslation/compiler package turns eligible application modules into localization-aware code during a build. It prepares translation calls and content-bearing components for runtime lookup or extraction by applying ordered source transforms. It does not translate content itself; it creates the identity and message data that the wider extraction and compilation pipeline can use.
Compiler role
The compiler runs between application source processing and the localization systems that extract or resolve messages. It is exposed as a universal build plugin for webpack, Vite, Rollup, esbuild, and Rspack, so supported bundlers can apply the same transform behavior. The package is currently version 1.3.70, and it requires Node.js 16 or later. The plugin tests also check that each bundler adapter exposes the same transform entry point. packages/…/package.json• L1-5 packages/…/package.json• L58-63 packages/…/index.test.ts• L286-297
The compiler has two main outputs:
- Instrumented application code with stable hashes and message metadata for runtime lookup.
- Structured source data that extraction workflows can use for strings and serialized JSX content.
Configuration inputs
Plugin settings
Plugin settings choose which source changes and checks are active. Direct plugin options can be combined with shared GT configuration, and the plugin can load gt.config.json when no direct configuration is supplied. The settings that most affect the pipeline are:
| Setting | Pipeline effect |
|---|---|
compileTimeHash | Adds known hashes to supported calls and components. It is enabled by default. |
enableMacroTransform | Converts supported tagged string syntax into regular translation calls. |
enableAutoJsxInjection | Adds internal translation and variable regions around eligible compiled JSX. |
autoderive | Allows selected dynamic content to remain for later resolution. |
disableBuildChecks | Logs invalid use and keeps the original module instead of stopping the build. |
devHotReload | Adds development-time runtime translation requests for selected content. |
The compiler also accepts settings for macro names, generated import sources, logging, legacy React imports, and hash diagnostics. These settings affect recognition or generated code without changing the broad pass order.
References: packages/…/index.test.ts• L132-223 packages/…/index.test.ts• L226-284 packages/…/CHANGELOG.md• L3-9 packages/…/package.json• L1-24
Source eligibility
The plugin processes JavaScript, TypeScript, JSX, and TSX modules. It parses these modules as source that can contain JSX, TypeScript, and legacy decorators. A module can pass through unchanged when no active stage needs to alter it.
References: packages/…/package.json• L40-47 packages/…/index.test.ts• L90-121
Processing state
Module state
Each module is processed with its own state. This state carries the settings, collected message records, scope knowledge, validation errors, logging context, and change counts needed by later stages. It supports one module transformation; it is not a shared translation catalog.
The state links the stages in three ways:
- Scope knowledge tells the collector whether a name refers to a GT translation API.
- Collected records give injection and development instrumentation the content and identity they need.
- Error and change records determine whether the build stops and whether the transformed module is emitted.
References: packages/…/types.ts• L1-120 packages/…/initializeState.ts• L1-100 packages/…/testHelpers.ts• L23-68
Scope and identity tracking
The compiler must distinguish a GT translation value from an unrelated local function with the same name. It tracks imports, aliases, declarations, and nested scopes so shadowed names do not trigger localization transforms.
Collection and injection also share an ordered record position. Both stages must see recognized call sites in the same order so each generated hash or message record is attached to the correct source location.
References: packages/…/ScopeTracker.ts• L1-180 packages/…/hashInjection.test.ts• L348-363
Collected localization data
The collection stage records string content, JSX content, and entries that already have an identity supplied by the source. Each record can include message metadata and a hash when the content is known at build time. Content that depends on later derivation remains marked for downstream resolution instead of receiving a false identity.
References: packages/…/hashInjection.test.ts• L1-19 packages/…/hashInjection.test.ts• L264-363 packages/…/stringExtractionE2E.test.ts• L14-39 packages/…/validateTranslationFunction.test.ts• L245-285
Ordered passes
Pass sequence
The compiler uses a four-pass Babel pipeline for eligible modules. It applies optional normalization, analysis, validation, and instrumentation in a fixed order. Earlier stages create forms that later stages can understand, while collection supplies the records consumed by the final source changes. Development runtime instrumentation is an optional stage after the core passes.
The following diagram shows how source becomes instrumented or extractable localization code.
flowchart LR
A[Eligible module] --> B[Parse source]
B --> C[Optional JSX insertion]
C --> D[Optional macro expansion]
D --> E[Collect and validate]
E --> F{Validation succeeds?}
F -- no --> G[Stop build or keep original module]
F -- yes --> H[Optional hash and metadata injection]
H --> I[Optional development runtime requests]
I --> J[Emit transformed module]
The stages have these roles:
- JSX insertion exposes eligible rendered text and dynamic values to localization.
- Macro expansion gives supported tagged string syntax the same shape as ordinary translation calls.
- Collection recognizes GT APIs, resolves known content, validates use, computes identities, and records localization data.
- Error handling applies the configured build policy to collected validation errors.
- Injection adds hashes or message metadata when compile-time identity is enabled.
- Runtime translation adds development requests when hot reload is enabled.
Collection is the central analysis stage. Injection and runtime translation use its records instead of discovering the source again.
References: packages/…/CLAUDE.md• L7-14 packages/…/testHelpers.ts• L13-68 packages/…/index.test.ts• L226-284
Pass boundaries
The stages keep analysis state separate from final source instrumentation. This lets syntax normalization prepare source for collection while preserving the names and records that collection and injection must share.
The compiler emits a replacement only when an active stage changes the module or when collected data requires generated metadata. Otherwise, the bundler can continue with no replacement. If collection reports validation errors, later source changes do not run and the original module is retained under the configured error policy.
References: packages/…/testHelpers.ts• L35-68 packages/…/index.test.ts• L205-223 packages/…/jsxInsertionE2E.test.ts• L1-16
Source transforms
JSX insertion
JSX insertion makes eligible rendered text visible to localization without requiring a developer to wrap every text region by hand. It works on compiled JSX runtime calls rather than raw JSX tags. The transform adds an internal translation region around the highest suitable text area and an internal variable region around each dynamic expression inside it.
User-written translation and variable components remain under application control. Structured boundaries such as branch, plural, derive, and static content affect where automatic regions can start or stop. The compiler and CLI extraction path must apply the same boundaries because the resulting serialized JSX is part of message identity.
Each dynamic child gets its own variable region. A parent region does not claim a separate component property, and user-written GT components remain opaque to automatic insertion. Branch and Plural content uses a shared child counter for rendered children, while each branch property starts from the branching element's ID. This keeps variable keys and serialized JSX identities aligned with the rendered <T> content.
References: packages/…/processCallExpression.ts• L1-180 packages/…/jsxInsertionPass.test.ts• L141-189 packages/…/jsxInsertionPass.test.ts• L294-376 packages/…/jsxInsertionE2E.test.ts• L77-115 packages/…/hashInjection.test.ts• L202-261 packages/…/CHANGELOG.md• L54-58
Macro expansion
Macro expansion makes supported tagged string syntax look like an ordinary translation call before collection. A template with interpolated values becomes a message with numbered placeholders and a mapping from those placeholders to source expressions.
Static expressions can be folded into message text. derive() expressions remain in the message for later resolution, while other dynamic expressions become placeholders. The transform adds a suitable translation import when the rewritten module needs one. Call arguments are not rewritten by this pass.
References: packages/…/processTaggedTemplateExpression.ts• L1-120 packages/…/macroExpansionPass.test.ts• L11-78 packages/…/macroExpansionPass.test.ts• L80-119 packages/…/transformTemplateLiteral.test.ts• L51-99
Collection and validation
Collection finds recognized GT imports, aliases, translation calls, callbacks, and compiled translation components. It checks message expressions, options, and JSX children before it records their source content and metadata.
Known static expressions can become extractable messages. Dynamic or derive-dependent values remain distinct from static content. When auto-derive is active, the compiler can preserve content for later resolution instead of assigning a hash that cannot identify the final message. Callback messages can be preloaded, while standalone t() and msg() entries remain runtime-only records.
References: packages/…/processCallExpression.ts• L1-180 packages/…/StringCollector.ts• L1-160 packages/…/validateTranslationFunction.test.ts• L57-243 packages/…/stringExtractionE2E.test.ts• L45-112
Metadata injection
Metadata injection revisits recognized calls and components after collection. It adds known hashes to standalone translation calls and component options. Callback-based APIs receive collected message records with their hashes and supported metadata.
The transform does not replace an explicit source hash. It also avoids injecting a hash, including an empty hash, for content whose identity depends on later derivation. It leaves those entries out of useGT preload data as well. This keeps runtime lookup from using an identity that does not match the resolved message and prevents a derived call from shifting the hash assigned to a later static call.
References: packages/…/injectCallbackDeclaratorFunctionParameters.ts• L1-140 packages/…/injectionPass.ts• L1-100 packages/…/hashInjection.test.ts• L264-363 packages/…/hashInjection.test.ts• L394-420
Runtime translation instrumentation
Development hot reload is an optional final transform. It turns eligible collected string and JSX records into runtime translation requests and groups those requests so the module can ask for selected messages during development.
String and JSX hot reload settings are independent. A module can request one content type without adding runtime hooks for the other. The transform adds only the imports needed by the requests it creates, and it batches requests into one development operation.
References: packages/…/processProgram.ts• L1-160 packages/…/runtimeTranslatePass.test.ts• L49-73 packages/…/runtimeTranslatePass.test.ts• L160-207
Localization outputs
Instrumented application code
When compile-time hashing is active, the emitted module keeps its application behavior while carrying lookup data beside translation calls and components. Runtime systems use this data to connect rendered or requested source content with localized results.
The default enables hash injection. Setting compileTimeHash to false leaves files unchanged unless another enabled transform, such as macro expansion, has work to do.
References: packages/…/processCallExpression.ts• L1-140 packages/…/index.test.ts• L226-284 packages/…/CHANGELOG.md• L337-339
Extractable localization content
The collected view gives extraction workflows structured strings and serialized JSX content. Each record includes its source data and available identity information. Records that depend on runtime values signal that their final form must be resolved later.
The compiler is one boundary in the wider flow. Language and framework extractors find source patterns outside this transform, while the CLI coordinates source discovery and artifact handling. See language and framework extractors and CLI orchestration for those related roles.
References: packages/…/StringCollector.ts• L1-160 packages/…/constructJsxChildren.ts• L48-68 packages/…/stringExtractionE2E.test.ts• L14-39
Cross-system boundaries
Hash agreement
A hash is an identity contract between compiled application code, extraction, and runtime lookup. It must use the same source representation and identity-affecting metadata in every path. JSX wrapper boundaries matter because they change the serialized content used for identity.
The runtime counter used for JSX element IDs is also part of this agreement. If Branch or Plural children advance IDs differently during compilation and rendering, variable keys can point to different content and hashes can diverge.
References: packages/…/calculateHash.test.ts• L6-23 packages/…/calculateHash.test.ts• L59-87 packages/…/jsxInsertionE2E.test.ts• L77-115 packages/…/hashInjection.test.ts• L202-261
Error boundary
Validation errors are gathered during analysis and handled after collection. With build checks enabled, invalid use stops the build. With checks disabled, the compiler logs the errors and returns the original module instead of emitting later injected code.
References: packages/…/index.test.ts• L226-284 packages/…/testHelpers.ts• L59-68 packages/…/validateTranslationFunction.test.ts• L94-128
Related pages
- Extraction and compilation pipeline — repository-level flow from command invocation to localization artifacts.
- CLI orchestration and repository workflows — command and file workflow around compilation and extraction.
- Language and framework extractors — source discovery and framework-specific extraction behavior.
- Paraglide JS compilation — related compilation flow for Paraglide JS output.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep collection and injection record order aligned when recognized call sites change.
- Keep compiler JSX boundaries and runtime ID numbering aligned with CLI extraction so serialized content and hashes match.
- Review all identity inputs when message metadata or data formats change.
- Preserve the relationship between direct plugin settings and shared GT configuration, including config loading and option precedence.
- Keep macro rewriting and collection support aligned for every supported macro form.
- Keep derive-dependent content out of compile-time hash injection and preload data until a later stage can resolve its final variant.
- Check the
generaltranslationand@generaltranslation/formatdependency contract when publishing a new compiler version because the current1.3.70release uses9.5.13and0.1.11.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Collection and injection process recognized sites in different orders | Metadata can attach to the wrong call or component. | Preserve one shared record order across both stages. |
| Compiler JSX rules differ from CLI extraction rules | Runtime and extracted identities can disagree. | Keep both paths aligned on wrapper boundaries and ID numbering. |
Branch or Plural children use a different ID counter from the runtime | Variable keys or hashes can identify the wrong content. | Continue the shared child counter and restart only branch properties from the branching element's ID. |
| Content depends on derivation or an allowed dynamic value | No compile-time hash identifies the final message. | Keep the unresolved marker and resolve it downstream. |
| A translation call or component fails validation | The build stops, or the original module passes through when checks are disabled. | Apply the configured error policy before injection. |
| The compiler runs before JSX becomes runtime calls | Automatic JSX insertion cannot find the expected source shape. | Run the plugin at the stage where compiled JSX is available. |
| The compiler package and its runtime or format dependencies use incompatible contracts | Generated metadata or runtime lookup can fail after publication. | Review dependency changes with the compiler release. |
Key files
| File | Why to read it |
|---|---|
| packages/…/package.json | Defines the published compiler package, supported bundler role, runtime floor, and release commands. |
| packages/…/CHANGELOG.md | Records release changes and dependency updates that affect the package contract. |
| packages/…/index.ts | Shows plugin entry, source eligibility, pass order, error handling, and output emission. |
| packages/…/config.ts | Defines public compiler settings and their effect on processing. |
| packages/…/types.ts | Describes the state shared by processing stages. |
| packages/…/StringCollector.ts | Shows how source content, identity data, and record order move between stages. |
| packages/…/ScopeTracker.ts | Explains how imports, aliases, and shadowed names control API recognition. |
| packages/…/jsxInsertionPass.ts | Entry point for automatic localization regions around compiled JSX. |
| packages/…/processCallExpression.ts | Defines the main JSX boundaries for text, dynamic values, and user components. |
| packages/…/processTaggedTemplateExpression.ts | Shows how tagged string syntax becomes a normal translation call. |
| packages/…/processCallExpression.ts | Connects recognized translation APIs to validation, identity, and collection. |
| packages/…/constructJsxChildren.ts | Defines serialized JSX child structure and the ID rules shared with runtime content. |
| packages/…/jsxInsertionE2E.test.ts | Demonstrates that inserted JSX regions survive collection and preserve branch numbering. |
| packages/…/hashInjection.test.ts | Demonstrates hash injection, derived-content exclusion, and callback alignment. |
| packages/…/injectCallbackDeclaratorFunctionParameters.ts | Shows how collected callback messages become generated metadata. |
| packages/…/processProgram.ts | Shows how development runtime requests use collected content. |
| packages/…/calculateHash.ts | Defines the shared identity rules for compiled localization content. |