How baidu/san-ssr works
Project type and purpose
• This is an open-source, CLI and programmatic library (“san-ssr”) that performs static analysis and server-side rendering code generation for San framework components.
• It exposes both a command-line front end (bin/) and a TypeScript/JavaScript API (src/models/SanProject).
File-structure analysis
• Top-level directories:
• bin/ – CLI entry scripts (debug, render-by-source, on-the-fly).
• demo/ – example usage to illustrate API and CLI.
• src/ – core modules, organized into:
– parsers/ – turn San/JS/TS source files into AST.
– ast/ – AST utilities, walkers, type guards.
– optimizers/ – AST transforms (bracket-to-dot, literal merges).
– compilers/ – compile ASTs into intermediate compiler models.
– runtime/ – runtime helpers for the generated SSR code.
– target-js/ – JS target implementation of the Compiler interface (emit JS code).
– models/ – type definitions for project, components, compiler, options.
– helpers/ and utils/ – cross-cutting utility functions.
– bin/ – programmatic entry point for SSR in code.
• test/ – unit, E2E, performance, stub and fixture cases.
• types/ – TypeScript declarations for consumers.
• Configuration files (jest.config.js, tsconfig.json, eslint, CI workflows) support testing, linting, and release automation.
Insights from README
• Core abstraction: SanProject class orchestrates end-to-end pipeline: component input → parser → AST → optimization → Compiler interface → target code → render function → HTML output.
• Supports multiple targets (js, php) via san-ssr-target-<target> plugins.
• CLI flag --target selects emitter implementation.
Components
1) CLI / API Layer
2) SanProject Orchestrator
3) Parsers Module
4) AST Utilities
5) Optimizers
6) Compiler Module
7) Target Code Generators (JS / PHP etc.)
8) Runtime Helpers
9) File I/O & Configuration
10) Demo & Tests (supporting examples & validation)
b. Relationships and data flow:
• CLI/API → SanProject
• SanProject → Parsers → produces AST
• AST → Optimizers → transforms AST
• Optimized AST → Compiler → intermediate representation
• IR → Target Code Generator → SSR code (JS or PHP)
• SSR code + Runtime Helpers → render function → HTML string
• File I/O for reading component sources and writing output
• Tests and Demo consume API & CLI to validate pipeline
c. Architectural patterns & principles:
• Pipeline pattern: discrete, composable stages (parse → optimize → compile → emit).
• Plugin/strategy pattern for target code generation (implements a common Compiler interface).
• Modular separation by concern (parsing vs. AST utilities vs. optimization vs. codegen).
• Strong typings via TypeScript for models and interfaces.
d. Key technologies/frameworks:
• Node.js (bin scripts)
• TypeScript (src, tests)
• Jest (unit & E2E tests)
• Semantic Release + GitHub Actions (CI/CD)
• San template parser (peer dependency)