@proteus-vue/compiler-backend 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # @proteus-vue/compiler-backend
2
+
3
+ > **G-29 编译器可插拔后端**(`docs/proteus-compiler-backend-1-plan/`)· B1
4
+
5
+ ## 一句话
6
+
7
+ **编译器从"固定 Node 工具链"升级为"可插拔后端"**——Node / Rust(SWC-ecosystem) / WASM 三端后端对同一份 SFC 产出语义等价的 `CompilerIR`,业务零感知一个 flag 切换。与 G-27 `ProteusRenderBackend`、G-28 `ProteusNativeBackend` 同构(语义契约 + 后端实现 + conformance)。
8
+
9
+ ## 内容
10
+
11
+ | 模块 | 说明 |
12
+ |------|------|
13
+ | `spi.ts` | `ProteusCompilerBackend` 接口 + `CompilerIR` 契约(version/render/semantic/bindings)+ `CompilerCapabilities`(对齐 plan 02-compiler-backend-spi.md) |
14
+ | `conformance.ts` | `runCompilerConformance(backend)` 自检——CMP004 版本协商 + CMP002 IR 合规(render/semantic 计数交叉核对)+ ★G-31.1 语义链接(p-* 标签 → TAG_SEMANTIC_MAP) |
15
+ | `node.ts` | **NodeBackend** 参考实现:真实模板编译(@vue/compiler-sfc + @vue/compiler-dom)→ CompilerIR,**semantic = toComponentIR 产物**——「源码 → C-IR」生产端雏形 |
16
+
17
+ ## 用法
18
+
19
+ ```ts
20
+ import { createNodeCompilerBackend, runCompilerConformance } from '@proteus-vue/compiler-backend'
21
+
22
+ const backend = createNodeCompilerBackend()
23
+ const result = runCompilerConformance(backend) // { ok: true, checks: [...] } —— CMP002/CMP004
24
+
25
+ const ir = backend.compile({ filename: 'grid.vue', source: `<template><p-grid :min-col-width="160" :max-cols="4"><p-box /></p-grid></template>` })
26
+ ir.semantic.tree // { tag: 'p-grid', semantic: 'layout.grid', props: { minColWidth: { expr: '160' }, maxCols: { expr: '4' } }, children: [...] }
27
+ ir.render.root // 渲染树(含 semantic 字段——G-27 nodeOps 消费)
28
+ ir.bindings // capability 入口 + v-model + 事件(G-28 消费)
29
+ ```
30
+
31
+ ## 与 G-31 衔接
32
+
33
+ `CompilerIR.semantic` = C-IR 树(`toComponentIR` 产物)——**真实模板编译接语义层**:
34
+ Renderer 树兼含语义组件(Layer 0)与兼容层标签(Layer 1);语义计数交叉核对(render semantic 节点数 == semanticCount == C-IR 树节点数)保证两棵树同源不漂移。
35
+
36
+ ## 严格规则
37
+
38
+ - **G-29.1**:三端 Backend 对同一份 SFC 必须产出语义等价的 CompilerIR(IR Golden Test 强制)
39
+ - **G-29.2**:新 Compiler Backend 必须通过 conformance test
40
+ - **G-29.3**:HMR 语义三端一致
41
+ - **CMP001-004**:依赖私有 API / IR 不合规 / HMR 不一致 / 版本不兼容
42
+
43
+ ## 路线
44
+
45
+ B1 CompilerIR 契约 + NodeBackend ✅ → B2 RustBackend(SWC-ecosystem)→ B3 WASM Backend(Playground)→ B4 HMR 三端一致 + Source Map + Tree-shaking
@@ -0,0 +1,19 @@
1
+ import type { ProteusCompilerBackend } from './spi';
2
+ export interface ConformanceCheck {
3
+ name: string;
4
+ pass: boolean;
5
+ detail?: string;
6
+ }
7
+ export interface ConformanceResult {
8
+ ok: boolean;
9
+ checks: ConformanceCheck[];
10
+ }
11
+ /** 默认 conformance fixture(一份含布局原语 + 兼容层 + 能力入口 + 事件/模型的真实 SFC) */
12
+ export declare const DEFAULT_CONFORMANCE_SFC = "<template>\n <p-stack :gap=\"12\">\n <p-grid :min-col-width=\"160\" :max-cols=\"4\">\n <p-box />\n <view class=\"compat\"></view>\n </p-grid>\n <p-text>{{ title }}</p-text>\n <p-button @click=\"onSave\">\u4FDD\u5B58</p-button>\n <p-input v-model=\"keyword\" />\n <p-scan-qr />\n </p-stack>\n</template>";
13
+ /**
14
+ * CompilerBackend 接口完整性 + IR 产出合规自检
15
+ * - CMP004:minCompatVersion ≠ 1 / IR version ≠ 1 → fail
16
+ * - CMP002:render 树 shape(type/children)+ semantic 计数与渲染树交叉核对 + bindings shape
17
+ * - G-31.1:渲染树中 p-* 元素 semantic 必须 == TAG_SEMANTIC_MAP[type](语义链接机器验证)
18
+ */
19
+ export declare function runCompilerConformance(backend: ProteusCompilerBackend, fixture?: string): ConformanceResult;
@@ -0,0 +1,20 @@
1
+ export type CompilerBackendChoice = 'node' | 'rust';
2
+ /**
3
+ * ★双编译语义等价校验:同一 SFC → Node/Rust 双后端 → diff
4
+ * @param rustBin Rust CLI 可执行(bin/cli.js 路径;null → skipped)
5
+ * @param runRust 注入式 runner(默认 execFileSync node bin compile —— 测试注入 mock 驱动 mismatch/skipped)
6
+ */
7
+ export declare function verifyDualCompilerEquivalence(source: string, opts: {
8
+ rustBin: string | null;
9
+ filename?: string;
10
+ runRust?: (bin: string, sfc: string) => string;
11
+ }): {
12
+ status: 'ok' | 'mismatch' | 'skipped';
13
+ details: string[];
14
+ reason?: string;
15
+ };
16
+ /**
17
+ * ★定位 Rust CLI 可执行(bin/cli.js——npm bin 壳:release 优先/缺失自动 cargo build):
18
+ * ① env PROTEUS_CC_RUST 显式路径 → ② 工程/本包解析 @proteus-vue/compiler-backend-rust 包 → null(调用方降级)
19
+ */
20
+ export declare function resolveRustCliBin(projectRoot: string): string | null;
@@ -0,0 +1,20 @@
1
+ import type { G38CompilerBackend } from './g38';
2
+ export interface G38ConformanceResult {
3
+ id: string;
4
+ status: 'PASS' | 'FAIL' | 'SKIP';
5
+ error?: string;
6
+ }
7
+ export interface G38ConformanceSummary {
8
+ total: number;
9
+ pass: number;
10
+ fail: number;
11
+ skip: number;
12
+ results: G38ConformanceResult[];
13
+ }
14
+ export declare function createG38TerminalBackend(): G38CompilerBackend;
15
+ /** 跑全部 42 项(单后端;C-02 生命周期组依赖顺序执行——与 runner 同构) */
16
+ export declare function runG38Conformance(backend: G38CompilerBackend, opts?: {
17
+ only?: string;
18
+ }): Promise<G38ConformanceSummary>;
19
+ /** 文本报告(CLI 打印) */
20
+ export declare function formatG38Conformance(name: string, s: G38ConformanceSummary): string;
@@ -0,0 +1,29 @@
1
+ import type { G38CompilerBackend } from './g38';
2
+ export interface G38FallbackLog {
3
+ from: string;
4
+ to: string;
5
+ reason: string;
6
+ }
7
+ export interface G38FallbackOptions {
8
+ /** 首选后端 id('node' | 'rust' | 'go' | 'wasm' | 'bytecode') */
9
+ preferred: string;
10
+ /** 降级目标(规范 §6 固定 node——Node 参考实现恒可用) */
11
+ fallback?: 'node';
12
+ /** 加载器(注入可单测):返回后端实例;null/抛错 → 降级 */
13
+ load?: (id: string) => Promise<G38CompilerBackend | null>;
14
+ /** 降级事件监听(日志/指标——C-07-02 可观测) */
15
+ onFallback?: (log: G38FallbackLog) => void;
16
+ }
17
+ export interface G38FallbackResult {
18
+ /** 实际选用的后端(preferred 可用 → 它;否则 node 参考实现) */
19
+ backend: G38CompilerBackend;
20
+ /** 降级记录(未降级 → null) */
21
+ fallback: G38FallbackLog | null;
22
+ isDegraded: boolean;
23
+ }
24
+ /**
25
+ * ★createG38FallbackBackend:selectCompilerBackend(01 §6 / 03-implementation-guide)
26
+ * 用法:const { backend } = await createG38FallbackBackend({ preferred: 'rust' })
27
+ * → rust 不可用 → backend = node 参考实现 + fallback 日志(from:rust to:node)
28
+ */
29
+ export declare function createG38FallbackBackend(opts: G38FallbackOptions): Promise<G38FallbackResult>;
@@ -0,0 +1,11 @@
1
+ import type { G38CompilerBackend, G38IncrementalSession } from './g38';
2
+ export interface G38SessionOptions {
3
+ /** 会话 id(缺省 'incr') */
4
+ id?: string;
5
+ /** 内容提供者(recompute 重算脏文件时取内容;缺省 → 用 track 注入的内容) */
6
+ getContent?: (file: string) => string | null;
7
+ }
8
+ /** ★createG38IncrementalSession:真增量会话(依赖图 + 签名缓存 + 局部重算 + commit/rollback 快照) */
9
+ export declare function createG38IncrementalSession(backend: G38CompilerBackend, cacheDir: string, opts?: G38SessionOptions): G38IncrementalSession;
10
+ /** 便捷:解析 SFC script 块 import 依赖(相对/包裸名——module-plan B0 同款正则;模板无 import → []) */
11
+ export declare function scanSfcImports(source: string): string[];
package/dist/g38.d.ts ADDED
@@ -0,0 +1,115 @@
1
+ import type { ComponentIR } from '@proteus-vue/component-ir';
2
+ import type { G38SessionOptions } from './g38-session';
3
+ export interface G38CompilerCapabilities {
4
+ incremental: boolean;
5
+ aot: boolean;
6
+ sourceMap: boolean;
7
+ minify: boolean;
8
+ treeShake: boolean;
9
+ targetPlatforms: ('web' | 'ios' | 'android' | 'harmony' | 'flutter')[];
10
+ supportedLanguages: ('sfc' | 'tsx' | 'jsx' | 'vue')[];
11
+ backend: 'native' | 'wasm' | 'js';
12
+ deterministic: boolean;
13
+ }
14
+ export interface G38SourceFile {
15
+ path?: string;
16
+ content: string;
17
+ }
18
+ export interface G38SourceLoc {
19
+ line: number;
20
+ column: number;
21
+ }
22
+ export interface G38Diagnostic {
23
+ code: string;
24
+ message: string;
25
+ loc: G38SourceLoc;
26
+ severity?: 'error' | 'warning';
27
+ }
28
+ export interface G38ElementNode {
29
+ kind: 'element';
30
+ tag: string;
31
+ attributes: Record<string, unknown>;
32
+ children: G38ElementNode[];
33
+ loc: G38SourceLoc;
34
+ }
35
+ export interface G38ProgramIR {
36
+ nodes: G38ElementNode[];
37
+ diagnostics: G38Diagnostic[];
38
+ }
39
+ export interface G38ImportNode {
40
+ source: string;
41
+ imported: string;
42
+ }
43
+ export interface G38CapabilityNode {
44
+ name: string;
45
+ semantic: string;
46
+ }
47
+ export interface G38ModuleMetadata {
48
+ semanticCount: number;
49
+ compatCount: number;
50
+ componentCount: number;
51
+ }
52
+ export interface G38IRModule {
53
+ readonly id: string;
54
+ readonly imports: G38ImportNode[];
55
+ /** ★语义组件树(ComponentIR——G-31 C-IR 同构,直接交 G-37 RenderBackend 消费) */
56
+ readonly components: ComponentIR[];
57
+ readonly capabilities: G38CapabilityNode[];
58
+ readonly metadata: G38ModuleMetadata;
59
+ }
60
+ export interface G38CompiledArtifact {
61
+ code: string;
62
+ map: unknown;
63
+ hash: string;
64
+ }
65
+ export interface G38IRModuleDiff {
66
+ changed: string[];
67
+ removed: string[];
68
+ added: string[];
69
+ affectedFiles: string[];
70
+ }
71
+ export interface G38IncrementalSession {
72
+ readonly id: string;
73
+ invalidate(file: string): void;
74
+ invalidateAll(): void;
75
+ recompute(): G38IRModuleDiff;
76
+ getDependencies(file: string): string[];
77
+ getDependents(file: string): string[];
78
+ commit(): void;
79
+ rollback(): void;
80
+ getStats(): Record<string, unknown>;
81
+ dispose(): void;
82
+ /** ★宿主驱动扩展(04-incremental-compilation:首次全量构建逐文件注册——非规范必需方法) */
83
+ track?(file: string, content: string, deps?: string[]): void;
84
+ }
85
+ export interface G38CompilerContext {
86
+ cacheDir?: string;
87
+ }
88
+ export interface G38ParseContext {
89
+ filename?: string;
90
+ }
91
+ export interface G38TransformContext {
92
+ filename?: string;
93
+ }
94
+ export interface G38EmitContext {
95
+ format?: 'bundle' | 'ir-json' | 'list';
96
+ }
97
+ /** ★G-38 编译后端 SPI(01 §2.1 同形)——任何合规后端必须实现全部方法 */
98
+ export interface G38CompilerBackend {
99
+ readonly id: string;
100
+ readonly version: string;
101
+ readonly capabilities: G38CompilerCapabilities;
102
+ initialize(ctx?: G38CompilerContext): Promise<void>;
103
+ dispose(): void;
104
+ parse(source: G38SourceFile, ctx?: G38ParseContext): G38ProgramIR;
105
+ transform(ast: G38ProgramIR, ctx?: G38TransformContext): G38IRModule;
106
+ emit(module: G38IRModule, ctx?: G38EmitContext): G38CompiledArtifact;
107
+ createIncrementalSession(cacheDir: string, opts?: G38SessionOptions): G38IncrementalSession;
108
+ reportDiagnostics(module: G38IRModule): G38Diagnostic[];
109
+ getCacheKey(input: G38SourceFile): string;
110
+ getArtifactHash(artifact: G38CompiledArtifact): string;
111
+ }
112
+ /** djb2 哈希(与 conformance-runner.js 同算法——产物 hash 可交叉比对) */
113
+ export declare function g38Hash(s: string): string;
114
+ /** ★createG38NodeBackend:Node 参考实现(G-38 01 §2.1 全部 16 方法) */
115
+ export declare function createG38NodeBackend(): G38CompilerBackend;
@@ -0,0 +1,14 @@
1
+ export type { ProteusCompilerBackend, CompilerCapabilities, CompilerIR, RenderIR, RenderNode, SemanticIR, LayoutConstraintIR, BindingIR, SFCSource, SourceLoc, TemplateAST, TemplateNode, TemplateNodeType, FileChange, UpdatePayload, SourceMap, } from './spi';
2
+ export { runCompilerConformance, DEFAULT_CONFORMANCE_SFC } from './conformance';
3
+ export type { ConformanceCheck, ConformanceResult } from './conformance';
4
+ export { createNodeCompilerBackend } from './node';
5
+ export { verifyDualCompilerEquivalence, resolveRustCliBin } from './dual-check';
6
+ export type { CompilerBackendChoice } from './dual-check';
7
+ export { createG38NodeBackend, g38Hash } from './g38';
8
+ export type { G38CompilerBackend, G38CompilerCapabilities, G38SourceFile, G38SourceLoc, G38Diagnostic, G38ElementNode, G38ProgramIR, G38ImportNode, G38CapabilityNode, G38ModuleMetadata, G38IRModule, G38CompiledArtifact, G38IRModuleDiff, G38IncrementalSession, G38CompilerContext, G38ParseContext, G38TransformContext, G38EmitContext, } from './g38';
9
+ export { createG38FallbackBackend } from './g38-fallback';
10
+ export type { G38FallbackOptions, G38FallbackResult, G38FallbackLog } from './g38-fallback';
11
+ export { createG38IncrementalSession, scanSfcImports } from './g38-session';
12
+ export type { G38SessionOptions } from './g38-session';
13
+ export { createG38TerminalBackend, runG38Conformance, formatG38Conformance } from './g38-conformance';
14
+ export type { G38ConformanceResult, G38ConformanceSummary } from './g38-conformance';