soma-core 1.0.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/app/api/page/route.ts +97 -0
- package/app/globals.css +3 -0
- package/app/layout.tsx +18 -0
- package/app/page.tsx +129 -0
- package/bin/soma.cjs +2 -0
- package/core/base/SomaApi.ts +14 -0
- package/core/base/SomaDict.ts +168 -0
- package/core/base/SomaEntity.ts +41 -0
- package/core/base/SomaHttpClient.ts +126 -0
- package/core/base/SomaLocaleOnlyPageRoot.ts +4 -0
- package/core/base/SomaPageRootBase.ts +54 -0
- package/core/base/SomaValidator.ts +147 -0
- package/core/base/index.ts +7 -0
- package/core/compiler/full-manifest-builder.ts +43 -0
- package/core/compiler/manifest-compiler.ts +237 -0
- package/core/compiler/route-builder.ts +31 -0
- package/core/compiler/soma-precompile.ts +48 -0
- package/core/compiler/soma-webpack-loader.js +35 -0
- package/core/compiler/vite-plugin-soma.ts +73 -0
- package/core/container/SomaContainerAdapter.ts +169 -0
- package/core/diagnostics/SomaDiagnostics.ts +186 -0
- package/core/engine/asset-resolver.ts +67 -0
- package/core/engine/dom-renderer.ts +366 -0
- package/core/engine/event-engine.ts +287 -0
- package/core/engine/lis.ts +60 -0
- package/core/engine/reactive-proxy.ts +225 -0
- package/core/engine/render-engine.ts +264 -0
- package/core/engine/runtime-builder.ts +100 -0
- package/core/engine/scope-resolver.ts +252 -0
- package/core/engine/soma-bootstrap.ts +244 -0
- package/core/index.ts +6 -0
- package/core/infra/auth/IAuthService.ts +13 -0
- package/core/infra/auth/TokenAuthService.ts +98 -0
- package/core/infra/cache/ICache.ts +13 -0
- package/core/infra/cache/MemoryCache.ts +95 -0
- package/core/infra/cache/ServerFileCache.ts +123 -0
- package/core/infra/config/IConfig.ts +9 -0
- package/core/infra/config/SomaConfig.ts +85 -0
- package/core/infra/event-bus/IEventBus.ts +9 -0
- package/core/infra/event-bus/SomaEventBus.ts +53 -0
- package/core/infra/http/IHttpClient.ts +21 -0
- package/core/infra/http/SomaHttpService.ts +141 -0
- package/core/infra/i18n/DefaultI18n.ts +41 -0
- package/core/infra/i18n/II18n.ts +7 -0
- package/core/infra/logger/ConsoleLogger.ts +81 -0
- package/core/infra/logger/ILogger.ts +19 -0
- package/core/infra/notify/DefaultNotifyService.ts +31 -0
- package/core/infra/notify/INotifyService.ts +14 -0
- package/core/infra/permission/IPermission.ts +10 -0
- package/core/infra/permission/SomaPermission.ts +48 -0
- package/core/infra/router/IRouter.ts +19 -0
- package/core/infra/router/SomaRouter.ts +66 -0
- package/core/infra/storage/IStorageService.ts +7 -0
- package/core/infra/storage/MemoryStorageService.ts +53 -0
- package/core/infra/timer/ITimer.ts +9 -0
- package/core/infra/timer/SomaTimer.ts +118 -0
- package/core/middleware/SomaMiddlewareChain.ts +29 -0
- package/core/parser/pipeline-parser.ts +276 -0
- package/core/parser/ui-config-parser.ts +336 -0
- package/core/parser/yaml-loader.ts +36 -0
- package/core/router/RouteHandler.ts +93 -0
- package/core/runtime/SomaSnapshotStore.ts +98 -0
- package/core/scheduler/SomaScheduler.ts +190 -0
- package/core/types/domain.types.ts +88 -0
- package/core/types/engine.types.ts +35 -0
- package/core/types/event.types.ts +57 -0
- package/core/types/keyboard.types.ts +18 -0
- package/core/types/manifest.types.ts +30 -0
- package/core/types/scheduler.types.ts +10 -0
- package/core/types/ui-config.types.ts +89 -0
- package/core/validation/contract-validator.ts +465 -0
- package/dist/cli/soma.js +49 -0
- package/global.d.ts +14 -0
- package/next.config.js +61 -0
- package/package.json +77 -0
- package/postcss.config.js +6 -0
- package/tailwind.config.ts +21 -0
- package/tsconfig.json +35 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// core/base/SomaValidator.ts — 数据校验
|
|
2
|
+
// 声明式的字段校验器,支持链式 addRule 和批量 validate
|
|
3
|
+
// 不依赖任何 DOM API,可在 Node.js 环境下独立运行
|
|
4
|
+
|
|
5
|
+
import type { ValidationRule, ValidationResult } from '../types/domain.types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* SomaValidator — 数据校验器
|
|
9
|
+
*
|
|
10
|
+
* 提供以下能力:
|
|
11
|
+
* - 链式 addRule:为字段添加校验规则
|
|
12
|
+
* - validate:批量校验实体对象,返回结构化的校验结果
|
|
13
|
+
* - 支持的内置校验:required / minLength / maxLength / min / max / pattern
|
|
14
|
+
* - 支持自定义校验函数(custom)
|
|
15
|
+
*
|
|
16
|
+
* 使用示例:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const validator = new SomaValidator()
|
|
19
|
+
* .addRule('name', { required: true, message: '名称不能为空' })
|
|
20
|
+
* .addRule('value', { min: 0, max: 1000, message: '数值必须在0-1000之间' });
|
|
21
|
+
*
|
|
22
|
+
* const result = validator.validate({ name: '', value: 2000 });
|
|
23
|
+
* // result.valid === false
|
|
24
|
+
* // result.errors === { name: ['名称不能为空'], value: ['数值必须在0-1000之间'] }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export class SomaValidator {
|
|
28
|
+
/** 字段 -> 校验规则列表 的映射 */
|
|
29
|
+
private rules: Map<string, ValidationRule[]> = new Map();
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 为字段添加一条或多条校验规则
|
|
33
|
+
* 支持链式调用
|
|
34
|
+
*
|
|
35
|
+
* @param field 字段名
|
|
36
|
+
* @param rules 校验规则(可传多个)
|
|
37
|
+
* @returns this(支持链式调用)
|
|
38
|
+
*/
|
|
39
|
+
addRule(field: string, ...rules: ValidationRule[]): this {
|
|
40
|
+
const existing = this.rules.get(field) || [];
|
|
41
|
+
this.rules.set(field, [...existing, ...rules]);
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 校验实体对象
|
|
47
|
+
* 遍历所有已注册的字段规则,逐条检查
|
|
48
|
+
*
|
|
49
|
+
* @param entity 待校验的对象
|
|
50
|
+
* @returns 校验结果,包含 valid 标志和各字段的错误消息
|
|
51
|
+
*/
|
|
52
|
+
validate(entity: Record<string, any>): ValidationResult {
|
|
53
|
+
const errors: Record<string, string[]> = {};
|
|
54
|
+
|
|
55
|
+
for (const [field, rules] of this.rules) {
|
|
56
|
+
const value = entity[field];
|
|
57
|
+
const fieldErrors: string[] = [];
|
|
58
|
+
|
|
59
|
+
for (const rule of rules) {
|
|
60
|
+
// 必填校验
|
|
61
|
+
if (rule.required && !value && value !== 0) {
|
|
62
|
+
fieldErrors.push(rule.message);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 最小长度校验(字符串)
|
|
66
|
+
if (
|
|
67
|
+
rule.minLength !== undefined &&
|
|
68
|
+
typeof value === 'string' &&
|
|
69
|
+
value.length < rule.minLength
|
|
70
|
+
) {
|
|
71
|
+
fieldErrors.push(rule.message);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// 最大长度校验(字符串)
|
|
75
|
+
if (
|
|
76
|
+
rule.maxLength !== undefined &&
|
|
77
|
+
typeof value === 'string' &&
|
|
78
|
+
value.length > rule.maxLength
|
|
79
|
+
) {
|
|
80
|
+
fieldErrors.push(rule.message);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 最小值校验(数字)
|
|
84
|
+
if (
|
|
85
|
+
rule.min !== undefined &&
|
|
86
|
+
typeof value === 'number' &&
|
|
87
|
+
value < rule.min
|
|
88
|
+
) {
|
|
89
|
+
fieldErrors.push(rule.message);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 最大值校验(数字)
|
|
93
|
+
if (
|
|
94
|
+
rule.max !== undefined &&
|
|
95
|
+
typeof value === 'number' &&
|
|
96
|
+
value > rule.max
|
|
97
|
+
) {
|
|
98
|
+
fieldErrors.push(rule.message);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// 正则表达式校验(字符串)
|
|
102
|
+
if (
|
|
103
|
+
rule.pattern &&
|
|
104
|
+
typeof value === 'string' &&
|
|
105
|
+
!rule.pattern.test(value)
|
|
106
|
+
) {
|
|
107
|
+
fieldErrors.push(rule.message);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 自定义校验函数
|
|
111
|
+
if (rule.custom && !rule.custom(value)) {
|
|
112
|
+
fieldErrors.push(rule.message);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (fieldErrors.length > 0) {
|
|
117
|
+
errors[field] = fieldErrors;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
valid: Object.keys(errors).length === 0,
|
|
123
|
+
errors,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 移除指定字段的所有校验规则
|
|
129
|
+
*
|
|
130
|
+
* @param field 字段名
|
|
131
|
+
* @returns this(支持链式调用)
|
|
132
|
+
*/
|
|
133
|
+
removeRules(field: string): this {
|
|
134
|
+
this.rules.delete(field);
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* 清空所有校验规则
|
|
140
|
+
*
|
|
141
|
+
* @returns this(支持链式调用)
|
|
142
|
+
*/
|
|
143
|
+
clearRules(): this {
|
|
144
|
+
this.rules.clear();
|
|
145
|
+
return this;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// core/compiler/full-manifest-builder.ts
|
|
2
|
+
// 职责:全量预编译项目所有声明式配置
|
|
3
|
+
|
|
4
|
+
import type { UiConfig } from '../types/ui-config.types';
|
|
5
|
+
import type { ParsedEvent } from '../types/event.types';
|
|
6
|
+
import { compileUiConfig, compileEventConfig, precompileYaml } from './soma-precompile';
|
|
7
|
+
import { buildRouteTable, type SomaRouteEntry } from './route-builder';
|
|
8
|
+
import yaml from 'js-yaml';
|
|
9
|
+
|
|
10
|
+
export interface SomaPrecompiledPage {
|
|
11
|
+
path: string;
|
|
12
|
+
uiConfig: UiConfig;
|
|
13
|
+
eventConfig: ParsedEvent[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface SomaPrecompiledManifest {
|
|
17
|
+
routes: SomaRouteEntry[];
|
|
18
|
+
locale: Record<string, unknown>;
|
|
19
|
+
appConfig: unknown;
|
|
20
|
+
pages: SomaPrecompiledPage[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 全量预编译整个项目的所有声明式配置。
|
|
25
|
+
* 构建产物是一个 JS 模块,运行时 import 即可使用,零解析。
|
|
26
|
+
*/
|
|
27
|
+
export function precompileAll(inputs: {
|
|
28
|
+
htmlFiles: string[];
|
|
29
|
+
localeYaml: string;
|
|
30
|
+
appConfigYaml: string;
|
|
31
|
+
pages: Array<{ path: string; uiYaml: string; eventYaml: string }>;
|
|
32
|
+
}): SomaPrecompiledManifest {
|
|
33
|
+
return {
|
|
34
|
+
routes: buildRouteTable(inputs.htmlFiles),
|
|
35
|
+
locale: (yaml.load(inputs.localeYaml) as Record<string, unknown>) || {},
|
|
36
|
+
appConfig: yaml.load(inputs.appConfigYaml) ?? {},
|
|
37
|
+
pages: inputs.pages.map(p => ({
|
|
38
|
+
path: p.path,
|
|
39
|
+
uiConfig: compileUiConfig(p.uiYaml),
|
|
40
|
+
eventConfig: compileEventConfig(p.eventYaml),
|
|
41
|
+
})),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import type { SomaContainerAdapter } from '../container/SomaContainerAdapter';
|
|
2
|
+
import type { SomaScopeResolver } from '../engine/scope-resolver';
|
|
3
|
+
import type { BindingType } from '../types/engine.types';
|
|
4
|
+
import type {
|
|
5
|
+
SomaCompiledBinding,
|
|
6
|
+
SomaCompiledEvent,
|
|
7
|
+
SomaRuntimeManifest,
|
|
8
|
+
} from '../types/manifest.types';
|
|
9
|
+
import type {
|
|
10
|
+
ExtendedShowValue,
|
|
11
|
+
NestedShowMapping,
|
|
12
|
+
ShowMapping,
|
|
13
|
+
UiConfig,
|
|
14
|
+
} from '../types/ui-config.types';
|
|
15
|
+
import type { ParsedEvent } from '../types/event.types';
|
|
16
|
+
|
|
17
|
+
export interface SomaManifestCompilerInput {
|
|
18
|
+
container: HTMLElement;
|
|
19
|
+
adapter: SomaContainerAdapter;
|
|
20
|
+
uiConfig: UiConfig;
|
|
21
|
+
eventConfig: ParsedEvent[];
|
|
22
|
+
resolver: SomaScopeResolver;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class SomaManifestCompiler {
|
|
26
|
+
/** 事件编译缓存:eventConfig 不依赖 DOM,可安全缓存 */
|
|
27
|
+
private eventCache = new Map<string, { events: SomaCompiledEvent[]; eventIndex: Map<string, SomaCompiledEvent> }>();
|
|
28
|
+
|
|
29
|
+
compile(input: SomaManifestCompilerInput): SomaRuntimeManifest {
|
|
30
|
+
const bindings: SomaCompiledBinding[] = [];
|
|
31
|
+
this.walkShow(input.uiConfig.show, input, bindings);
|
|
32
|
+
|
|
33
|
+
const bindingIndex = new Map<string, SomaCompiledBinding[]>();
|
|
34
|
+
for (const binding of bindings) {
|
|
35
|
+
for (const path of binding.watchPaths) {
|
|
36
|
+
const existing = bindingIndex.get(path);
|
|
37
|
+
if (existing) {
|
|
38
|
+
existing.push(binding);
|
|
39
|
+
} else {
|
|
40
|
+
bindingIndex.set(path, [binding]);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 事件编译缓存:eventConfig 纯数据转换,不依赖 DOM
|
|
46
|
+
const eventKey = JSON.stringify(input.eventConfig);
|
|
47
|
+
let eventResult = this.eventCache.get(eventKey);
|
|
48
|
+
if (!eventResult) {
|
|
49
|
+
const events: SomaCompiledEvent[] = input.eventConfig.map((event) => ({
|
|
50
|
+
...event,
|
|
51
|
+
eventKey: `${event.domEvent}:${event.somaId}`,
|
|
52
|
+
priority: 'immediate' as const,
|
|
53
|
+
}));
|
|
54
|
+
const eventIndex = new Map<string, SomaCompiledEvent>();
|
|
55
|
+
for (const event of events) {
|
|
56
|
+
eventIndex.set(event.eventKey, event);
|
|
57
|
+
}
|
|
58
|
+
eventResult = { events, eventIndex };
|
|
59
|
+
this.eventCache.set(eventKey, eventResult);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
bindings,
|
|
64
|
+
bindingIndex,
|
|
65
|
+
events: eventResult.events,
|
|
66
|
+
eventIndex: eventResult.eventIndex,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private walkShow(
|
|
71
|
+
showMapping: ShowMapping,
|
|
72
|
+
input: SomaManifestCompilerInput,
|
|
73
|
+
bindings: SomaCompiledBinding[],
|
|
74
|
+
): void {
|
|
75
|
+
for (const [rawKey, showValue] of Object.entries(showMapping)) {
|
|
76
|
+
if (this.isNestedShowMapping(showValue)) {
|
|
77
|
+
const element = input.adapter.findAnchor(input.container, rawKey);
|
|
78
|
+
const role = element ? input.adapter.readRole(element) : null;
|
|
79
|
+
const dataPath = input.resolver.resolvePath(showValue.bind.obj);
|
|
80
|
+
bindings.push({
|
|
81
|
+
id: `${rawKey}:${bindings.length}`,
|
|
82
|
+
somaId: rawKey,
|
|
83
|
+
type: role === 'item' ? 'list' : 'obj',
|
|
84
|
+
dataPath,
|
|
85
|
+
watchPaths: dataPath ? [dataPath] : [],
|
|
86
|
+
childShowConfig: showValue.show,
|
|
87
|
+
});
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const { somaId, attrName } = this.parseSomaKey(rawKey);
|
|
92
|
+
const element = input.adapter.findAnchor(input.container, somaId);
|
|
93
|
+
|
|
94
|
+
if (this.isExtendedShowValue(showValue)) {
|
|
95
|
+
this.compileExtendedValue(somaId, showValue, input, bindings);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const dataPath = input.resolver.resolvePath(String(showValue));
|
|
100
|
+
const bindingType = this.resolveBindingType(element, attrName);
|
|
101
|
+
const isStatic = this.isStaticPath(dataPath);
|
|
102
|
+
bindings.push({
|
|
103
|
+
id: `${rawKey}:${bindings.length}`,
|
|
104
|
+
somaId,
|
|
105
|
+
type: bindingType,
|
|
106
|
+
dataPath,
|
|
107
|
+
watchPaths: isStatic ? [] : (dataPath ? [dataPath] : []),
|
|
108
|
+
attrName: bindingType === 'attr' ? attrName ?? undefined : undefined,
|
|
109
|
+
static: isStatic || undefined,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private compileExtendedValue(
|
|
115
|
+
somaId: string,
|
|
116
|
+
value: ExtendedShowValue,
|
|
117
|
+
input: SomaManifestCompilerInput,
|
|
118
|
+
bindings: SomaCompiledBinding[],
|
|
119
|
+
): void {
|
|
120
|
+
if (value.text) {
|
|
121
|
+
const dataPath = input.resolver.resolvePath(value.text);
|
|
122
|
+
const isStatic = this.isStaticPath(dataPath);
|
|
123
|
+
bindings.push({
|
|
124
|
+
id: `${somaId}:text:${bindings.length}`,
|
|
125
|
+
somaId,
|
|
126
|
+
type: 'text',
|
|
127
|
+
dataPath,
|
|
128
|
+
watchPaths: isStatic ? [] : [dataPath],
|
|
129
|
+
static: isStatic || undefined,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (value.model) {
|
|
134
|
+
const dataPath = input.resolver.resolvePath(value.model);
|
|
135
|
+
bindings.push({
|
|
136
|
+
id: `${somaId}:model:${bindings.length}`,
|
|
137
|
+
somaId,
|
|
138
|
+
type: 'model',
|
|
139
|
+
dataPath,
|
|
140
|
+
watchPaths: [dataPath],
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (value.when) {
|
|
145
|
+
const { dataPath, negate } = this.parseConditionalPath(value.when, input.resolver);
|
|
146
|
+
bindings.push({
|
|
147
|
+
id: `${somaId}:when:${bindings.length}`,
|
|
148
|
+
somaId,
|
|
149
|
+
type: 'when',
|
|
150
|
+
dataPath,
|
|
151
|
+
watchPaths: [dataPath],
|
|
152
|
+
negate,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (value.class) {
|
|
157
|
+
const classRules = Object.fromEntries(
|
|
158
|
+
Object.entries(value.class).map(([className, path]) => [
|
|
159
|
+
className,
|
|
160
|
+
input.resolver.resolvePath(path),
|
|
161
|
+
]),
|
|
162
|
+
);
|
|
163
|
+
bindings.push({
|
|
164
|
+
id: `${somaId}:class:${bindings.length}`,
|
|
165
|
+
somaId,
|
|
166
|
+
type: 'class',
|
|
167
|
+
dataPath: '',
|
|
168
|
+
watchPaths: [...new Set(Object.values(classRules))],
|
|
169
|
+
classRules,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (value.show) {
|
|
174
|
+
this.walkShow(value.show, input, bindings);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private parseSomaKey(rawKey: string): { somaId: string; attrName: string | null } {
|
|
179
|
+
const [somaId, attrName] = rawKey.split('@');
|
|
180
|
+
return {
|
|
181
|
+
somaId,
|
|
182
|
+
attrName: attrName ?? null,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
private parseConditionalPath(
|
|
187
|
+
path: string,
|
|
188
|
+
resolver: SomaScopeResolver,
|
|
189
|
+
): { dataPath: string; negate: boolean } {
|
|
190
|
+
const negate = path.startsWith('!');
|
|
191
|
+
const rawPath = negate ? path.slice(1) : path;
|
|
192
|
+
return {
|
|
193
|
+
dataPath: resolver.resolvePath(rawPath),
|
|
194
|
+
negate,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private resolveBindingType(
|
|
199
|
+
element: HTMLElement | null,
|
|
200
|
+
attrName: string | null,
|
|
201
|
+
): BindingType {
|
|
202
|
+
if (attrName === 'html') {
|
|
203
|
+
return 'html';
|
|
204
|
+
}
|
|
205
|
+
if (attrName) {
|
|
206
|
+
return 'attr';
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const role =
|
|
210
|
+
element?.getAttribute('data-soma-role') ??
|
|
211
|
+
element?.querySelector('template')?.getAttribute('data-soma-role') ??
|
|
212
|
+
null;
|
|
213
|
+
if (role === 'item') {
|
|
214
|
+
return 'list';
|
|
215
|
+
}
|
|
216
|
+
if (role === 'obj' || role === 'div') {
|
|
217
|
+
return 'obj';
|
|
218
|
+
}
|
|
219
|
+
return 'text';
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 判断 dataPath 是否为静态(初始化后不会变化)。
|
|
224
|
+
* _locale.* 和 _config.* 在 mount 时设置一次,不受业务逻辑变更影响。
|
|
225
|
+
*/
|
|
226
|
+
private isStaticPath(dataPath: string): boolean {
|
|
227
|
+
return dataPath.startsWith('_locale.') || dataPath.startsWith('_config.');
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private isNestedShowMapping(value: unknown): value is NestedShowMapping {
|
|
231
|
+
return typeof value === 'object' && value !== null && 'bind' in value && 'show' in value;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private isExtendedShowValue(value: unknown): value is ExtendedShowValue {
|
|
235
|
+
return typeof value === 'object' && value !== null;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// core/compiler/route-builder.ts
|
|
2
|
+
// 职责:从目录结构生成路由表
|
|
3
|
+
|
|
4
|
+
export interface SomaRouteEntry {
|
|
5
|
+
path: string;
|
|
6
|
+
htmlFile: string;
|
|
7
|
+
uiFile: string;
|
|
8
|
+
eventFile: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 从 HTML 文件列表生成路由表。
|
|
13
|
+
* shared/ 和 system/ 目录下的文件被排除(非页面文件)。
|
|
14
|
+
*/
|
|
15
|
+
export function buildRouteTable(htmlFiles: string[]): SomaRouteEntry[] {
|
|
16
|
+
return htmlFiles
|
|
17
|
+
.filter(f => !f.startsWith('shared/') && !f.startsWith('system/'))
|
|
18
|
+
.map(htmlFile => {
|
|
19
|
+
const base = htmlFile.replace(/\.html$/, '');
|
|
20
|
+
return {
|
|
21
|
+
path: `/${base}`,
|
|
22
|
+
htmlFile,
|
|
23
|
+
uiFile: `${base}_ui.yaml`,
|
|
24
|
+
eventFile: `${base}_event.yaml`,
|
|
25
|
+
};
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function precompileRouteTable(htmlFiles: string[]): string {
|
|
30
|
+
return JSON.stringify(buildRouteTable(htmlFiles));
|
|
31
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// core/compiler/soma-precompile.ts
|
|
2
|
+
// 职责:YAML 配置文件的预编译(纯 YAML → JS 转换)
|
|
3
|
+
|
|
4
|
+
import yaml from 'js-yaml';
|
|
5
|
+
import { parseUiConfig } from '../parser/ui-config-parser';
|
|
6
|
+
import { parseEventConfig } from '../parser/pipeline-parser';
|
|
7
|
+
import type { UiConfig } from '../types/ui-config.types';
|
|
8
|
+
import type { ParsedEvent } from '../types/event.types';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 预编译 UI YAML → JSON 字符串
|
|
12
|
+
*/
|
|
13
|
+
export function precompileUiYaml(yamlStr: string): string {
|
|
14
|
+
const raw = yaml.load(yamlStr);
|
|
15
|
+
const uiConfig = parseUiConfig(raw);
|
|
16
|
+
return JSON.stringify(uiConfig);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 预编译 Event YAML → JSON 字符串
|
|
21
|
+
*/
|
|
22
|
+
export function precompileEventYaml(yamlStr: string): string {
|
|
23
|
+
const raw = yaml.load(yamlStr);
|
|
24
|
+
const events = parseEventConfig(raw as any);
|
|
25
|
+
return JSON.stringify(events);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 预编译 UI YAML → UiConfig 对象
|
|
30
|
+
*/
|
|
31
|
+
export function compileUiConfig(yamlStr: string): UiConfig {
|
|
32
|
+
return parseUiConfig(yaml.load(yamlStr));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 预编译 Event YAML → ParsedEvent[]
|
|
37
|
+
*/
|
|
38
|
+
export function compileEventConfig(yamlStr: string): ParsedEvent[] {
|
|
39
|
+
return parseEventConfig(yaml.load(yamlStr) as any);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 预编译任意 YAML → JSON 字符串(locale、app.config 等纯数据配置)
|
|
44
|
+
*/
|
|
45
|
+
export function precompileYaml(yamlStr: string): string {
|
|
46
|
+
const obj = yaml.load(yamlStr);
|
|
47
|
+
return JSON.stringify(obj ?? {});
|
|
48
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// core/compiler/soma-webpack-loader.js
|
|
2
|
+
// Webpack 预编译 loader:将所有声明式 YAML 配置编译为 JS 模块
|
|
3
|
+
// Next.js 使用 webpack,因此需要这个 loader 版本
|
|
4
|
+
|
|
5
|
+
/** @type {import('webpack').LoaderDefinitionFunction} */
|
|
6
|
+
module.exports = function somaWebpackLoader(source) {
|
|
7
|
+
const resourcePath = this.resourcePath;
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
const { precompileUiYaml, precompileEventYaml, precompileYaml } = require('./soma-precompile');
|
|
11
|
+
|
|
12
|
+
// SOMA UI 配置
|
|
13
|
+
if (resourcePath.endsWith('_ui.yaml')) {
|
|
14
|
+
const compiled = precompileUiYaml(source);
|
|
15
|
+
return `export default ${compiled};`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// SOMA Event 配置
|
|
19
|
+
if (resourcePath.endsWith('_event.yaml')) {
|
|
20
|
+
const compiled = precompileEventYaml(source);
|
|
21
|
+
return `export default ${compiled};`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// 通用 YAML(locale、app.config 等)
|
|
25
|
+
if (resourcePath.endsWith('.yaml') || resourcePath.endsWith('.yml')) {
|
|
26
|
+
const compiled = precompileYaml(source);
|
|
27
|
+
return `export default ${compiled};`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return source;
|
|
31
|
+
} catch (e) {
|
|
32
|
+
this.emitError(new Error(`[soma-webpack-loader] 预编译失败 (${resourcePath}): ${e.message}`));
|
|
33
|
+
return source;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// core/compiler/vite-plugin-soma.ts
|
|
2
|
+
// Vite 预编译插件:在构建阶段将所有声明式 YAML 配置编译为 JS 模块
|
|
3
|
+
|
|
4
|
+
import type { Plugin } from 'vite';
|
|
5
|
+
import { precompileUiYaml, precompileEventYaml, precompileYaml } from './soma-precompile';
|
|
6
|
+
|
|
7
|
+
export interface SomaPluginOptions {
|
|
8
|
+
/** UI 配置文件后缀,默认 _ui.yaml */
|
|
9
|
+
uiSuffix?: string;
|
|
10
|
+
/** 事件配置文件后缀,默认 _event.yaml */
|
|
11
|
+
eventSuffix?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* SOMA Vite 预编译插件
|
|
16
|
+
*
|
|
17
|
+
* 在 transform 阶段拦截所有 YAML 配置文件,将 YAML → JS 对象的转换
|
|
18
|
+
* 移到构建时完成。运行时直接 import 得到已解析的对象。
|
|
19
|
+
*
|
|
20
|
+
* 覆盖范围:
|
|
21
|
+
* - `_ui.yaml` → UiConfig 对象(parseUiConfig 预编译)
|
|
22
|
+
* - `_event.yaml` → ParsedEvent[] 数组(parseEventConfig 预编译)
|
|
23
|
+
* - `locale.yaml` → Record<string, unknown>
|
|
24
|
+
* - `*.config.yaml` → 任意配置对象
|
|
25
|
+
* - 其他 `.yaml` → 通用 YAML → JS 对象
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* // vite.config.ts
|
|
30
|
+
* import { somaPlugin } from './core/compiler/vite-plugin-soma';
|
|
31
|
+
*
|
|
32
|
+
* export default defineConfig({
|
|
33
|
+
* plugins: [somaPlugin()],
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function somaPlugin(options: SomaPluginOptions = {}): Plugin {
|
|
38
|
+
const uiSuffix = options.uiSuffix ?? '_ui.yaml';
|
|
39
|
+
const eventSuffix = options.eventSuffix ?? '_event.yaml';
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
name: 'vite-plugin-soma',
|
|
43
|
+
enforce: 'pre',
|
|
44
|
+
|
|
45
|
+
transform(code: string, id: string) {
|
|
46
|
+
if (!id.endsWith('.yaml') && !id.endsWith('.yml')) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
// SOMA UI 配置
|
|
52
|
+
if (id.endsWith(uiSuffix)) {
|
|
53
|
+
const compiled = precompileUiYaml(code);
|
|
54
|
+
return { code: `export default ${compiled};`, map: null };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// SOMA Event 配置
|
|
58
|
+
if (id.endsWith(eventSuffix)) {
|
|
59
|
+
const compiled = precompileEventYaml(code);
|
|
60
|
+
return { code: `export default ${compiled};`, map: null };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// 通用 YAML(locale、app.config 等)
|
|
64
|
+
const compiled = precompileYaml(code);
|
|
65
|
+
return { code: `export default ${compiled};`, map: null };
|
|
66
|
+
} catch (e: any) {
|
|
67
|
+
this.error(`[vite-plugin-soma] 预编译失败 (${id}): ${e.message}`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return null;
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|