Paraglide JS compilation
Paraglide JS compilation collects source context for Paraglide message use. The package can run as the paraglide-context CLI or as the collectContext library API. It finds the application and its generated Paraglide output, follows imports as the project bundler would, analyzes supported source formats, and writes metadata beside the source-locale message files without changing Paraglide or running the application bundler.
The page Extraction and compilation gives the wider system view. For nearby concerns, see CLI orchestration, compiler transform pipeline, and language and framework extractors.
Compiler role
The compiler is a context bridge between a Paraglide application and the translation workflow. It turns message uses into stable records that the gt CLI can upload with each message, while leaving the generated Paraglide runtime and application source unchanged.
The public package has two main entry points:
| Entry point | Role |
|---|---|
paraglide-context | Scans a project from the command line and writes message metadata. |
collectContext | Runs the same scan from another Node.js program with typed options. |
The compiler package also exposes its compiler entry point through the package's ./compiler export. The scan is read-only until its metadata write stage. It does not need to start Vite, Webpack, or Rspack. Instead, it reads enough project and bundler information to reproduce their import view.
References: packages/…/package.json• L3-22 packages/…/README.md• L14-38 packages/…/compiler.ts• L1-12 packages/…/index.ts• L11-35
CLI tool installation
The CLI can prepare a project for framework-aware analysis. paraglide-context install maps requested language families to their required parser tools, checks the project and nearby dependency packages, and adds only missing tools as development dependencies through the detected package manager.
The command reports a clear error when an installed tool cannot be used. In particular, it asks for a TypeScript version with the required JavaScript API instead of silently attempting an incomplete analysis. Framework tools remain optional package peers, so projects install only the tools needed for the source formats they use.
References: packages/…/package.json• L92-142 packages/…/install.ts• L24-33 packages/…/install.ts• L87-105 packages/…/package-manager.ts• L6-43 packages/…/packages.ts• L12-65
Project and catalog discovery
Project discovery establishes the path rules for the whole scan. A project root is the nearest directory with package.json; a workspace root may be found above it from common monorepo markers. Include and exclude paths are then interpreted relative to the project root.
The collector treats a Paraglide outdir as the directory that holds generated Paraglide files. It detects one when the generated files match the installed Paraglide package. An explicit outdir can override detection when the generated files cannot be verified. Generated files are used as message and runtime targets, not as application source to scan.
The catalog is the set of message IDs exposed by an outdir. The collector reads it from generated exports without executing those files. It can follow generated re-exports and can read declarations for message modules that exist only in memory.
An outdir is paired with an Inlang project, which is the project directory that defines the source-locale message files. The pairing uses the outdir's recorded source project when available. Otherwise, it compares the outdir catalog with nearby .inlang projects and chooses the project with the strongest message-ID match.
The discovery relationship is:
flowchart TD
Root[Project or workspace root] --> Sources[Included source files]
Root --> Outdirs[Paraglide outdirs]
Outdirs --> Catalog[Generated message catalog]
Root --> Projects[Inlang project candidates]
Catalog --> Pair[Outdir to project pairing]
Projects --> Pair
Pair --> Base[Base-locale message files]
Base --> Metadata[Metadata files]
References: packages/…/roots.ts• L14-67 packages/…/collector.ts• L142-164 packages/…/outdir.ts• L78-124 packages/…/catalog.ts• L12-37 packages/…/outdir-projects.ts• L17-55 packages/…/match.ts• L4-35
Source selection
Source selection starts with the whole project unless include narrows it. The default exclusions remove tests, tool configuration, dependency directories, framework output, and build output. Project .gitignore files also affect the walk. This keeps generated or non-product code from producing translation context.
The scan also looks for auto-import declaration files. These declarations make names such as a message namespace visible in files that do not contain an explicit import. The same behavior can be configured directly or disabled.
References: packages/…/files.ts• L40-101 packages/…/files.ts• L109-165 packages/…/auto-imports.ts• L36-84 packages/…/options.ts• L5-37
Inlang message files
An Inlang project supplies the message files that receive output. The collector reads the base locale and the configured JSON path pattern, then treats leaf strings and variant arrays as message entries. It preserves other metadata fields that a person already wrote.
References: packages/…/project.ts• L9-41 packages/…/message-file.ts• L7-30 packages/…/project.ts• L53-78
Bundler-aware resolution
Import resolution answers the same product question as a bundler: which file does an application import reach? The project resolver first probes the first available supported bundler configuration. It then combines those aliases with Node-style resolution so analysis can follow application imports into Paraglide output.
The default bundler support is:
| Source of resolution | Purpose |
|---|---|
| Vite | Reads the resolved root and aliases, including aliases added by plugins. |
| Webpack | Reads resolve.alias from a config object, function, or first config in a list. |
| Rspack | Uses the same webpack-like alias rules for Rspack config files. |
| Node and TypeScript rules | Resolve package exports/imports, tsconfig paths, aliases, and common source extensions. |
The CLI can add aliases directly with --alias and can disable bundler config reading. If a possible alias stays unresolved, the resolver reports it after the scan because missing links can hide message uses.
References: packages/…/project-resolver.ts• L21-59 packages/…/project-resolver.ts• L61-105 packages/…/unresolved-imports.ts• L18-43 packages/…/index.ts• L25-35
Source analysis
Source analysis builds a module graph, which is a map of loaded modules and the imports that connect them. It loads all selected application sources before it evaluates message use, so a value passed through one module can still be linked to a Paraglide export in another. It follows application modules but stops at dependencies and recognized generated targets.
The analysis tracks the meaning of values rather than only matching text. It can recognize direct calls, method calls, references, component uses, re-exports, CommonJS exports, dynamic message names, and values passed through component props. Each use records its message or target, source range, expression, scope, and path through the module graph.
flowchart LR
Files[Selected application files] --> Parse[Language parsing or generated source]
Parse --> Graph[Module graph and resolved exports]
Targets[Paraglide generated targets] --> Graph
Graph --> Uses[Tracked message and runtime uses]
Uses --> Context[Source ranges and context snippets]
References: packages/…/collector.ts• L137-164 packages/…/module-graph.ts• L97-193 packages/…/module-graph.ts• L224-273 packages/…/analyze.ts• L61-79 packages/…/analyze.ts• L161-241
Supported source languages
The compiler gives each non-JavaScript format a language adapter. The adapter turns the format into one or more JavaScript or TypeScript modules and maps the result back to the original source. This lets template expressions, component markup, and embedded scripts take part in the same value analysis as ordinary scripts.
The default language set covers:
- Vue single-file components and VitePress Markdown.
- Svelte components.
- Astro components and their client scripts.
- MDX and ordinary Markdown.
- Angular components, templates, and Analog components.
- Marko templates.
- Ember template-tag files.
- JavaScript and TypeScript markup forms, including tagged HTML templates such as Lit-style templates.
Language tools load from the project when needed. A missing tool can skip a whole file or one part of a file, such as a Pug template. The collector reports the affected files and gives an install command instead of treating incomplete source as reliable.
The Vue and VitePress adapters use the same language service model for scripts and templates. The Svelte adapter uses the project's Svelte version and preserves mappings through generated TypeScript. These mappings keep reported context tied to the source file rather than to generated adapter code.
References: packages/…/package.json• L64-142 packages/…/index.ts• L15-39 packages/…/language.ts• L42-66 packages/…/language.ts• L94-123 packages/…/language.ts• L197-235 packages/…/markdown.ts• L18-33 packages/…/svelte.ts• L14-52 packages/…/generate.ts• L58-105 packages/…/missing-tools.ts• L48-80
Paraglide targets
Targets define which generated or installed modules have translation meaning. The default set covers message exports, runtime helpers, server helpers, the registry, and official Paraglide framework adapters. A target gives an export a tracked value or namespace, which allows the module graph to follow it without running the generated module.
References: packages/…/targets.ts• L19-40 packages/…/targets.ts• L65-102 packages/…/module-graph.ts• L237-273
Metadata outputs
Metadata connects a message ID to the source code where the application uses it. For each paired Inlang project, the collector writes one metadata file beside each base-locale JSON message file. The sourceCode field is shaped like the message file and contains project-relative files with before, target, and after text around each use.
The context radius defaults to ten lines above and below the use. Dynamic names are matched against known catalog IDs when a pattern gives enough information. Repeated snippets are collapsed, and hand-written metadata fields remain in place. Writes occur only when content changes and use a temporary file followed by a rename so readers do not see a partial file.
The complete scan has this order:
- Select source files and discover auto-import declarations.
- Detect outdirs and load their catalogs.
- Pair outdirs with Inlang projects.
- Load and analyze the reachable source modules.
- Attach each use to its project and catalog.
- Render and write the metadata files.
- Report unresolved imports, missing tools, unknown messages, and other scan limits.
References: packages/…/collector.ts• L137-168 packages/…/catalog-check.ts• L10-19 packages/…/source-code.ts• L5-20 packages/…/source-code.ts• L33-69 packages/…/metadata.ts• L19-47 packages/…/metadata-files.ts• L27-31 packages/…/metadata-files.ts• L89-145
Virtual modules
Virtual modules are bundler modules that exist in memory instead of as files. Paraglide's Vite mode uses virtual message module IDs while its generated messages.js barrel remains on disk. The compiler supports this mode as generated output: it accepts the virtual barrel shape and reads its message names from the generated declaration file.
Virtual message modules are catalog inputs, not source files to analyze. This keeps the compiler focused on the application's original source while still allowing it to resolve and validate message exports from a development or production build that uses in-memory modules.
References: packages/…/virtual-modules.ts• L4-19 packages/…/outdir.ts• L68-75 packages/…/outdir.ts• L214-233 packages/…/catalog.ts• L12-21 packages/…/catalog.ts• L148-163
Change checklist
When these files or sections are changed, remember to consider these:
- A new Paraglide generated file or export must stay aligned across target matching, catalog reading, and outdir verification.
- A new source format must be added to the default language set and its required project tools must remain optional at package load time.
- A changed virtual-module ID or declaration shape must be updated in both catalog reading and generated outdir verification.
- A changed alias or project-root rule must be checked against CLI aliases, bundler probing, and Node-style resolution.
- A changed metadata field or message path must remain compatible with the
gtCLI and with preservation of hand-written fields. - A changed language requirement must stay aligned across lazy project loading, missing-tool warnings, and
paraglide-context install. - A changed diagnostic must preserve the CLI's shared source and severity format for both usage errors and scan warnings.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| No verified or configured Paraglide outdir | Message and runtime uses cannot be recorded. | Configure outdirs when generated output cannot be verified. |
| An outdir has no matching Inlang project | Its message files receive no metadata. | Keep the compiled-from project link valid or pass inlangProjects. |
| A bundler config is unreadable or an alias stays unresolved | Imports behind that path are missed. | Fix the config or provide the alias directly. |
| A framework language tool is missing or rejects the source | That file or source part is skipped. | Install the tool with the CLI advice and review the warning. |
| A catalog lacks a referenced message ID | The use is reported as an unknown message and gets no metadata. | Regenerate the Paraglide output or correct the message use. |
| Existing metadata is unreadable or is not a JSON object | The file is left unchanged to avoid losing hand-written data. | Repair or remove the metadata file before the next scan. |
paraglide-context install cannot detect or run the package manager | Required language tools are not added. | Run the displayed package command yourself. |
Key files
| File | Why to read it |
|---|---|
| packages/…/package.json | Public package entry points, CLI binary, runtime requirement, and optional language-tool peers. |
| packages/…/compiler.ts | Public compiler exports for collection, targets, languages, and custom resolvers. |
| packages/…/index.ts | CLI contract and option mapping. |
| packages/…/install.ts | Language-tool installation flow and version checks. |
| packages/…/print.ts | Shared CLI output and usage-error formatting. |
| packages/…/collector.ts | Scan lifecycle, catalog pairing, analysis, warnings, and writes. |
| packages/…/catalog-check.ts | Catalog checks for static and dynamic message names. |
| packages/…/files.ts | Project source selection and default exclusions. |
| packages/…/package-manager.ts | Package-manager command selection and execution for language tools. |
| packages/…/packages.ts | Project-local and dependency-level language-tool loading. |
| packages/…/project-resolver.ts | Bundler-aware import resolution and alias warnings. |
| packages/…/unresolved-imports.ts | Diagnostics for imports that may be missing aliases. |
| packages/…/module-graph.ts | Module linking and Paraglide target boundaries. |
| packages/…/analyze.ts | Meaning of a tracked source use. |
| packages/…/language.ts | Common contract for framework and markup source analysis. |
| packages/…/generate.ts | Vue and VitePress source generation and source mappings. |
| packages/…/svelte.ts | Svelte source generation and source mappings. |
| packages/…/outdir.ts | Recognition of generated Paraglide output. |
| packages/…/catalog.ts | Message catalog discovery from generated and virtual output. |
| packages/…/metadata.ts | Metadata file shape and message-path mapping. |
| packages/…/metadata-files.ts | Metadata preservation and atomic file writes. |
| packages/…/source-code.ts | Source context shape and dynamic-message matching. |
| packages/…/diagnostics.ts | Common warning and error message format. |