soma-core 2.1.1 → 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
|
-
|
|
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:
|
|
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):
|
|
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
|
-
|
|
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
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
}
|
package/host-next/next.config.js
CHANGED
|
@@ -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.
|
|
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",
|