Source extraction and parsing
Source extraction and parsing find the project files that may use Panda styles, read those files as source trees, and turn recognized style usage into structured parser results. This stage is the source-analysis part of the compiler pipeline. It uses the boundaries from configuration resolution and records atomic styles, recipes, patterns, tokens, and JSX style props before the CSS generation stage described in CSS artifact generation.
Source file discovery
Source discovery defines the set of project files that Panda can analyze. It uses the resolved include, exclude, and working-directory settings, so configuration resolution determines the input boundary for this page.
Include and exclude scope
The Node runtime expands the configured file patterns into absolute paths. Excluded paths are not analyzed, and declaration files are ignored by default when no explicit exclusion exists. A source pattern also defines the directory and pattern used for build-tool dependency tracking.
References: packages/…/create-context.ts• L61-64 packages/…/node-runtime.ts• L29-43 packages/…/parse-dependency.ts• L6-24
Source project
The parser keeps the discovered files in a TypeScript project. It loads each file with its text and maintains the source set as files are added, changed, or removed. Framework integrations can transform source text before analysis, which lets the same parser model work with source formats that need preprocessing.
References: packages/…/project.ts• L30-42 packages/…/project.ts• L52-89 packages/…/project.ts• L98-117 packages/…/project.ts• L166-172
Panda usage extraction
Extraction locates style usage in a source tree without running the application. It recognizes only the tags, functions, and templates that the resolved imports and Panda configuration identify as style-producing APIs.
Import and alias recognition
The parser first reads named and namespace imports from each source file. It matches those imports to Panda APIs and applies TypeScript path mappings when needed. This import map lets later extraction treat aliases as the correct Panda function, component, pattern, or recipe.
References: packages/…/get-import-declarations.ts• L6-49 packages/…/parser.ts• L36-40 packages/…/parser.ts• L130-135
Style usage forms
The extractor collects three broad forms of usage:
- Function calls such as CSS helpers, patterns, recipes, and token helpers.
- JSX tags and compiled JSX factory calls that carry style props.
- Tagged templates when the configured syntax uses template literals.
It groups each match by its recognized name and preserves the relationship between the source node and its style data. It skips import and export declarations because those nodes describe module wiring rather than style use.
References: packages/…/extract.ts• L24-41 packages/…/extract.ts• L143-161 packages/…/extract.ts• L213-299 packages/…/parser.ts• L136-170 packages/…/parser.ts• L208-297
Static value recovery
Extraction recovers values that are safe to determine from source. It can follow object values, spreads, conditions, and selected expressions while keeping unresolved branches separate from the direct value. This allows the compiler to retain styles from both sides of a conditional instead of silently dropping a possible use.
The evaluator uses restricted, deterministic rules. Values that cannot be resolved remain marked as unresolved and do not become ordinary style data until a later stage can handle them.
References: packages/…/evaluate-node.ts• L8-40 packages/…/extract.ts• L76-141 packages/…/unbox.ts• L31-103 packages/…/unbox.ts• L118-161
Parser result model
The parser converts extracted nodes into a per-file result that gives the rest of the compiler a stable view of Panda usage. Each result keeps the original source path and groups recognized data by the kind of output it can produce.
Usage categories
Parser results separate usage into categories that have different downstream meaning:
| Category | Compiler meaning |
|---|---|
css | Style objects or CSS templates that produce atomic styles |
cva and sva | Single-part and slot-based recipe definitions |
pattern and recipe | Named reusable styling APIs |
jsx | Style props found on Panda-aware JSX usage |
token | Token lookups that must be tracked but do not add CSS by themselves |
The result also keeps an ordered list for consumers that need the source encounter order. Named pattern and recipe results are grouped by name so the compiler can apply their own configuration.
References: packages/…/parser.ts• L3-30 packages/…/parser-result.ts• L5-18 packages/…/parser-result.ts• L151-164
Parser classification
The parser maps extracted names to their configured role. CSS helpers become CSS results, valid recipe and pattern names keep their named groups, and Panda JSX tags become either style-prop, pattern, or recipe results. Custom JSX matching can extend or replace the default Panda tag rules.
References: packages/…/parser.ts• L53-128 packages/…/parser.ts• L130-207 packages/…/parser.ts• L304-341
Encoder handoff
When the parser stores a result, it sends the recovered style data to the shared style encoder. CSS data becomes atomic style input, recipe data becomes recipe input, and JSX data becomes style-prop input. Token calls stay in the result for tracking because they refer to already-defined design tokens rather than create CSS directly.
This handoff is the boundary between source analysis and CSS generation. The generator later collects the encoder data and processes it into the stylesheet described in CSS artifact generation. The same result groups also support usage classification for reports without changing the CSS input.
References: packages/…/parser-result.ts• L27-120 packages/…/classify.ts• L295-318 packages/…/generator.ts• L82-99
Parsing lifecycle
The normal build parses every discovered file with one shared encoder, while watch and build-tool flows reparse only files whose source or configuration state changed. The result is incremental style collection rather than a separate CSS pass for each file.
flowchart LR
A[Include and exclude patterns] --> B[Discovered source paths]
B --> C[TypeScript source project]
C --> D[Import matching and AST extraction]
D --> E[Per-file ParserResult]
E --> F[Shared style encoder]
F --> G[CSS generation]
Initial build
At context creation, the project loads the discovered source files. A build then parses each file and keeps the files and non-empty parser results available for build status and later CSS assembly. The same parse pass can report which files contain usable Panda styles.
References: packages/…/create-context.ts• L32-41 packages/…/create-context.ts• L85-107 packages/…/build-info.ts• L4-18
Watch updates
Watch mode maps file events to source-project changes. An added or changed file is loaded again and parsed into the shared encoder, while a removed file is removed from the project. Configuration changes rebuild the context because they can change which files or APIs count as Panda usage.
References: packages/…/create-context.ts• L195-217 packages/…/generate.ts• L30-71 packages/…/builder.ts• L82-95 packages/…/builder.ts• L151-177
Change checklist
When these files or sections are changed, remember to consider these:
- Keep source matching, import alias matching, and JSX matching aligned. A change in one can make a valid style call invisible without causing a parse error.
- Keep every parser result category aligned across the result type, result storage, and encoder handoff. A new category that is not stored or encoded can silently omit styles.
- Keep source-project refresh behavior aligned with file discovery. New, changed, and removed files must reach the same project that the initial build parses.
- Keep glob dependency messages aligned with the include patterns. Build tools use those messages to know when a source set may have changed.
- Keep parser transforms aligned with framework plugins. The transformed text must match the source path that is later reported and reparsed.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| An include or exclude pattern misses a source file | Panda does not see its style usage, so its CSS is absent | Check the discovered file set and the dependency message derived from the same patterns |
| An import alias or path mapping is not recognized | Calls that look like Panda usage are treated as ordinary code | Keep import matching and TypeScript path mappings consistent with generated API paths |
| A value cannot be resolved statically | The parser preserves no direct style data for that value | Use resolvable source values or a supported dynamic path; unresolved values are not silently converted into CSS |
| A changed file is not refreshed in the source project | Watch mode can rebuild from stale source text | Route add, change, and unlink events through the project lifecycle |
| A parser result is stored without encoder processing | The result may be visible to reports while its CSS is missing | Use the result setters that perform the matching encoder handoff |
Key files
| File | Why to read it |
|---|---|
| packages/…/create-context.ts | Connects configuration file patterns, the source project, parsing, and build-time results. |
| packages/…/node-runtime.ts | Defines file glob expansion and file watching for the Node build environment. |
| packages/…/project.ts | Owns the source project lifecycle and the hooks around source transformation and parsing. |
| packages/…/parser.ts | Maps imports and extracted usage to the parser result categories consumed by compilation. |
| packages/…/parser-result.ts | Defines the per-file result and the handoff from parsed data to the style encoder. |
| packages/…/extract.ts | Shows the high-level source analysis that recognizes functions, JSX, and tagged templates. |
| packages/…/unbox.ts | Shows how extracted values and conditional branches become plain style data. |
| packages/…/generator.ts | Shows where encoded source usage enters stylesheet processing. |