soma-core 2.1.0 → 2.1.2

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.
@@ -1,6 +1,18 @@
1
1
  import fs from 'node:fs';
2
+ import { createRequire } from 'node:module';
2
3
  import path from 'node:path';
3
- import createJiti from 'jiti';
4
+
5
+ type JitiFactory = (id: string, opts?: unknown) => (file: string) => unknown;
6
+
7
+ let cachedJitiFactory: JitiFactory | null = null;
8
+ function getJitiFactory(): JitiFactory {
9
+ if (cachedJitiFactory === null) {
10
+ const req = createRequire(import.meta.url);
11
+ const jitiMod = req('jiti');
12
+ cachedJitiFactory = (jitiMod.default ?? jitiMod) as JitiFactory;
13
+ }
14
+ return cachedJitiFactory;
15
+ }
4
16
  import type { IService } from './IService';
5
17
  import { resolveServiceFilePath } from './service-path';
6
18
  import { getSomaAssetSources } from '../host/source-packages';
@@ -17,13 +29,14 @@ function getCoreDir(): string {
17
29
  return path.resolve(process.cwd(), 'core');
18
30
  }
19
31
 
20
- let jitiLoader: ReturnType<typeof createJiti> | null = null;
32
+ let jitiLoader: ((file: string) => unknown) | null = null;
21
33
  let jitiHostDir: string | null = null;
22
34
  const instanceCache = new Map<string, IService>();
23
35
 
24
- function getJiti(hostDir: string): ReturnType<typeof createJiti> {
36
+ function getJiti(hostDir: string): (file: string) => unknown {
25
37
  if (!jitiLoader || jitiHostDir !== hostDir) {
26
38
  const serverOnlyEmpty = resolveServerOnlyEmpty();
39
+ const createJiti = getJitiFactory();
27
40
  jitiLoader = createJiti(import.meta.url, {
28
41
  interopDefault: true,
29
42
  alias: {
@@ -41,6 +54,7 @@ function getJiti(hostDir: string): ReturnType<typeof createJiti> {
41
54
  * 按约定从宿主 api/service/ 加载 IService 实例(运行时读盘,无需手写 registry)。
42
55
  */
43
56
  export function loadHostService(segments: string[]): IService | null {
57
+ if (typeof process !== 'undefined' && process.env.NEXT_RUNTIME === 'edge') return null;
44
58
  const hostDir = process.env.SOMA_HOST_DIR;
45
59
  if (!hostDir) return null;
46
60
 
@@ -12,7 +12,17 @@ import fs from 'node:fs';
12
12
  import { createRequire } from 'node:module';
13
13
  import path from 'node:path';
14
14
  import { fileURLToPath } from 'node:url';
15
- import createJiti from 'jiti';
15
+ type JitiFactory = (id: string, opts?: unknown) => (file: string) => unknown;
16
+
17
+ let cachedJiti: JitiFactory | null = null;
18
+ function getJiti(): JitiFactory {
19
+ if (cachedJiti === null) {
20
+ const req = createRequire(import.meta.url);
21
+ const jitiMod = req('jiti');
22
+ cachedJiti = (jitiMod.default ?? jitiMod) as JitiFactory;
23
+ }
24
+ return cachedJiti;
25
+ }
16
26
  import type { SomaHostAssembly } from '../../shared/types/host-assembly.types';
17
27
  import { getSomaHostSourceRoot } from './source-packages';
18
28
 
@@ -185,6 +195,9 @@ export function resolveServerOnlyEmpty(): string | undefined {
185
195
  }
186
196
 
187
197
  function loadAppSync(appRoot: string): LoadedApp | null {
198
+ if (typeof process !== 'undefined' && process.env.NEXT_RUNTIME === 'edge') {
199
+ return null;
200
+ }
188
201
  const key = path.resolve(appRoot);
189
202
  const cached = cache.get(key);
190
203
  if (cached !== undefined) return cached;
@@ -196,6 +209,7 @@ function loadAppSync(appRoot: string): LoadedApp | null {
196
209
  return null;
197
210
  }
198
211
  const serverOnlyEmpty = resolveServerOnlyEmpty();
212
+ const createJiti = getJiti();
199
213
  const loader = createJiti(import.meta.url, {
200
214
  interopDefault: false,
201
215
  requireCache: false,
@@ -217,6 +231,7 @@ function loadAppSync(appRoot: string): LoadedApp | null {
217
231
  * Host 的同步消费点(编译点、虚拟页、树上下文)要求本函数先完成:入口路由在首次取页 / 取接口前 await 它。
218
232
  */
219
233
  export async function loadHostAssembly(appRoot: string): Promise<SomaHostAssembly | null> {
234
+ if (typeof process !== 'undefined' && process.env.NEXT_RUNTIME === 'edge') return null;
220
235
  const key = path.resolve(appRoot);
221
236
  const app = loadAppSync(key);
222
237
  if (app === null) return null;
@@ -237,6 +252,7 @@ export async function loadHostAssembly(appRoot: string): Promise<SomaHostAssembl
237
252
  * 只由宿主壳的启动入口(`instrumentation.ts`)调用;`soma build` 不调用。
238
253
  */
239
254
  export async function runHostStartup(appRoot: string): Promise<void> {
255
+ if (typeof process !== 'undefined' && process.env.NEXT_RUNTIME === 'edge') return;
240
256
  const key = path.resolve(appRoot);
241
257
  let pending = startups.get(key);
242
258
  if (pending === undefined) {
@@ -24,6 +24,7 @@ export async function resolveHostResponseHeaders(
24
24
  hostDir: string | undefined = process.env.SOMA_HOST_DIR,
25
25
  ): Promise<Readonly<Record<string, string>> | undefined> {
26
26
  if (hostDir === undefined || hostDir.trim() === '') return undefined;
27
+ if (typeof process !== 'undefined' && process.env.NEXT_RUNTIME === 'edge') return undefined;
27
28
  const assembly = await loadHostAssembly(hostDir);
28
29
  if (assembly?.responseHeaders === undefined) return undefined;
29
30
  const headers = await assembly.responseHeaders({
@@ -3,11 +3,16 @@
3
3
  // 时机:`soma dev` / `soma start` 的服务进程启动时(Node 运行时);`soma build` 不执行(build 只跑装配的 `prewarm`)。
4
4
  // 这里加载 `soma.app.ts` 并等服务端面(`server()`)装配完,再 await `startup()`;装配或启动钩子抛错,`register` 随之抛错,
5
5
  // 服务不进入可用状态(`soma start` 进程报错退出)。没有 `soma.app.ts` 或装配没写 `startup` 时只完成装配(无副作用)。
6
+ //
7
+ // Node/Edge 编译隔离:必须使用 `if (process.env.NEXT_RUNTIME === 'nodejs')` 块级条件包裹,
8
+ // 让 Next.js 在为 Edge 运行时编译 instrumentation.ts 时通过死代码消除(DCE)直接抹除对 host-assembly 的依赖,
9
+ // 严禁使用顶层 early return(early return 无法阻止 Webpack 分析后续的动态 import 进而触发 Can't resolve 'fs')。
6
10
  export async function register(): Promise<void> {
7
- if (process.env.NEXT_RUNTIME !== 'nodejs') return;
8
- if (process.env.NEXT_PHASE === 'phase-production-build') return;
9
- const hostDir = process.env.SOMA_HOST_DIR;
10
- if (!hostDir || !hostDir.trim()) return;
11
- const { runHostStartup } = await import('./core/server/host/host-assembly');
12
- await runHostStartup(hostDir);
11
+ if (process.env.NEXT_RUNTIME === 'nodejs') {
12
+ if (process.env.NEXT_PHASE === 'phase-production-build') return;
13
+ const hostDir = process.env.SOMA_HOST_DIR;
14
+ if (!hostDir || !hostDir.trim()) return;
15
+ const { runHostStartup } = await import('./core/server/host/host-assembly');
16
+ await runHostStartup(hostDir);
17
+ }
13
18
  }
@@ -298,7 +298,7 @@ const nextConfig = {
298
298
 
299
299
  transpilePackages,
300
300
 
301
- webpack: (config, { dev, isServer, webpack: webpackInstance }) => {
301
+ webpack: (config, { dev, isServer, nextRuntime, webpack: webpackInstance }) => {
302
302
  if (webpackCacheMode === 'memory' && dev) {
303
303
  config.cache = { type: 'memory' };
304
304
  }
@@ -308,6 +308,19 @@ const nextConfig = {
308
308
  ignored: mergeWatchIgnored(config.watchOptions?.ignored),
309
309
  };
310
310
  }
311
+ // Node/Edge 与浏览器编译隔离:非 Node.js 运行时一律排除 jiti 与 Node 原生模块回退
312
+ if (nextRuntime === 'edge' || !isServer) {
313
+ config.resolve.alias = {
314
+ ...config.resolve.alias,
315
+ jiti: false,
316
+ };
317
+ config.resolve.fallback = {
318
+ ...config.resolve.fallback,
319
+ fs: false,
320
+ path: false,
321
+ module: false,
322
+ };
323
+ }
311
324
  config.resolve.alias['@'] = coreRoot;
312
325
  Object.assign(config.resolve.alias, readCorePublicWebpackAliases());
313
326
  config.resolve.alias['@core'] = path.resolve(coreRoot, 'core');
@@ -340,6 +353,9 @@ const nextConfig = {
340
353
  checkResource(resource) {
341
354
  if (/soma\.server/.test(resource)) return true;
342
355
  if (!hasHostDomainUi && resource.startsWith('@soma-host/domain/ui')) return true;
356
+ if (nextRuntime === 'edge' || !isServer) {
357
+ if (/host-assembly/.test(resource) || resource === 'jiti' || resource.startsWith('jiti/')) return true;
358
+ }
343
359
  return false;
344
360
  },
345
361
  }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "soma-core",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "SOMA — Semantic Oriented Modular Architecture. A declarative frontend framework runtime with no UI framework dependency; the Next.js host shell ships in host-next/ (code-isolated from core/, next/react are optional peer dependencies).",
5
5
  "keywords": [
6
6
  "soma",
@@ -301,36 +301,36 @@
301
301
  "verify:precompile": "node scripts/verify-precompile-fresh.mjs"
302
302
  },
303
303
  "dependencies": {
304
- "@atlaskit/pragmatic-drag-and-drop": "^1.0.0",
305
- "@atlaskit/pragmatic-drag-and-drop-hitbox": "^1.0.0",
306
- "@tanstack/table-core": "^8.21.0",
307
- "@tanstack/virtual-core": "^3.13.0",
308
- "@zag-js/combobox": "^1",
309
- "@zag-js/dialog": "^1",
310
- "@zag-js/listbox": "^1",
311
- "@zag-js/menu": "^1",
312
- "@zag-js/popover": "^1",
313
- "@zag-js/popper": "^1",
314
- "@zag-js/radio-group": "^1",
315
- "@zag-js/select": "^1",
316
- "@zag-js/tabs": "^1",
317
- "@zag-js/tree-view": "^1",
318
- "@zag-js/vanilla": "^1",
319
- "diff": "^8.0.0",
320
- "jiti": "^1.21.7",
321
- "js-yaml": "^4.3.2",
304
+ "@atlaskit/pragmatic-drag-and-drop": "1.8.1",
305
+ "@atlaskit/pragmatic-drag-and-drop-hitbox": "1.2.0",
306
+ "@tanstack/table-core": "8.21.3",
307
+ "@tanstack/virtual-core": "3.19.7",
308
+ "@zag-js/combobox": "1.46.0",
309
+ "@zag-js/dialog": "1.46.0",
310
+ "@zag-js/listbox": "1.46.0",
311
+ "@zag-js/menu": "1.46.0",
312
+ "@zag-js/popover": "1.46.0",
313
+ "@zag-js/popper": "1.46.0",
314
+ "@zag-js/radio-group": "1.46.0",
315
+ "@zag-js/select": "1.46.0",
316
+ "@zag-js/tabs": "1.46.0",
317
+ "@zag-js/tree-view": "1.46.0",
318
+ "@zag-js/vanilla": "1.46.0",
319
+ "diff": "8.0.4",
320
+ "jiti": "1.21.7",
321
+ "js-yaml": "4.3.2",
322
322
  "parse5": "7.3.0",
323
- "qrcode-generator": "^2.0.0",
324
- "server-only": "^0.0.1",
323
+ "qrcode-generator": "2.0.4",
324
+ "server-only": "0.0.1",
325
325
  "soma-foundation": "^0.1.0",
326
- "temporal-polyfill": "^0.3.0"
326
+ "temporal-polyfill": "0.3.2"
327
327
  },
328
328
  "peerDependencies": {
329
329
  "echarts": "^6.0.0",
330
330
  "jsdom": "^26.1.0",
331
331
  "next": "16.4.1",
332
- "react": "^18.3.0",
333
- "react-dom": "^18.3.0",
332
+ "react": "18.3.1",
333
+ "react-dom": "18.3.1",
334
334
  "tailwindcss": "^4.1.0"
335
335
  },
336
336
  "peerDependenciesMeta": {
@@ -354,28 +354,28 @@
354
354
  }
355
355
  },
356
356
  "devDependencies": {
357
- "@types/dompurify": "^3.0.5",
358
- "@types/js-yaml": "^4.0.9",
359
- "@types/node": "^20.11.0",
360
- "@types/react": "^18.3.0",
361
- "@types/react-dom": "^18.3.0",
362
- "@types/ws": "^8.5.13",
363
- "@vitest/coverage-v8": "^3.2.7",
364
- "dompurify": "^3.4.17",
365
- "echarts": "^6.0.0",
366
- "esbuild": "^0.25.12",
367
- "jsdom": "^26.1.0",
357
+ "@types/dompurify": "3.0.5",
358
+ "@types/js-yaml": "4.0.9",
359
+ "@types/node": "20.19.40",
360
+ "@types/react": "18.3.31",
361
+ "@types/react-dom": "18.3.7",
362
+ "@types/ws": "8.18.1",
363
+ "@vitest/coverage-v8": "3.2.7",
364
+ "dompurify": "3.4.17",
365
+ "echarts": "6.1.0",
366
+ "esbuild": "0.25.12",
367
+ "jsdom": "26.1.0",
368
368
  "next": "16.4.1",
369
- "playwright": "^1.62.1",
370
- "react": "^18.3.0",
371
- "react-dom": "^18.3.0",
372
- "reflect-metadata": "^0.2.1",
373
- "tailwindcss": "^4.1.0",
374
- "tsx": "^4.23.13",
375
- "typescript": "^5.3.3",
369
+ "playwright": "1.62.1",
370
+ "react": "18.3.1",
371
+ "react-dom": "18.3.1",
372
+ "reflect-metadata": "0.2.2",
373
+ "tailwindcss": "4.3.3",
374
+ "tsx": "4.23.13",
375
+ "typescript": "5.9.3",
376
376
  "vite": "6.4.3",
377
- "vitest": "^3.2.7",
378
- "ws": "^8.21.0"
377
+ "vitest": "3.2.7",
378
+ "ws": "8.21.0"
379
379
  },
380
380
  "publishConfig": {
381
381
  "access": "public",