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.
@@ -2,6 +2,11 @@
2
2
 
3
3
  const fs = require('node:fs');
4
4
  const path = require('node:path');
5
+ const {
6
+ isSomaSourcePackage,
7
+ readExplicitSourcePackages,
8
+ resolvePackageRoot,
9
+ } = require('../../core/server/host/source-packages.cjs');
5
10
 
6
11
  function readJson(filePath) {
7
12
  try {
@@ -16,6 +21,16 @@ function isSafePackageChild(packageRoot, candidate) {
16
21
  return candidate === packageRoot || candidate.startsWith(`${packageRoot}${path.sep}`);
17
22
  }
18
23
 
24
+ function isDirectoryOrSymlinkToDir(baseDir, entry) {
25
+ if (entry.isDirectory()) return true;
26
+ if (!entry.isSymbolicLink()) return false;
27
+ try {
28
+ return fs.statSync(path.join(baseDir, entry.name)).isDirectory();
29
+ } catch {
30
+ return false;
31
+ }
32
+ }
33
+
19
34
  function collectFiles(root) {
20
35
  if (!fs.existsSync(root) || !fs.statSync(root).isDirectory()) return [];
21
36
  const files = [];
@@ -60,59 +75,68 @@ function expandWildcardExportAliases(packageName, packageRoot, entry, target) {
60
75
  }
61
76
 
62
77
  /**
63
- * 枚举单个包 package.json 中的公开 exports 映射表。
78
+ * 枚举单个包 package.json 中的公开 exports 映射表(缺省 exports 时回退 main)。
64
79
  * 返回 map: specifier -> resolvedFilePath
65
80
  */
66
81
  function readPublicPackageAliases(packageName, packageRoot) {
67
- const exportsMap = readJson(path.join(packageRoot, 'package.json'))?.exports;
68
- if (!exportsMap || typeof exportsMap !== 'object') return {};
82
+ const pkgJson = readJson(path.join(packageRoot, 'package.json'));
83
+ const exportsMap = pkgJson?.exports;
69
84
 
70
85
  const exactAliases = {};
71
86
  const wildcardAliases = {};
72
87
 
73
- for (const [entry, descriptor] of Object.entries(exportsMap)) {
74
- if (entry !== '.' && !entry.startsWith('./')) continue;
75
- let target = null;
76
- if (typeof descriptor === 'string') {
77
- target = descriptor;
78
- } else if (descriptor && typeof descriptor === 'object') {
79
- target =
80
- descriptor.import?.default ??
81
- descriptor.import ??
82
- descriptor.default ??
83
- descriptor.require?.default ??
84
- descriptor.require;
85
- if (typeof target === 'object' && target !== null) {
86
- target = target.default;
88
+ if (exportsMap && typeof exportsMap === 'object') {
89
+ for (const [entry, descriptor] of Object.entries(exportsMap)) {
90
+ if (entry !== '.' && !entry.startsWith('./')) continue;
91
+ let target = null;
92
+ if (typeof descriptor === 'string') {
93
+ target = descriptor;
94
+ } else if (descriptor && typeof descriptor === 'object') {
95
+ target =
96
+ descriptor.import?.default ??
97
+ descriptor.import ??
98
+ descriptor.default ??
99
+ descriptor.require?.default ??
100
+ descriptor.require;
101
+ if (typeof target === 'object' && target !== null) {
102
+ target = target.default;
103
+ }
104
+ }
105
+ if (typeof target !== 'string' || !target.startsWith('./')) continue;
106
+
107
+ const isWildcard = entry.includes('*') || target.includes('*');
108
+ if (entry.includes('*') !== target.includes('*')) continue;
109
+ if (isWildcard) {
110
+ Object.assign(
111
+ wildcardAliases,
112
+ expandWildcardExportAliases(packageName, packageRoot, entry, target),
113
+ );
114
+ continue;
87
115
  }
116
+
117
+ const resolved = path.resolve(packageRoot, target);
118
+ if (!isSafePackageChild(packageRoot, resolved) || !fs.existsSync(resolved)) continue;
119
+ const publicPath = entry === '.' ? '' : entry.slice(2);
120
+ const specifier = publicPath ? `${packageName}/${publicPath}` : packageName;
121
+ exactAliases[specifier] = resolved;
88
122
  }
89
- if (typeof target !== 'string' || !target.startsWith('./')) continue;
90
-
91
- const isWildcard = entry.includes('*') || target.includes('*');
92
- if (entry.includes('*') !== target.includes('*')) continue;
93
- if (isWildcard) {
94
- Object.assign(
95
- wildcardAliases,
96
- expandWildcardExportAliases(packageName, packageRoot, entry, target),
97
- );
98
- continue;
123
+ } else if (typeof pkgJson?.main === 'string' && pkgJson.main.trim()) {
124
+ const resolved = path.resolve(packageRoot, pkgJson.main.trim());
125
+ if (isSafePackageChild(packageRoot, resolved) && fs.existsSync(resolved) && fs.statSync(resolved).isFile()) {
126
+ exactAliases[packageName] = resolved;
99
127
  }
100
-
101
- const resolved = path.resolve(packageRoot, target);
102
- if (!isSafePackageChild(packageRoot, resolved) || !fs.existsSync(resolved)) continue;
103
- const publicPath = entry === '.' ? '' : entry.slice(2);
104
- const specifier = publicPath ? `${packageName}/${publicPath}` : packageName;
105
- exactAliases[specifier] = resolved;
106
128
  }
107
129
 
108
130
  return { ...wildcardAliases, ...exactAliases };
109
131
  }
110
132
 
111
133
  /**
112
- * 扫描仓库/依赖中的全部 soma-* 包并生成完整的别名表
134
+ * 扫描仓库/依赖中的全部 soma-* 包及声明了 soma.source(或 SOMA_SOURCE_PACKAGES)的源包,
135
+ * 生成完整的公开入口别名表。源包判定唯一复用 core/server/host/source-packages.cjs(S1-d)。
113
136
  */
114
- function readAllSomaPublicAliases(hostDir, repoRoot, extraRoots = []) {
137
+ function readAllSomaPublicAliases(hostDir, repoRoot, extraRoots = [], env = process.env) {
115
138
  const aliases = {};
139
+ const explicitPackages = new Set(readExplicitSourcePackages(env));
116
140
  const searchRoots = [
117
141
  repoRoot,
118
142
  path.join(repoRoot, 'soma-core'),
@@ -130,37 +154,110 @@ function readAllSomaPublicAliases(hostDir, repoRoot, extraRoots = []) {
130
154
  }
131
155
 
132
156
  const seenPackages = new Set();
157
+ const seenNodeModules = new Set();
158
+
159
+ const shouldIncludePackage = (pkgName, pkgRoot) =>
160
+ Boolean(pkgName) && (pkgName.startsWith('soma-') || isSomaSourcePackage(pkgRoot, explicitPackages.has(pkgName)));
161
+
162
+ const registerPackage = (pkgName, pkgRoot) => {
163
+ if (!pkgName || seenPackages.has(pkgName) || !fs.existsSync(pkgRoot)) return;
164
+ if (!shouldIncludePackage(pkgName, pkgRoot)) return;
165
+ seenPackages.add(pkgName);
166
+ Object.assign(aliases, readPublicPackageAliases(pkgName, pkgRoot));
167
+ // 递归检查源包自带的嵌套 node_modules(防止传递源包未被提升到宿主根时漏扫)
168
+ const nestedNm = path.join(pkgRoot, 'node_modules');
169
+ if (fs.existsSync(nestedNm)) scanNodeModules(nestedNm);
170
+ };
171
+
172
+ const scanNodeModules = (nmDir) => {
173
+ let realNm = path.resolve(nmDir);
174
+ try {
175
+ realNm = fs.realpathSync(nmDir);
176
+ } catch {
177
+ /* keep resolved */
178
+ }
179
+ if (seenNodeModules.has(realNm)) return;
180
+ seenNodeModules.add(realNm);
181
+
182
+ let entries = [];
183
+ try {
184
+ entries = fs.readdirSync(nmDir, { withFileTypes: true });
185
+ } catch {
186
+ return;
187
+ }
188
+ for (const entry of entries) {
189
+ if (entry.name.startsWith('.')) continue;
190
+ if (!isDirectoryOrSymlinkToDir(nmDir, entry)) continue;
191
+ const entryPath = path.join(nmDir, entry.name);
192
+ if (entry.name.startsWith('@')) {
193
+ let scopedEntries = [];
194
+ try {
195
+ scopedEntries = fs.readdirSync(entryPath, { withFileTypes: true });
196
+ } catch {
197
+ scopedEntries = [];
198
+ }
199
+ for (const scoped of scopedEntries) {
200
+ if (scoped.name.startsWith('.')) continue;
201
+ if (!isDirectoryOrSymlinkToDir(entryPath, scoped)) continue;
202
+ registerPackage(`${entry.name}/${scoped.name}`, path.join(entryPath, scoped.name));
203
+ }
204
+ } else {
205
+ registerPackage(entry.name, entryPath);
206
+ }
207
+ }
208
+ };
133
209
 
134
210
  for (const searchRoot of searchRoots) {
135
- if (!fs.existsSync(searchRoot)) continue;
211
+ if (!searchRoot || !fs.existsSync(searchRoot)) continue;
136
212
  const pkgJsonPath = path.join(searchRoot, 'package.json');
137
213
  const pkgJson = readJson(pkgJsonPath);
138
- if (pkgJson?.name && pkgJson.name.startsWith('soma-') && !seenPackages.has(pkgJson.name)) {
139
- seenPackages.add(pkgJson.name);
140
- Object.assign(aliases, readPublicPackageAliases(pkgJson.name, searchRoot));
214
+ if (pkgJson?.name) {
215
+ registerPackage(pkgJson.name, searchRoot);
216
+ }
217
+
218
+ // 检查声明在 dependencies / devDependencies(含 file: 依赖)及 SOMA_SOURCE_PACKAGES 里的源包
219
+ const deps = {
220
+ ...(pkgJson?.dependencies ?? {}),
221
+ ...(pkgJson?.devDependencies ?? {}),
222
+ };
223
+ const depNames = new Set([...Object.keys(deps), ...explicitPackages]);
224
+ for (const depName of depNames) {
225
+ if (seenPackages.has(depName)) continue;
226
+ const depRoot = resolvePackageRoot(searchRoot, depName, deps[depName]);
227
+ if (depRoot) registerPackage(depName, depRoot);
141
228
  }
142
229
 
143
- // 检查子目录或 node_modules
230
+ // 扫描 node_modules(含实包、软链与 @scope/*)
144
231
  const nm = path.join(searchRoot, 'node_modules');
145
232
  if (fs.existsSync(nm)) {
146
- try {
147
- for (const entry of fs.readdirSync(nm, { withFileTypes: true })) {
148
- if (!entry.isDirectory()) continue;
149
- if (entry.name.startsWith('soma-') && !seenPackages.has(entry.name)) {
150
- const pkgPath = path.join(nm, entry.name);
151
- seenPackages.add(entry.name);
152
- Object.assign(aliases, readPublicPackageAliases(entry.name, pkgPath));
153
- }
154
- }
155
- } catch {}
233
+ scanNodeModules(nm);
156
234
  }
157
235
  }
158
236
 
159
237
  return aliases;
160
238
  }
161
239
 
240
+ /**
241
+ * 根据别名表构造 esbuild `onResolve` 过滤正则(精确覆盖全部公开包根,含 `@scope/pkg`);空表返回 null。
242
+ */
243
+ function createPublicAliasesFilter(aliases) {
244
+ const roots = new Set();
245
+ for (const spec of Object.keys(aliases || {})) {
246
+ const root = spec.startsWith('@')
247
+ ? spec.split('/').slice(0, 2).join('/')
248
+ : spec.split('/')[0];
249
+ if (root) roots.add(root);
250
+ }
251
+ if (roots.size === 0) return null;
252
+ const escaped = [...roots]
253
+ .sort()
254
+ .map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
255
+ return new RegExp(`^(?:${escaped.join('|')})(?:/.*)?$`);
256
+ }
257
+
162
258
  module.exports = {
163
259
  readJson,
164
260
  readPublicPackageAliases,
165
261
  readAllSomaPublicAliases,
262
+ createPublicAliasesFilter,
166
263
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "soma-core",
3
- "version": "2.2.9",
3
+ "version": "2.2.10",
4
4
  "description": "SOMA — Semantic Oriented Modular Architecture. A declarative frontend framework runtime with no UI framework dependency; native host shell ships in host-native/.",
5
5
  "keywords": [
6
6
  "soma",