soma-core 2.2.9 → 2.2.10

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.
@@ -317,6 +317,9 @@ var require_source_packages = __commonJS({
317
317
  }
318
318
  module2.exports = {
319
319
  SOMA_SOURCE_PACKAGES_ENV,
320
+ isSomaSourcePackage,
321
+ readExplicitSourcePackages,
322
+ resolvePackageRoot,
320
323
  getSomaHostSourceRoot: getSomaHostSourceRoot4,
321
324
  getSomaHostApiRoot: getSomaHostApiRoot4,
322
325
  resolveSafeChildRoot: resolveSafeChildRoot2,
@@ -3003,7 +3006,9 @@ var SOMA_PIPELINE_LIMITS = {
3003
3006
  "bodyAt": "\u65E0 key \u6761\u76EE\u6309\u6B63\u6587\u914D\u5BF9\u7684\u76F8\u4F3C\u5EA6\u9608\u503C\uFF08\u7B14\u8BB0 C.2 ItemOpts.match.bodyAt 0.5\uFF09",
3004
3007
  "maxAlignTokens": "\u5E8F\u5217\u5BF9\u9F50\u7684\u4E0D\u540C\u952E\u6570\u4E0A\u9650\uFF08\u6BCF\u4E2A\u952E\u6620\u5C04\u4E3A\u4E00\u4E2A BMP \u79C1\u7528\u533A\u5B57\u7B26\uFF0CU+E000\u2013U+F8FF \u5171 6400 \u4E2A\uFF09",
3005
3008
  "maxPairMatrix": "\u4E00\u6BB5\u5220\u589E\u533A\u95F4\u5185\u53E5 / \u6BB5\u914D\u5BF9\u7684\u5019\u9009\u77E9\u9635\u4E0A\u9650\uFF08\u5220\u6570 \xD7 \u589E\u6570\uFF09\uFF0C\u8D85\u51FA\u4E0D\u914D\u5BF9\u3001\u6309\u5220 + \u589E\u5448\u73B0\uFF1B\u7ECF\u9A8C\u503C\uFF0C\u8BC1\u636E\u5F31",
3006
- "maxPairCandidates": "\u65E0 key \u6761\u76EE\u6309\u76F8\u4F3C\u5EA6\u914D\u5BF9\u7684\u5019\u9009\u77E9\u9635\u4E0A\u9650\uFF08\u5269\u4F59\u65E7\u6761\u76EE \xD7 \u5269\u4F59\u65B0\u6761\u76EE\uFF09\uFF1B\u7ECF\u9A8C\u503C\uFF0C\u8BC1\u636E\u5F31"
3009
+ "maxPairCandidates": "\u65E0 key \u6761\u76EE\u6309\u76F8\u4F3C\u5EA6\u914D\u5BF9\u7684\u5019\u9009\u77E9\u9635\u4E0A\u9650\uFF08\u5269\u4F59\u65E7\u6761\u76EE \xD7 \u5269\u4F59\u65B0\u6761\u76EE\uFF09\uFF1B\u7ECF\u9A8C\u503C\uFF0C\u8BC1\u636E\u5F31",
3010
+ "codeContext": "\u4EE3\u7801\u5DEE\u5F02\u6BCF\u5757\u7684\u4E0A\u4E0B\u6587\u884C\u6570\uFF08\u7B14\u8BB0 C.5\u300C\u4E0A\u4E0B\u6587 3 \u884C\u300D\uFF0C\u540C git diff -U3 / VS Code\uFF09",
3011
+ "codeIntraMaxLine": "\u4EE3\u7801\u6539\u884C\u505A\u884C\u5185\u5B57\u7B26\u5DEE\u5F02\u7684\u5355\u884C\u957F\u5EA6\u4E0A\u9650\uFF08\u5B57\u7B26\uFF09\uFF1B\u66F4\u957F\u7684\u884C\u53EA\u6807\u6574\u884C\u589E\u5220\uFF08\u7ECF\u9A8C\u503C\uFF1A\u538B\u7F29\u540E\u7684\u957F\u884C\u9010\u5B57\u6BD4\u8F83\u65E0\u610F\u4E49\u4E14\u8017\u65F6\uFF09"
3007
3012
  },
3008
3013
  "maxChars": 2e5,
3009
3014
  "maxCharsPerCompare": 2e4,
@@ -3018,7 +3023,9 @@ var SOMA_PIPELINE_LIMITS = {
3018
3023
  "bodyAt": 0.5,
3019
3024
  "maxAlignTokens": 6400,
3020
3025
  "maxPairMatrix": 2500,
3021
- "maxPairCandidates": 25e4
3026
+ "maxPairCandidates": 25e4,
3027
+ "codeContext": 3,
3028
+ "codeIntraMaxLine": 500
3022
3029
  },
3023
3030
  "highlight": {
3024
3031
  "basis": {
@@ -5809,7 +5809,9 @@ var SOMA_PIPELINE_LIMITS = {
5809
5809
  "bodyAt": "\u65E0 key \u6761\u76EE\u6309\u6B63\u6587\u914D\u5BF9\u7684\u76F8\u4F3C\u5EA6\u9608\u503C\uFF08\u7B14\u8BB0 C.2 ItemOpts.match.bodyAt 0.5\uFF09",
5810
5810
  "maxAlignTokens": "\u5E8F\u5217\u5BF9\u9F50\u7684\u4E0D\u540C\u952E\u6570\u4E0A\u9650\uFF08\u6BCF\u4E2A\u952E\u6620\u5C04\u4E3A\u4E00\u4E2A BMP \u79C1\u7528\u533A\u5B57\u7B26\uFF0CU+E000\u2013U+F8FF \u5171 6400 \u4E2A\uFF09",
5811
5811
  "maxPairMatrix": "\u4E00\u6BB5\u5220\u589E\u533A\u95F4\u5185\u53E5 / \u6BB5\u914D\u5BF9\u7684\u5019\u9009\u77E9\u9635\u4E0A\u9650\uFF08\u5220\u6570 \xD7 \u589E\u6570\uFF09\uFF0C\u8D85\u51FA\u4E0D\u914D\u5BF9\u3001\u6309\u5220 + \u589E\u5448\u73B0\uFF1B\u7ECF\u9A8C\u503C\uFF0C\u8BC1\u636E\u5F31",
5812
- "maxPairCandidates": "\u65E0 key \u6761\u76EE\u6309\u76F8\u4F3C\u5EA6\u914D\u5BF9\u7684\u5019\u9009\u77E9\u9635\u4E0A\u9650\uFF08\u5269\u4F59\u65E7\u6761\u76EE \xD7 \u5269\u4F59\u65B0\u6761\u76EE\uFF09\uFF1B\u7ECF\u9A8C\u503C\uFF0C\u8BC1\u636E\u5F31"
5812
+ "maxPairCandidates": "\u65E0 key \u6761\u76EE\u6309\u76F8\u4F3C\u5EA6\u914D\u5BF9\u7684\u5019\u9009\u77E9\u9635\u4E0A\u9650\uFF08\u5269\u4F59\u65E7\u6761\u76EE \xD7 \u5269\u4F59\u65B0\u6761\u76EE\uFF09\uFF1B\u7ECF\u9A8C\u503C\uFF0C\u8BC1\u636E\u5F31",
5813
+ "codeContext": "\u4EE3\u7801\u5DEE\u5F02\u6BCF\u5757\u7684\u4E0A\u4E0B\u6587\u884C\u6570\uFF08\u7B14\u8BB0 C.5\u300C\u4E0A\u4E0B\u6587 3 \u884C\u300D\uFF0C\u540C git diff -U3 / VS Code\uFF09",
5814
+ "codeIntraMaxLine": "\u4EE3\u7801\u6539\u884C\u505A\u884C\u5185\u5B57\u7B26\u5DEE\u5F02\u7684\u5355\u884C\u957F\u5EA6\u4E0A\u9650\uFF08\u5B57\u7B26\uFF09\uFF1B\u66F4\u957F\u7684\u884C\u53EA\u6807\u6574\u884C\u589E\u5220\uFF08\u7ECF\u9A8C\u503C\uFF1A\u538B\u7F29\u540E\u7684\u957F\u884C\u9010\u5B57\u6BD4\u8F83\u65E0\u610F\u4E49\u4E14\u8017\u65F6\uFF09"
5813
5815
  },
5814
5816
  "maxChars": 2e5,
5815
5817
  "maxCharsPerCompare": 2e4,
@@ -5824,7 +5826,9 @@ var SOMA_PIPELINE_LIMITS = {
5824
5826
  "bodyAt": 0.5,
5825
5827
  "maxAlignTokens": 6400,
5826
5828
  "maxPairMatrix": 2500,
5827
- "maxPairCandidates": 25e4
5829
+ "maxPairCandidates": 25e4,
5830
+ "codeContext": 3,
5831
+ "codeIntraMaxLine": 500
5828
5832
  },
5829
5833
  "highlight": {
5830
5834
  "basis": {
@@ -0,0 +1,27 @@
1
+ import type { CodeDiff, CodeDiffHunk, CodeDiffLine, MarkdownBlockKind, MarkdownDiff } from './port';
2
+ import { type ProseRun } from './prose';
3
+ export interface CodeConfig {
4
+ readonly maxChars: number;
5
+ readonly maxAlignTokens: number;
6
+ readonly maxPairMatrix: number;
7
+ readonly pairAt: number;
8
+ /** 每块上下文行数 */
9
+ readonly context: number;
10
+ /** 行内字符差异只对不超过此长度的行做 */
11
+ readonly intraMaxLine: number;
12
+ }
13
+ /** 行级操作 → 按上下文切块(纯函数) */
14
+ export declare function hunksOf(lines: readonly CodeDiffLine[], context: number): CodeDiffHunk[];
15
+ export declare function runCode(run: ProseRun, cfg: CodeConfig, a: string, b: string): Promise<CodeDiff>;
16
+ /** 源码 → 块(空行分隔;围栏代码整块,围栏里的空行不切);纯函数 */
17
+ export declare function splitMarkdownBlocks(source: string): string[];
18
+ export declare function markdownBlockKind(block: string): {
19
+ kind: MarkdownBlockKind;
20
+ detail: string;
21
+ };
22
+ /** 块的可比文字:去掉块首标记(# / > / 列表符号 / 围栏行),空白归一 */
23
+ export declare function markdownBlockText(block: string, kind: MarkdownBlockKind): string;
24
+ export interface MarkdownConfig extends CodeConfig {
25
+ readonly maxBlocks: number;
26
+ }
27
+ export declare function runMarkdown(run: ProseRun, cfg: MarkdownConfig, a: string, b: string): Promise<MarkdownDiff>;
@@ -1,5 +1,6 @@
1
- export type { DiffFragment, DiffMode, DiffOptions, DiffPart, ItemDiff, ItemDiffEntry, ItemDiffOptions, ItemFieldDiff, ItemFieldSpec, ItemFieldType, ItemState, ProseBlock, ProseDegraded, ProseDiff, ProseDiffOptions, ProseNormalizeOptions, SomaDiffAdapter, SomaDiffPort, } from './port';
1
+ export type { CodeDiff, CodeDiffHunk, CodeDiffLine, CodeDiffOptions, MarkdownBlockKind, MarkdownDiff, MarkdownDiffBlock, DiffFragment, DiffMode, DiffOptions, DiffPart, ItemDiff, ItemDiffEntry, ItemDiffOptions, ItemFieldDiff, ItemFieldSpec, ItemFieldType, ItemState, ProseBlock, ProseDegraded, ProseDiff, ProseDiffOptions, ProseNormalizeOptions, SomaDiffAdapter, SomaDiffPort, } from './port';
2
2
  export { assertDiffAdapter } from './port';
3
+ export { hunksOf, markdownBlockKind, markdownBlockText, splitMarkdownBlocks } from './code';
3
4
  import type { EngineLoadTelemetry } from '../load';
4
5
  import { type SomaDiffAdapter, type SomaDiffPort } from './port';
5
6
  export interface DiffPortOptions {
@@ -127,6 +127,79 @@ export interface ItemDiff {
127
127
  readonly movesSkipped: boolean;
128
128
  readonly degraded?: ProseDegraded | 'too-many-items';
129
129
  }
130
+ export interface CodeDiffLine {
131
+ readonly op: 'eq' | 'ins' | 'del';
132
+ /** 旧版行号(1 起;增行无) */
133
+ readonly a?: number;
134
+ /** 新版行号(1 起;删行无) */
135
+ readonly b?: number;
136
+ readonly text: string;
137
+ /** 改行:配对另一侧的行号(删行给新版行号,增行给旧版行号) */
138
+ readonly pair?: number;
139
+ /** 改行的行内字符差异(删行只含 eq / del,增行只含 eq / ins);行过长或超时缺省 */
140
+ readonly fragments?: readonly DiffFragment[];
141
+ }
142
+ export interface CodeDiffHunk {
143
+ readonly aStart: number;
144
+ readonly aLines: number;
145
+ readonly bStart: number;
146
+ readonly bLines: number;
147
+ /** 在 `lines` 中的起止下标(含) */
148
+ readonly from: number;
149
+ readonly to: number;
150
+ readonly lines: readonly CodeDiffLine[];
151
+ }
152
+ export interface CodeDiff {
153
+ readonly kind: 'same' | 'changed';
154
+ /** 全部行(合并视图顺序:改行组内删行在增行上方) */
155
+ readonly lines: readonly CodeDiffLine[];
156
+ /** 按 design `contentEngines.diff.codeContext` 行上下文切出的块 */
157
+ readonly hunks: readonly CodeDiffHunk[];
158
+ readonly stats: {
159
+ readonly added: number;
160
+ readonly removed: number;
161
+ readonly modified: number;
162
+ };
163
+ /** 去掉全部空白后两侧相同 */
164
+ readonly whitespaceOnly: boolean;
165
+ /** too-large / timeout:不逐行对齐,整体按「全删 + 全增」给出 */
166
+ readonly degraded?: 'too-large' | 'timeout';
167
+ }
168
+ export type MarkdownBlockKind = 'heading' | 'paragraph' | 'list' | 'quote' | 'code' | 'table' | 'hr';
169
+ export interface MarkdownDiffBlock {
170
+ readonly op: 'eq' | 'ins' | 'del' | 'mod';
171
+ readonly kind: MarkdownBlockKind;
172
+ /** 块的 Markdown 源码(组件经 Markdown 端口渲染) */
173
+ readonly before?: string;
174
+ readonly after?: string;
175
+ /** mod:块文字(去块首标记)的词级差异;代码块不给(组件改用 diffCode) */
176
+ readonly fragments?: readonly DiffFragment[];
177
+ /** 结构变化:标题层级、列表有序 / 无序、代码语言 */
178
+ readonly notes?: readonly {
179
+ readonly what: 'heading-level' | 'list-type' | 'code-language';
180
+ readonly before: string;
181
+ readonly after: string;
182
+ }[];
183
+ readonly similarity?: number;
184
+ }
185
+ export interface MarkdownDiff {
186
+ readonly kind: 'same' | 'changed';
187
+ readonly blocks: readonly MarkdownDiffBlock[];
188
+ readonly stats: {
189
+ readonly added: number;
190
+ readonly removed: number;
191
+ readonly modified: number;
192
+ readonly same: number;
193
+ };
194
+ /** too-large / timeout:blocks 为空,组件退回源码对比(diffCode) */
195
+ readonly degraded?: 'too-large' | 'timeout';
196
+ }
197
+ export interface CodeDiffOptions {
198
+ /** 时限(调用方只能更小) */
199
+ readonly timeoutMs?: number;
200
+ /** 上下文行数(调用方只能更小) */
201
+ readonly context?: number;
202
+ }
130
203
  export interface SomaDiffPort {
131
204
  /** 两段字符串按粒度求片段(超时退回整段替换,不抛错) */
132
205
  diff(a: string, b: string, mode: DiffMode, options?: DiffOptions): Promise<readonly DiffPart[]>;
@@ -134,5 +207,9 @@ export interface SomaDiffPort {
134
207
  diffProse(a: string, b: string, options?: ProseDiffOptions): Promise<ProseDiff>;
135
208
  /** 条目列表差异:key 或内容配对、字段差异、LIS 最少移动 */
136
209
  diffItems(before: readonly unknown[], after: readonly unknown[], options?: ItemDiffOptions): Promise<ItemDiff>;
210
+ /** 代码差异:按行对齐、改行行内字符差异、按上下文切块;超长 / 超时退化为全删 + 全增(不抛) */
211
+ diffCode(a: string, b: string, options?: CodeDiffOptions): Promise<CodeDiff>;
212
+ /** Markdown 差异:源码块级对齐 + 块内词级 + 结构变化说明;超长 / 超时 blocks 为空并标 degraded(不抛) */
213
+ diffMarkdown(a: string, b: string, options?: CodeDiffOptions): Promise<MarkdownDiff>;
137
214
  }
138
215
  export declare function assertDiffAdapter(adapter: unknown): void;
@@ -10,6 +10,11 @@ export type SomaAssetSourceItem = {
10
10
  };
11
11
  /** 环境变量:显式声明的源包名(逗号分隔),不论其 package.json 怎么写都当源包 */
12
12
  export const SOMA_SOURCE_PACKAGES_ENV: "SOMA_SOURCE_PACKAGES";
13
+ /** 源包判定:显式声明(环境变量)或 `package.json` 的 `soma.source === true` */
14
+ export function isSomaSourcePackage(packageRoot: any, explicit: any): boolean;
15
+ /** @param {SomaEnv} [env] */
16
+ export function readExplicitSourcePackages(env?: SomaEnv): string[];
17
+ export function resolvePackageRoot(hostDir: any, packageName: any, version: any): string | null;
13
18
  export function getSomaHostSourceRoot(hostDir: any, env?: NodeJS.ProcessEnv): string;
14
19
  export function getSomaHostApiRoot(hostDir: any, env?: NodeJS.ProcessEnv): string;
15
20
  /** 子路径必须落在 parent 内且存在,否则回落 parent(越界不报错、不跟随) */
@@ -531,6 +531,8 @@ export declare const SOMA_PIPELINE_LIMITS: {
531
531
  readonly maxAlignTokens: "序列对齐的不同键数上限(每个键映射为一个 BMP 私用区字符,U+E000–U+F8FF 共 6400 个)";
532
532
  readonly maxPairMatrix: "一段删增区间内句 / 段配对的候选矩阵上限(删数 × 增数),超出不配对、按删 + 增呈现;经验值,证据弱";
533
533
  readonly maxPairCandidates: "无 key 条目按相似度配对的候选矩阵上限(剩余旧条目 × 剩余新条目);经验值,证据弱";
534
+ readonly codeContext: "代码差异每块的上下文行数(笔记 C.5「上下文 3 行」,同 git diff -U3 / VS Code)";
535
+ readonly codeIntraMaxLine: "代码改行做行内字符差异的单行长度上限(字符);更长的行只标整行增删(经验值:压缩后的长行逐字比较无意义且耗时)";
534
536
  };
535
537
  readonly maxChars: 200000;
536
538
  readonly maxCharsPerCompare: 20000;
@@ -546,6 +548,8 @@ export declare const SOMA_PIPELINE_LIMITS: {
546
548
  readonly maxAlignTokens: 6400;
547
549
  readonly maxPairMatrix: 2500;
548
550
  readonly maxPairCandidates: 250000;
551
+ readonly codeContext: 3;
552
+ readonly codeIntraMaxLine: 500;
549
553
  };
550
554
  readonly highlight: {
551
555
  readonly basis: {
@@ -2,7 +2,7 @@ import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { createHash } from 'node:crypto';
4
4
  import esbuild, { type BuildContext, type Plugin } from 'esbuild';
5
- import { readAllSomaPublicAliases } from './public-packages.cjs';
5
+ import { readAllSomaPublicAliases, createPublicAliasesFilter } from './public-packages.cjs';
6
6
  import { collectDevChunks, outputsRelativeTo, releaseDevChunks } from './dev-chunk-gc';
7
7
  import { toPascalCase } from '../../core/server/api/service-path';
8
8
  import { getSomaHostSourceRoot, getSomaHostApiRoot } from '../../core/server/host/source-packages.cjs';
@@ -87,14 +87,16 @@ const repoRoot = path.resolve(coreRoot, '..');
87
87
  * 客户端专属 esbuild 插件:
88
88
  * 1. 严禁 import 'server-only';
89
89
  * 2. 映射 @soma-host/* 虚拟/可选模块;
90
- * 3. 映射 soma-* 公开子路径。
90
+ * 3. 映射 soma-* 及 soma.source 源包公开子路径。
91
91
  */
92
92
  function createBrowserClientPlugin(options: {
93
93
  hostDir: string;
94
94
  webRoot: string;
95
+ env?: NodeJS.ProcessEnv;
95
96
  }): Plugin {
96
- const { hostDir, webRoot } = options;
97
- const publicAliases = readAllSomaPublicAliases(hostDir, repoRoot, [webRoot, coreRoot]) as Record<string, string>;
97
+ const { hostDir, webRoot, env } = options;
98
+ const publicAliases = readAllSomaPublicAliases(hostDir, repoRoot, [webRoot, coreRoot], env) as Record<string, string>;
99
+ const publicFilter = createPublicAliasesFilter(publicAliases);
98
100
 
99
101
  return {
100
102
  name: 'soma-browser-client-plugin',
@@ -175,7 +177,7 @@ function createBrowserClientPlugin(options: {
175
177
  });
176
178
 
177
179
  // 5b. 源码根别名(soma.sourceRoots)
178
- registerSourceRootResolve(build, hostDir);
180
+ registerSourceRootResolve(build, hostDir, env);
179
181
 
180
182
  // 6. 拦截内部 engine 模块私有深导入(RT4 守卫)
181
183
  build.onResolve({ filter: /core\/client\/engine\// }, (args) => {
@@ -187,14 +189,16 @@ function createBrowserClientPlugin(options: {
187
189
  return undefined;
188
190
  });
189
191
 
190
- // 7. soma-* 公开子路径解析
191
- build.onResolve({ filter: /^soma-[a-z0-9-]+(?:\/.*)?$/ }, (args) => {
192
- const direct = publicAliases[args.path];
193
- if (direct && fs.existsSync(direct)) {
194
- return { path: direct };
195
- }
196
- return undefined;
197
- });
192
+ // 7. soma-* 与 soma.source 源包公开子路径解析
193
+ if (publicFilter) {
194
+ build.onResolve({ filter: publicFilter }, (args) => {
195
+ const direct = publicAliases[args.path];
196
+ if (direct && fs.existsSync(direct)) {
197
+ return { path: direct };
198
+ }
199
+ return undefined;
200
+ });
201
+ }
198
202
 
199
203
  // 8. 可选 peer 依赖 echarts:仅当包 `echarts` 本身无法解析(未安装)时才回落轻量无头桩模块,
200
204
  // 防未装 echarts 的应用在 soma dev / build 时崩溃;已安装 echarts 但某个子路径解析失败(版本不符、路径写错)
@@ -264,7 +268,7 @@ export async function buildBrowserClient(
264
268
  fs.mkdirSync(clientOutDir, { recursive: true });
265
269
 
266
270
  const clientEntry = path.join(hostNativeRoot, 'client/browser-entry.ts');
267
- const plugin = createBrowserClientPlugin({ hostDir, webRoot });
271
+ const plugin = createBrowserClientPlugin({ hostDir, webRoot, env: options.env });
268
272
 
269
273
  // 编译并输出 CSS
270
274
  const cssSourcePath = path.join(hostNativeRoot, 'styles/globals.css');
@@ -371,8 +375,20 @@ export async function buildBrowserClient(
371
375
  };
372
376
  }
373
377
 
378
+ /** 将公开包说明符映射为无冲突的共享 AOT 预编译文件名(不含 .cjs 后缀) */
379
+ function sharedPackageSlug(specifier: string): string {
380
+ const safe = specifier
381
+ .replace(/^@/, '')
382
+ .replace(/[^a-zA-Z0-9_-]+/g, '__')
383
+ .replace(/^_+|_+$/g, '');
384
+ const hash = createHash('sha256').update(specifier).digest('hex').slice(0, 8);
385
+ return `${safe || 'pkg'}-${hash}`;
386
+ }
387
+
374
388
  /**
375
- * 后端 AOT 预编译:扫描后端服务源文件及 soma.app.ts,内联编译成 Node CJS 极速产物并生成 aot-manifest.json
389
+ * 后端 AOT 预编译:扫描后端服务源文件及 soma.app.ts,内联编译成 Node CJS 极速产物并生成 aot-manifest.json。
390
+ * 被引用的 soma-* / soma.source 源包出口统一预编译一次到 `aot-server/_packages/*.cjs`,各服务通过相对 require 共享同一份实例,
391
+ * 既彻底避免 Node 24 拒绝在 node_modules 下剥离 .ts 类型(ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING),又避免多服务重复内联导致的体积膨胀与单例分裂。
376
392
  */
377
393
  export async function buildServerAot(
378
394
  options: BuildServerAotOptions,
@@ -382,15 +398,17 @@ export async function buildServerAot(
382
398
  const apiRoot = path.resolve(options.apiRoot || getSomaHostApiRoot(hostDir, options.env));
383
399
  const outDir = path.resolve(options.outDir);
384
400
  const aotServerDir = path.join(outDir, 'aot-server');
401
+ const sharedPackagesDir = path.join(aotServerDir, '_packages');
385
402
  fs.mkdirSync(aotServerDir, { recursive: true });
403
+ fs.rmSync(sharedPackagesDir, { recursive: true, force: true });
386
404
 
387
405
  const serviceFiles = scanServiceFiles(apiRoot, webRoot);
388
- const entryPoints: Record<string, string> = {};
406
+ const serviceEntryPoints: Record<string, string> = {};
389
407
  const routes: Record<string, string> = {};
390
408
 
391
409
  for (const item of serviceFiles) {
392
410
  const entryName = `services/${item.routeKey.replace(/\//g, '_')}`;
393
- entryPoints[entryName] = item.filePath;
411
+ serviceEntryPoints[entryName] = item.filePath;
394
412
  routes[item.routeKey] = `${entryName}.cjs`;
395
413
 
396
414
  // 支持不同大小写路由键
@@ -409,85 +427,123 @@ export async function buildServerAot(
409
427
  let hostAppCompiledPath: string | undefined;
410
428
  const hostAppPath = path.resolve(webRoot, 'soma.app.ts');
411
429
  if (fs.existsSync(hostAppPath)) {
412
- entryPoints['host-app'] = hostAppPath;
413
430
  hostAppCompiledPath = 'host-app.cjs';
414
431
  }
415
432
 
416
- if (Object.keys(entryPoints).length > 0) {
433
+ const publicAliases = readAllSomaPublicAliases(hostDir, repoRoot, [webRoot, apiRoot, coreRoot], options.env) as Record<string, string>;
434
+ const publicFilter = createPublicAliasesFilter(publicAliases);
435
+ const sharedEntries = new Map<string, string>();
436
+ const nodePaths = [
437
+ path.join(apiRoot, 'node_modules'),
438
+ path.join(webRoot, 'node_modules'),
439
+ path.join(hostDir, 'node_modules'),
440
+ path.join(coreRoot, 'node_modules'),
441
+ ].filter((d) => fs.existsSync(d));
442
+
443
+ const createServerAotPlugin = (mode: { sharedOutPrefix: string } | { inlinePublic: true }): Plugin => ({
444
+ name: 'soma-server-aot-plugin',
445
+ setup(build) {
446
+ build.onResolve({ filter: /^server-only$/ }, () => {
447
+ return { path: path.resolve(coreRoot, 'core/server/host/server-only-noop.cjs') };
448
+ });
449
+ build.onResolve({ filter: /^@core(?:\/.*)?$/ }, (args) => {
450
+ const subpath = args.path.replace(/^@core\/?/, '');
451
+ return { path: resolveModuleFile(path.resolve(coreRoot, 'core'), subpath) };
452
+ });
453
+ build.onResolve({ filter: /^@soma-host\/bootstrap\/soma-bootstrap$/ }, () => {
454
+ const candidateHostFile = path.resolve(webRoot, 'bootstrap/soma-bootstrap');
455
+ for (const ext of ['.ts', '.js', '.mjs']) {
456
+ if (fs.existsSync(candidateHostFile + ext)) {
457
+ return { path: candidateHostFile + ext };
458
+ }
459
+ }
460
+ if (fs.existsSync(path.resolve(webRoot, 'soma.app.ts'))) {
461
+ return { path: path.resolve(coreRoot, 'core/client/host-bridge/soma-bootstrap-from-app.ts') };
462
+ }
463
+ return { path: path.resolve(coreRoot, 'core/client/host-bridge/soma-bootstrap-host.stub.ts') };
464
+ });
465
+ build.onResolve({ filter: /^@soma-host\/bootstrap\/soma-domain-loaders$/ }, () => {
466
+ const candidateLoader = path.resolve(webRoot, 'bootstrap/soma-domain-loaders');
467
+ for (const ext of ['.ts', '.js', '.mjs']) {
468
+ if (fs.existsSync(candidateLoader + ext)) {
469
+ return { path: candidateLoader + ext };
470
+ }
471
+ }
472
+ return { path: path.resolve(coreRoot, 'core/client/host-bridge/soma-domain-loaders.stub.ts') };
473
+ });
474
+ build.onResolve({ filter: /^@soma-host\/domain\/ui\/.*Class\.ts$/ }, (args) => {
475
+ const baseName = path.basename(args.path);
476
+ const diskFile = path.resolve(webRoot, 'domain/ui', baseName);
477
+ if (fs.existsSync(diskFile)) {
478
+ return { path: diskFile };
479
+ }
480
+ return { path: args.path, namespace: 'soma-absent-domain' };
481
+ });
482
+ build.onLoad({ filter: /.*/, namespace: 'soma-absent-domain' }, (args) => ({
483
+ contents: `const err = new Error("Cannot find module '${args.path}'"); err.code = "MODULE_NOT_FOUND"; throw err;`,
484
+ loader: 'js',
485
+ }));
486
+ build.onResolve({ filter: /^@soma-host(?:\/.*)?$/ }, (args) => {
487
+ const subpath = args.path.replace(/^@soma-host\/?/, '');
488
+ return { path: resolveModuleFile(webRoot, subpath) };
489
+ });
490
+ registerSourceRootResolve(build, hostDir, options.env);
491
+ build.onResolve({ filter: /^@soma-api(?:\/.*)?$/ }, (args) => {
492
+ const subpath = args.path.replace(/^@soma-api\/?/, '');
493
+ return { path: resolveModuleFile(apiRoot, subpath) };
494
+ });
495
+ if (publicFilter) {
496
+ build.onResolve({ filter: publicFilter }, (args) => {
497
+ const direct = publicAliases[args.path];
498
+ if (!direct || !fs.existsSync(direct)) {
499
+ return undefined;
500
+ }
501
+ if ('inlinePublic' in mode) {
502
+ return { path: direct };
503
+ }
504
+ const slug = sharedPackageSlug(args.path);
505
+ sharedEntries.set(slug, direct);
506
+ return { path: `${mode.sharedOutPrefix}_packages/${slug}.cjs`, external: true };
507
+ });
508
+ }
509
+ },
510
+ });
511
+
512
+ const baseServerBuildOptions: Omit<esbuild.BuildOptions, 'entryPoints' | 'outdir' | 'plugins'> = {
513
+ bundle: true,
514
+ platform: 'node',
515
+ format: 'cjs',
516
+ target: 'node20',
517
+ packages: 'external',
518
+ nodePaths,
519
+ outExtension: { '.js': '.cjs' },
520
+ };
521
+
522
+ if (Object.keys(serviceEntryPoints).length > 0) {
523
+ await esbuild.build({
524
+ ...baseServerBuildOptions,
525
+ entryPoints: serviceEntryPoints,
526
+ outdir: aotServerDir,
527
+ plugins: [createServerAotPlugin({ sharedOutPrefix: '../' })],
528
+ });
529
+ }
530
+
531
+ if (hostAppCompiledPath) {
417
532
  await esbuild.build({
418
- entryPoints,
533
+ ...baseServerBuildOptions,
534
+ entryPoints: { 'host-app': hostAppPath },
419
535
  outdir: aotServerDir,
420
- bundle: true,
421
- platform: 'node',
422
- format: 'cjs',
423
- target: 'node20',
424
- packages: 'external',
425
- nodePaths: [path.join(apiRoot, 'node_modules'), path.join(webRoot, 'node_modules'), path.join(hostDir, 'node_modules'), path.join(coreRoot, 'node_modules')].filter((d) => fs.existsSync(d)),
426
- outExtension: { '.js': '.cjs' },
427
- plugins: [
428
- {
429
- name: 'soma-server-aot-plugin',
430
- setup(build) {
431
- const publicAliases = readAllSomaPublicAliases(hostDir, repoRoot, [webRoot, apiRoot, coreRoot]) as Record<string, string>;
432
- build.onResolve({ filter: /^server-only$/ }, () => {
433
- return { path: path.resolve(coreRoot, 'core/server/host/server-only-noop.cjs') };
434
- });
435
- build.onResolve({ filter: /^@core(?:\/.*)?$/ }, (args) => {
436
- const subpath = args.path.replace(/^@core\/?/, '');
437
- return { path: resolveModuleFile(path.resolve(coreRoot, 'core'), subpath) };
438
- });
439
- build.onResolve({ filter: /^@soma-host\/bootstrap\/soma-bootstrap$/ }, () => {
440
- const candidateHostFile = path.resolve(webRoot, 'bootstrap/soma-bootstrap');
441
- for (const ext of ['.ts', '.js', '.mjs']) {
442
- if (fs.existsSync(candidateHostFile + ext)) {
443
- return { path: candidateHostFile + ext };
444
- }
445
- }
446
- if (fs.existsSync(path.resolve(webRoot, 'soma.app.ts'))) {
447
- return { path: path.resolve(coreRoot, 'core/client/host-bridge/soma-bootstrap-from-app.ts') };
448
- }
449
- return { path: path.resolve(coreRoot, 'core/client/host-bridge/soma-bootstrap-host.stub.ts') };
450
- });
451
- build.onResolve({ filter: /^@soma-host\/bootstrap\/soma-domain-loaders$/ }, () => {
452
- const candidateLoader = path.resolve(webRoot, 'bootstrap/soma-domain-loaders');
453
- for (const ext of ['.ts', '.js', '.mjs']) {
454
- if (fs.existsSync(candidateLoader + ext)) {
455
- return { path: candidateLoader + ext };
456
- }
457
- }
458
- return { path: path.resolve(coreRoot, 'core/client/host-bridge/soma-domain-loaders.stub.ts') };
459
- });
460
- build.onResolve({ filter: /^@soma-host\/domain\/ui\/.*Class\.ts$/ }, (args) => {
461
- const baseName = path.basename(args.path);
462
- const diskFile = path.resolve(webRoot, 'domain/ui', baseName);
463
- if (fs.existsSync(diskFile)) {
464
- return { path: diskFile };
465
- }
466
- return { path: args.path, namespace: 'soma-absent-domain' };
467
- });
468
- build.onLoad({ filter: /.*/, namespace: 'soma-absent-domain' }, (args) => ({
469
- contents: `const err = new Error("Cannot find module '${args.path}'"); err.code = "MODULE_NOT_FOUND"; throw err;`,
470
- loader: 'js',
471
- }));
472
- build.onResolve({ filter: /^@soma-host(?:\/.*)?$/ }, (args) => {
473
- const subpath = args.path.replace(/^@soma-host\/?/, '');
474
- return { path: resolveModuleFile(webRoot, subpath) };
475
- });
476
- registerSourceRootResolve(build, hostDir, options.env);
477
- build.onResolve({ filter: /^@soma-api(?:\/.*)?$/ }, (args) => {
478
- const subpath = args.path.replace(/^@soma-api\/?/, '');
479
- return { path: resolveModuleFile(apiRoot, subpath) };
480
- });
481
- build.onResolve({ filter: /^soma-[a-z0-9-]+(?:\/.*)?$/ }, (args) => {
482
- const direct = publicAliases[args.path];
483
- if (direct && fs.existsSync(direct)) {
484
- return { path: direct };
485
- }
486
- return undefined;
487
- });
488
- },
489
- },
490
- ],
536
+ plugins: [createServerAotPlugin({ sharedOutPrefix: './' })],
537
+ });
538
+ }
539
+
540
+ if (sharedEntries.size > 0) {
541
+ fs.mkdirSync(sharedPackagesDir, { recursive: true });
542
+ await esbuild.build({
543
+ ...baseServerBuildOptions,
544
+ entryPoints: Object.fromEntries(sharedEntries),
545
+ outdir: sharedPackagesDir,
546
+ plugins: [createServerAotPlugin({ inlinePublic: true })],
491
547
  });
492
548
  }
493
549