@proteus-vue/compiler 0.3.0-beta.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 +74 -0
- package/dist/explain.d.ts +25 -0
- package/dist/explain.js +59 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +3396 -0
- package/dist/overrides.d.ts +14 -0
- package/dist/overrides.js +34 -0
- package/dist/script.d.ts +15 -0
- package/dist/script.js +318 -0
- package/dist/style-safety/constants.d.ts +6 -0
- package/dist/style-safety/index.d.ts +25 -0
- package/dist/style-safety/index.js +276 -0
- package/dist/style-safety/reachability.d.ts +8 -0
- package/dist/style.d.ts +3 -0
- package/dist/style.js +96 -0
- package/dist/tags.d.ts +3 -0
- package/dist/tags.js +49 -0
- package/dist/template.d.ts +3 -0
- package/dist/template.js +337 -0
- package/dist/trace.d.ts +6 -0
- package/dist/trace.js +18 -0
- package/dist/transforms/registry.d.ts +18 -0
- package/dist/transforms/registry.js +48 -0
- package/dist/transforms/script.d.ts +2 -0
- package/dist/transforms/script.js +179 -0
- package/dist/transforms/style.d.ts +2 -0
- package/dist/transforms/style.js +74 -0
- package/dist/transforms/template.d.ts +3 -0
- package/dist/transforms/template.js +371 -0
- package/dist/transforms/types.d.ts +50 -0
- package/dist/transforms/types.js +7 -0
- package/dist/transforms/validate.d.ts +2 -0
- package/dist/transforms/validate.js +50 -0
- package/dist/types.d.ts +1 -0
- package/dist/types.js +4 -0
- package/dist/validate.d.ts +18 -0
- package/dist/validate.js +54 -0
- package/package.json +42 -0
package/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# @proteus-vue/compiler —— 编译引擎独立包
|
|
2
|
+
|
|
3
|
+
> Proteus 的**编译引擎**,v0.2 起独立为 monorepo 包(`packages/compiler/`)。
|
|
4
|
+
> 设计目标:**零 Vite 依赖、零项目配置依赖、纯函数、选项全入参**——可独立分发、独立测试、被任何构建器消费。
|
|
5
|
+
|
|
6
|
+
## 模块边界(必须遵守)
|
|
7
|
+
|
|
8
|
+
| 允许 | 禁止 |
|
|
9
|
+
|---|---|
|
|
10
|
+
| import `@vue/compiler-sfc` / `@vue/compiler-dom`(编译期 AST 处理) | import `vite` 或任何 Vite 相关 API |
|
|
11
|
+
| import 同包 `./src` 内部模块 | import `proteus.config.ts` 或读取项目配置 |
|
|
12
|
+
| 选项全部通过函数入参传入 | 读写文件系统 / 网络(纯函数) |
|
|
13
|
+
|
|
14
|
+
- 所有转换函数是**纯函数**:相同输入 → 相同输出,可独立单测
|
|
15
|
+
- 编译期警告通过返回值 `warnings` 透出(不直接抛错,除非不可恢复)
|
|
16
|
+
|
|
17
|
+
## 公开 API
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
// 整包编译(推荐入口)
|
|
21
|
+
compileVueSfc(source: string, options: CompileOptions): CompileResult
|
|
22
|
+
// CompileResult = { wxml, js, wxss, warnings, trace }
|
|
23
|
+
|
|
24
|
+
// 分步转换(精细控制 / 单测)
|
|
25
|
+
transformTemplateToWxml(templateSource, styleOpts): { wxml, vModelBindings, warnings }
|
|
26
|
+
transformScriptToPage(scriptSource, styleOpts, { file, isComponent, vModelBindings }): { js, warnings }
|
|
27
|
+
transformStyleToWxss(styleSource, styleOpts): string
|
|
28
|
+
|
|
29
|
+
// ★ AI-native 透明:编译规则注册表(69 条规则,每条一份 AI 说明书)
|
|
30
|
+
listTransformRules(phase?) // 枚举规则(能力清单)
|
|
31
|
+
getTransformRule(id) // 查单条规则
|
|
32
|
+
formatTransformRule(rule) // 渲染单条 AI 说明书
|
|
33
|
+
formatTransformCatalog() // 渲染全量目录
|
|
34
|
+
|
|
35
|
+
// ★ 阶段三分派层(底线循环 ① 完全形态):AI 覆盖规则 apply 即获得新能力
|
|
36
|
+
// (implemented 规则可携带 apply();style/px-to-rpx、template/scope-attr 已登记示范)
|
|
37
|
+
executeRule(id, ctx) // 按规则 ID 执行(RuleContext:input → output)
|
|
38
|
+
|
|
39
|
+
// ★ 决策 trace(底线循环 ②):源码 → 实际触发的全部规则
|
|
40
|
+
explainTransform(source, options?) // → { events: [{ ruleId, phase, line, before, after }] }
|
|
41
|
+
formatTransformTrace(result) // 渲染为按阶段分组的可读文本
|
|
42
|
+
|
|
43
|
+
// 规则覆盖(底线循环 ①③):AI / config 改写或禁用规则
|
|
44
|
+
// (TransformRuleOverrides:disabled / mapping / customTags,经 CompileOptions.rules 传入)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## 产物契约
|
|
48
|
+
|
|
49
|
+
- `.wxml`:标准标签 → 小程序标签(`div→view` / `span,p,h1-h6→text` / `img→image` …)+ 指令映射
|
|
50
|
+
- `.js`:`Page({ data, methods, onReady, onUnload, onLoad, proteusOnXInput, proteusNavigateTo })`(组件为 `Component()`)
|
|
51
|
+
- `.wxss`:px → rpx、Skyline 不支持属性编译期警告、语义基础样式注入
|
|
52
|
+
- `.json`:**不属于本引擎**,由 `scripts/gen-routes.ts`(框架路由生成器)负责
|
|
53
|
+
|
|
54
|
+
## 消费方
|
|
55
|
+
|
|
56
|
+
- `vite-plugin-mp-transform.ts`(monorepo 根,`@proteus-vue/plugin-vite` 前身):薄适配层——扫描页面、传选项(含 `rules` 覆盖)、`emitFile` 产物
|
|
57
|
+
- `tests/*.test.ts`:直接 import `../packages/compiler/src` 调纯函数(vitest,无 Vite 依赖)
|
|
58
|
+
- 未来 CLI `@proteus-vue/cli`:`proteus build` / `proteus explain` 核心即调 `compileVueSfc` + `explainTransform`
|
|
59
|
+
|
|
60
|
+
## 构建与发布
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
cd packages/compiler
|
|
64
|
+
npm run build # tsc -p tsconfig.build.json → dist/(ESM + .d.ts)
|
|
65
|
+
npm publish # 发布 @proteus-vue/compiler(main/types/exports 指向 dist)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
发布后适配层可改为 `import { compileVueSfc } from '@proteus-vue/compiler'`(npm workspace 链接或直接依赖),
|
|
69
|
+
`proteus.config.ts` 的 `style` / `rules` 选项由适配层读取后传入——编译引擎 API 不变。
|
|
70
|
+
|
|
71
|
+
## MVP 限制(逐步补充边缘情况)
|
|
72
|
+
|
|
73
|
+
computed 读路径(v0.3 已支持:`computed(() => 表达式)` → data 派生 + setData 合并重算)/ watch / 跨模块引用、复杂事件表达式、`:class` 数组语法、
|
|
74
|
+
方法体对 setup ref 的复合赋值、`<template v-slot>` —— 均编译期警告或忽略。
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { TransformTraceEvent } from './trace';
|
|
2
|
+
import type { TransformRuleOverrides } from './types';
|
|
3
|
+
export interface ExplainOptions {
|
|
4
|
+
/** 源文件名(写入 trace 注释) */
|
|
5
|
+
filename?: string;
|
|
6
|
+
/** 组件模式(Component() vs Page()) */
|
|
7
|
+
isComponent?: boolean;
|
|
8
|
+
/** 是否 px → rpx(默认 true) */
|
|
9
|
+
px2rpx?: boolean;
|
|
10
|
+
/** px→rpx 比例(默认 2) */
|
|
11
|
+
rpxRatio?: number;
|
|
12
|
+
/** 是否启用行号注释(默认 false) */
|
|
13
|
+
annotateLines?: boolean;
|
|
14
|
+
/** 规则覆盖(可选:★底线循环 ①③,与 proteus.config.ts rules 同构) */
|
|
15
|
+
rules?: TransformRuleOverrides;
|
|
16
|
+
}
|
|
17
|
+
export interface ExplainResult {
|
|
18
|
+
filename?: string;
|
|
19
|
+
/** 全部决策事件(template → script → style 按阶段顺序) */
|
|
20
|
+
events: TransformTraceEvent[];
|
|
21
|
+
}
|
|
22
|
+
/** 对一份 Vue SFC 源码执行全链路转换并收集决策 trace */
|
|
23
|
+
export declare function explainTransform(source: string, options?: ExplainOptions): ExplainResult;
|
|
24
|
+
/** 渲染决策 trace 为人类可读文本(按阶段分组 + 行号) */
|
|
25
|
+
export declare function formatTransformTrace(result: ExplainResult): string;
|
package/dist/explain.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// src/compiler/explain.ts
|
|
2
|
+
// explainTransform —— 阶段二核心:对一份 Vue SFC 输出它实际触发的全部转换规则(决策 trace)
|
|
3
|
+
// AI 用法:改完编译器 / 写完页面后,跑 explainTransform 看转换链路,理解"为什么产物是这样"。
|
|
4
|
+
import { parse as sfcParse } from '@vue/compiler-sfc';
|
|
5
|
+
import { transformTemplateToWxml } from './template';
|
|
6
|
+
import { transformScriptToPage } from './script';
|
|
7
|
+
import { transformStyleToWxss } from './style';
|
|
8
|
+
import { createTrace } from './trace';
|
|
9
|
+
/** 对一份 Vue SFC 源码执行全链路转换并收集决策 trace */
|
|
10
|
+
export function explainTransform(source, options = {}) {
|
|
11
|
+
const { descriptor } = sfcParse(source, { filename: options.filename ?? 'anonymous.vue' });
|
|
12
|
+
const styleOpts = { px2rpx: options.px2rpx ?? true, rpxRatio: options.rpxRatio ?? 2 };
|
|
13
|
+
const events = [];
|
|
14
|
+
// template 阶段(vModelBindings / usesNavigate 传给 script 阶段)
|
|
15
|
+
const tplTrace = createTrace('template');
|
|
16
|
+
const tpl = descriptor.template?.content ?? '';
|
|
17
|
+
const tplResult = transformTemplateToWxml(tpl, {
|
|
18
|
+
...styleOpts,
|
|
19
|
+
filename: options.filename,
|
|
20
|
+
annotateLines: options.annotateLines,
|
|
21
|
+
rules: options.rules,
|
|
22
|
+
trace: tplTrace,
|
|
23
|
+
});
|
|
24
|
+
events.push(...tplTrace.events);
|
|
25
|
+
// script 阶段
|
|
26
|
+
const setup = descriptor.scriptSetup?.content ?? descriptor.script?.content ?? '';
|
|
27
|
+
const scriptTrace = createTrace('script');
|
|
28
|
+
transformScriptToPage(setup, styleOpts, {
|
|
29
|
+
file: options.filename,
|
|
30
|
+
isComponent: options.isComponent,
|
|
31
|
+
vModelBindings: tplResult.vModelBindings,
|
|
32
|
+
usesNavigate: tplResult.usesNavigate,
|
|
33
|
+
rules: options.rules,
|
|
34
|
+
trace: scriptTrace,
|
|
35
|
+
});
|
|
36
|
+
events.push(...scriptTrace.events);
|
|
37
|
+
// style 阶段
|
|
38
|
+
const styleTrace = createTrace('style');
|
|
39
|
+
transformStyleToWxss(descriptor.styles.map((s) => s.content).join('\n'), { ...styleOpts, rules: options.rules, trace: styleTrace });
|
|
40
|
+
events.push(...styleTrace.events);
|
|
41
|
+
return { filename: options.filename, events };
|
|
42
|
+
}
|
|
43
|
+
/** 渲染决策 trace 为人类可读文本(按阶段分组 + 行号) */
|
|
44
|
+
export function formatTransformTrace(result) {
|
|
45
|
+
const phases = ['template', 'script', 'style', 'validate'];
|
|
46
|
+
const blocks = [];
|
|
47
|
+
for (const phase of phases) {
|
|
48
|
+
const evts = result.events.filter((e) => e.phase === phase);
|
|
49
|
+
if (!evts.length)
|
|
50
|
+
continue;
|
|
51
|
+
const lines = evts.map((e) => {
|
|
52
|
+
const line = e.line != null ? `L${e.line} ` : '';
|
|
53
|
+
const detail = [e.before, e.after].filter(Boolean).join(' → ');
|
|
54
|
+
return ` ${line}${e.ruleId}${detail ? `:${detail}` : ''}`;
|
|
55
|
+
});
|
|
56
|
+
blocks.push(`### ${phase} 阶段(${evts.length} 个决策)\n${lines.join('\n')}`);
|
|
57
|
+
}
|
|
58
|
+
return `# explainTransform ${result.filename ?? ''}\n\n${blocks.join('\n\n')}`;
|
|
59
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { CompileOptions, CompileResult } from './types';
|
|
2
|
+
/** djb2 哈希 → scoped 属性名(稳定:同文件同 scopeId;零依赖纯函数) */
|
|
3
|
+
export declare function scopedIdFrom(filename: string): string;
|
|
4
|
+
export type { CompileOptions, CompileResult, StyleTransformOptions, TemplateTransformOptions, TemplateTransformResult, ScriptTransformOptions, ScriptTransformResult, } from './types';
|
|
5
|
+
export { transformTemplateToWxml } from './template';
|
|
6
|
+
export { transformScriptToPage } from './script';
|
|
7
|
+
export { transformStyleToWxss } from './style';
|
|
8
|
+
export { validateJs, validateWxml, CompilerError } from './validate';
|
|
9
|
+
export { listTransformRules, getTransformRule, formatTransformRule, formatTransformCatalog } from './transforms/registry';
|
|
10
|
+
export type { TransformRule, TransformPhase, RuleStatus } from './transforms/types';
|
|
11
|
+
export type { TransformRuleOverrides } from './types';
|
|
12
|
+
export { explainTransform, formatTransformTrace } from './explain';
|
|
13
|
+
export type { ExplainOptions, ExplainResult } from './explain';
|
|
14
|
+
export { createTrace, lineAt } from './trace';
|
|
15
|
+
export type { TransformTrace, TransformTraceEvent } from './trace';
|
|
16
|
+
/** 整包编译:标准 Vue SFC 源码 → { wxml, js, wxss }(.json 由路由生成器负责) */
|
|
17
|
+
export declare function compileVueSfc(source: string, options?: CompileOptions): CompileResult;
|