@bleedingdev/modern-js-app-tools 3.5.0-ultramodern.30 → 3.5.0-ultramodern.31

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.
Files changed (116) hide show
  1. package/dist/cjs/plugins/backend-federation/codegen.js +208 -0
  2. package/dist/cjs/plugins/backend-federation/config.js +217 -0
  3. package/dist/cjs/plugins/backend-federation/emit.js +139 -0
  4. package/dist/cjs/plugins/backendFederationBuild.js +4 -298
  5. package/dist/cjs/plugins/deploy/platforms/cloudflare/artifacts.js +127 -0
  6. package/dist/cjs/plugins/deploy/platforms/cloudflare/constants.js +137 -0
  7. package/dist/cjs/plugins/deploy/platforms/cloudflare/delivery-unit.js +118 -0
  8. package/dist/cjs/plugins/deploy/platforms/cloudflare/i18n-worker.js +135 -0
  9. package/dist/cjs/plugins/deploy/platforms/cloudflare/index.js +126 -0
  10. package/dist/cjs/plugins/deploy/platforms/cloudflare/security-policies.js +223 -0
  11. package/dist/cjs/plugins/deploy/platforms/cloudflare/types.js +18 -0
  12. package/dist/cjs/plugins/deploy/platforms/cloudflare/utils.js +66 -0
  13. package/dist/cjs/plugins/deploy/platforms/cloudflare/worker-manifest.js +178 -0
  14. package/dist/cjs/plugins/deploy/platforms/cloudflare/wrangler-config.js +189 -0
  15. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/identity.js +83 -0
  16. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/index.js +184 -0
  17. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/issues.js +54 -0
  18. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/manifest.js +83 -0
  19. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/mutation-policy.js +158 -0
  20. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/worker-bundles.js +137 -0
  21. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier.js +23 -363
  22. package/dist/cjs/plugins/deploy/platforms/cloudflare.js +2 -745
  23. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.001-bootstrap-security.mjs +22 -0
  24. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.002-assets-routes.mjs +0 -0
  25. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.003-i18n-locales.mjs +0 -0
  26. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.004-rendering-css.mjs +0 -0
  27. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.005-worker-dispatch.mjs +0 -0
  28. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.006-fetch-handler.mjs +32 -0
  29. package/dist/esm/plugins/backend-federation/codegen.mjs +157 -0
  30. package/dist/esm/plugins/backend-federation/config.mjs +141 -0
  31. package/dist/esm/plugins/backend-federation/emit.mjs +88 -0
  32. package/dist/esm/plugins/backendFederationBuild.mjs +1 -296
  33. package/dist/esm/plugins/deploy/platforms/cloudflare/artifacts.mjs +64 -0
  34. package/dist/esm/plugins/deploy/platforms/cloudflare/constants.mjs +33 -0
  35. package/dist/esm/plugins/deploy/platforms/cloudflare/delivery-unit.mjs +67 -0
  36. package/dist/esm/plugins/deploy/platforms/cloudflare/i18n-worker.mjs +97 -0
  37. package/dist/esm/plugins/deploy/platforms/cloudflare/index.mjs +78 -0
  38. package/dist/esm/plugins/deploy/platforms/cloudflare/security-policies.mjs +167 -0
  39. package/dist/esm/plugins/deploy/platforms/cloudflare/types.mjs +0 -0
  40. package/dist/esm/plugins/deploy/platforms/cloudflare/utils.mjs +12 -0
  41. package/dist/esm/plugins/deploy/platforms/cloudflare/worker-manifest.mjs +118 -0
  42. package/dist/esm/plugins/deploy/platforms/cloudflare/wrangler-config.mjs +135 -0
  43. package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/identity.mjs +45 -0
  44. package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/index.mjs +129 -0
  45. package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/issues.mjs +10 -0
  46. package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/manifest.mjs +45 -0
  47. package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/mutation-policy.mjs +97 -0
  48. package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/worker-bundles.mjs +68 -0
  49. package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier.mjs +1 -347
  50. package/dist/esm/plugins/deploy/platforms/cloudflare.mjs +1 -735
  51. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.001-bootstrap-security.mjs +22 -0
  52. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.002-assets-routes.mjs +0 -0
  53. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.003-i18n-locales.mjs +0 -0
  54. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.004-rendering-css.mjs +0 -0
  55. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.005-worker-dispatch.mjs +0 -0
  56. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.006-fetch-handler.mjs +32 -0
  57. package/dist/esm-node/plugins/backend-federation/codegen.mjs +158 -0
  58. package/dist/esm-node/plugins/backend-federation/config.mjs +142 -0
  59. package/dist/esm-node/plugins/backend-federation/emit.mjs +89 -0
  60. package/dist/esm-node/plugins/backendFederationBuild.mjs +1 -296
  61. package/dist/esm-node/plugins/deploy/platforms/cloudflare/artifacts.mjs +65 -0
  62. package/dist/esm-node/plugins/deploy/platforms/cloudflare/constants.mjs +34 -0
  63. package/dist/esm-node/plugins/deploy/platforms/cloudflare/delivery-unit.mjs +68 -0
  64. package/dist/esm-node/plugins/deploy/platforms/cloudflare/i18n-worker.mjs +98 -0
  65. package/dist/esm-node/plugins/deploy/platforms/cloudflare/index.mjs +79 -0
  66. package/dist/esm-node/plugins/deploy/platforms/cloudflare/security-policies.mjs +168 -0
  67. package/dist/esm-node/plugins/deploy/platforms/cloudflare/types.mjs +1 -0
  68. package/dist/esm-node/plugins/deploy/platforms/cloudflare/utils.mjs +13 -0
  69. package/dist/esm-node/plugins/deploy/platforms/cloudflare/worker-manifest.mjs +119 -0
  70. package/dist/esm-node/plugins/deploy/platforms/cloudflare/wrangler-config.mjs +136 -0
  71. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/identity.mjs +46 -0
  72. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/index.mjs +130 -0
  73. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/issues.mjs +11 -0
  74. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/manifest.mjs +46 -0
  75. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/mutation-policy.mjs +98 -0
  76. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/worker-bundles.mjs +69 -0
  77. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier.mjs +1 -347
  78. package/dist/esm-node/plugins/deploy/platforms/cloudflare.mjs +1 -735
  79. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.001-bootstrap-security.mjs +22 -0
  80. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.002-assets-routes.mjs +0 -0
  81. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.003-i18n-locales.mjs +0 -0
  82. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.004-rendering-css.mjs +0 -0
  83. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.005-worker-dispatch.mjs +0 -0
  84. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.006-fetch-handler.mjs +32 -0
  85. package/dist/types/plugins/backend-federation/codegen.d.ts +82 -0
  86. package/dist/types/plugins/backend-federation/config.d.ts +103 -0
  87. package/dist/types/plugins/backend-federation/emit.d.ts +13 -0
  88. package/dist/types/plugins/backendFederationBuild.d.ts +2 -11
  89. package/dist/types/plugins/deploy/platforms/cloudflare/artifacts.d.ts +15 -0
  90. package/dist/types/plugins/deploy/platforms/cloudflare/constants.d.ts +23 -0
  91. package/dist/types/plugins/deploy/platforms/cloudflare/delivery-unit.d.ts +23 -0
  92. package/dist/types/plugins/deploy/platforms/cloudflare/i18n-worker.d.ts +21 -0
  93. package/dist/types/plugins/deploy/platforms/cloudflare/index.d.ts +2 -0
  94. package/dist/types/plugins/deploy/platforms/cloudflare/security-policies.d.ts +88 -0
  95. package/dist/types/plugins/deploy/platforms/cloudflare/types.d.ts +5 -0
  96. package/dist/types/plugins/deploy/platforms/cloudflare/utils.d.ts +6 -0
  97. package/dist/types/plugins/deploy/platforms/cloudflare/worker-manifest.d.ts +100 -0
  98. package/dist/types/plugins/deploy/platforms/cloudflare/wrangler-config.d.ts +39 -0
  99. package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/identity.d.ts +4 -0
  100. package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/index.d.ts +18 -0
  101. package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/issues.d.ts +14 -0
  102. package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/manifest.d.ts +2 -0
  103. package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/mutation-policy.d.ts +13 -0
  104. package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/worker-bundles.d.ts +16 -0
  105. package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier.d.ts +1 -32
  106. package/dist/types/plugins/deploy/platforms/cloudflare.d.ts +1 -2
  107. package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.001-bootstrap-security.d.mts +1 -0
  108. package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.002-assets-routes.d.mts +1 -0
  109. package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.003-i18n-locales.d.mts +1 -0
  110. package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.004-rendering-css.d.mts +1 -0
  111. package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.005-worker-dispatch.d.mts +1 -0
  112. package/package.json +11 -11
  113. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.mjs +0 -879
  114. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.mjs +0 -879
  115. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.mjs +0 -879
  116. /package/dist/types/plugins/deploy/platforms/templates/{cloudflare-entry.d.mts → cloudflare-entry.006-fetch-handler.d.mts} +0 -0
@@ -0,0 +1,22 @@
1
+ const MODERN_WORKER_MANIFEST = p_workerManifest;
2
+ p_workerModuleLoaders;
3
+ new Map();
4
+ new Map();
5
+ new Map();
6
+ const CORS_POLICY = MODERN_WORKER_MANIFEST.security?.cors || {};
7
+ CORS_POLICY.assets;
8
+ (CORS_POLICY.allowedOrigins || []).map((origin)=>String(origin).toLowerCase());
9
+ (CORS_POLICY.allowedMethods?.length ? CORS_POLICY.allowedMethods : [
10
+ 'GET',
11
+ 'HEAD',
12
+ 'POST',
13
+ 'PUT',
14
+ 'PATCH',
15
+ 'DELETE',
16
+ 'OPTIONS'
17
+ ]).join(', ');
18
+ (CORS_POLICY.allowedHeaders?.length ? CORS_POLICY.allowedHeaders : [
19
+ '*'
20
+ ]).join(', ');
21
+ globalThis.__dirname ??= '/';
22
+ globalThis.__filename ??= '/index.js';
@@ -0,0 +1,32 @@
1
+ export default {
2
+ async fetch (request, env, ctx) {
3
+ const corsPreflightResponse = await createCorsPreflightResponse(request, env);
4
+ if (corsPreflightResponse) return finalizeResponseForRequest(corsPreflightResponse, request);
5
+ const bffResponse = await dispatchBffRequest(request, env);
6
+ if (bffResponse) return finalizeResponseForRequest(withAppCorsHeaders(bffResponse, request), request);
7
+ const serviceBindingResponse = await dispatchServiceBindingRequest(request, env);
8
+ if (serviceBindingResponse) return finalizeResponseForRequest(withAppCorsHeaders(serviceBindingResponse, request), request);
9
+ const route = findRoute(request);
10
+ const { pathname } = new URL(request.url);
11
+ if (isAssetLikePathname(pathname) && !routeMatchesExactly(route, pathname)) {
12
+ const assetResponse = await fetchAsset(request, env);
13
+ if (assetResponse) return finalizeResponseForRequest(assetResponse, request);
14
+ return finalizeResponseForRequest(withAppCorsHeaders(new Response('Not found', {
15
+ status: 404
16
+ }), request), request);
17
+ }
18
+ const localeRedirectResponse = createLocaleRedirectResponseForRequest(route, request);
19
+ if (localeRedirectResponse) return finalizeResponseForRequest(withAppCorsHeaders(localeRedirectResponse, request), request);
20
+ if (route?.worker) {
21
+ const renderableRequest = createRenderableRequest(request);
22
+ return finalizeResponseForRequest(withAppCorsHeaders(await dispatchRouteWorker(route, renderableRequest, env, ctx), request), request);
23
+ }
24
+ const htmlResponse = await fetchRouteHtml(route, request, env);
25
+ if (htmlResponse) return finalizeResponseForRequest(htmlResponse, request);
26
+ const assetResponse = await fetchAsset(request, env);
27
+ if (assetResponse) return finalizeResponseForRequest(assetResponse, request);
28
+ return finalizeResponseForRequest(withAppCorsHeaders(new Response('Not found', {
29
+ status: 404
30
+ }), request), request);
31
+ }
32
+ };
@@ -0,0 +1,157 @@
1
+ import node_path from "node:path";
2
+ import { BACKEND_FEDERATION_CONTRACT_VERSION, BACKEND_FEDERATION_EFFECT_EXPOSE, BACKEND_FEDERATION_NODE_ADAPTER_VERSION, BACKEND_FEDERATION_REMOTE_ENTRY_FILE, formatBackendFederationValidationErrors, validateBackendFederationManifest } from "@modern-js/utils/universal";
3
+ import { normalizeRelativePath } from "./config.mjs";
4
+ const createRelativeImportSpecifier = (fromDirectory, toFile)=>{
5
+ const relativePath = normalizeRelativePath(node_path.relative(fromDirectory, toFile));
6
+ return relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
7
+ };
8
+ const createPublicPath = (containerEntry, fallbackPort)=>{
9
+ try {
10
+ return new URL('.', containerEntry).href;
11
+ } catch {
12
+ const slashIndex = containerEntry.lastIndexOf('/');
13
+ if (slashIndex >= 0) return containerEntry.slice(0, slashIndex + 1);
14
+ return `http://localhost:${fallbackPort}/`;
15
+ }
16
+ };
17
+ const createBackendRemoteEntrySource = (workspaceRoot, app, effectApiPath, entryPath)=>{
18
+ const effectApiSpecifier = createRelativeImportSpecifier(node_path.dirname(entryPath), effectApiPath);
19
+ return `// Generated by Modern.js backend federation build.
20
+ const exposedModules = {
21
+ ${JSON.stringify(BACKEND_FEDERATION_EFFECT_EXPOSE)}: () =>
22
+ import(new URL(${JSON.stringify(effectApiSpecifier)}, import.meta.url).href),
23
+ };
24
+
25
+ export const __modernjsBackendFederation = ${JSON.stringify({
26
+ buildVersion: app.buildVersion,
27
+ expose: BACKEND_FEDERATION_EFFECT_EXPOSE,
28
+ name: app.backendName,
29
+ packageName: app.packageName,
30
+ remoteType: app.remoteType,
31
+ source: normalizeRelativePath(node_path.relative(workspaceRoot, effectApiPath)),
32
+ version: app.version,
33
+ ...app.unitId ? {
34
+ unitId: app.unitId
35
+ } : {},
36
+ ...app.sourceRevision ? {
37
+ sourceRevision: app.sourceRevision
38
+ } : {}
39
+ }, null, 2)};
40
+
41
+ export function init() {}
42
+
43
+ export function get(id) {
44
+ const load = exposedModules[id];
45
+ if (!load) {
46
+ throw new Error(\`Unexpected backend federation expose: \${id}\`);
47
+ }
48
+
49
+ return async () => load();
50
+ }
51
+
52
+ export default { get, init };
53
+ `;
54
+ };
55
+ const createBackendManifest = (workspaceRoot, distDirectory, app)=>{
56
+ const sourceModule = `${app.directory}/api/effect-api.ts`;
57
+ const publicPath = createPublicPath(app.containerEntry, app.port);
58
+ const manifest = {
59
+ schemaVersion: 1,
60
+ id: app.backendName,
61
+ name: app.backendName,
62
+ version: app.version,
63
+ buildVersion: app.buildVersion,
64
+ metaData: {
65
+ name: app.backendName,
66
+ type: 'backend',
67
+ buildInfo: {
68
+ buildVersion: app.buildVersion,
69
+ buildName: app.packageName
70
+ },
71
+ remoteEntry: {
72
+ name: BACKEND_FEDERATION_REMOTE_ENTRY_FILE,
73
+ path: '',
74
+ type: app.remoteType
75
+ },
76
+ globalName: app.backendName,
77
+ publicPath,
78
+ ssrRemoteEntry: {
79
+ name: BACKEND_FEDERATION_REMOTE_ENTRY_FILE,
80
+ path: '',
81
+ type: app.remoteType
82
+ },
83
+ ssrPublicPath: publicPath
84
+ },
85
+ entry: {
86
+ file: BACKEND_FEDERATION_REMOTE_ENTRY_FILE,
87
+ path: normalizeRelativePath(node_path.relative(workspaceRoot, node_path.join(distDirectory, BACKEND_FEDERATION_REMOTE_ENTRY_FILE))),
88
+ type: app.remoteType,
89
+ url: app.containerEntry
90
+ },
91
+ exposes: [
92
+ {
93
+ id: `${app.backendName}:${BACKEND_FEDERATION_EFFECT_EXPOSE}`,
94
+ name: BACKEND_FEDERATION_EFFECT_EXPOSE,
95
+ path: '',
96
+ assets: {
97
+ js: {
98
+ async: [],
99
+ sync: [
100
+ BACKEND_FEDERATION_REMOTE_ENTRY_FILE
101
+ ]
102
+ },
103
+ css: {
104
+ async: [],
105
+ sync: []
106
+ }
107
+ }
108
+ }
109
+ ],
110
+ shared: [],
111
+ backendFederation: {
112
+ role: 'microvertical-server',
113
+ name: app.backendName,
114
+ runtimeFramework: 'effect',
115
+ strictEffectApproach: true,
116
+ contractVersion: BACKEND_FEDERATION_CONTRACT_VERSION,
117
+ nodeAdapterVersion: BACKEND_FEDERATION_NODE_ADAPTER_VERSION,
118
+ remoteType: app.remoteType,
119
+ expose: BACKEND_FEDERATION_EFFECT_EXPOSE,
120
+ manifestUrl: app.manifestUrl,
121
+ containerEntry: app.containerEntry,
122
+ source: {
123
+ module: sourceModule
124
+ },
125
+ readinessPath: `${app.apiPrefix}/${app.apiStem}/readiness`,
126
+ openapiPath: `${app.apiPrefix}/openapi.json`,
127
+ ...app.deliveryUnit ? {
128
+ deliveryUnit: app.deliveryUnit
129
+ } : {},
130
+ versionBoundary: {
131
+ invariant: 'web-and-api-same-build',
132
+ packageName: app.packageName,
133
+ version: app.version,
134
+ buildVersion: app.buildVersion,
135
+ ...app.uiManifestUrl ? {
136
+ uiManifestUrl: app.uiManifestUrl
137
+ } : {},
138
+ ...app.deliveryUnit ? {
139
+ deliveryUnit: {
140
+ unitId: app.deliveryUnit.unitId,
141
+ buildMarker: app.deliveryUnit.buildMarker,
142
+ sourceRevision: app.deliveryUnit.sourceRevision
143
+ }
144
+ } : {}
145
+ }
146
+ }
147
+ };
148
+ const validation = validateBackendFederationManifest(manifest, {
149
+ path: 'backendFederationManifest',
150
+ requireEffectExpose: true,
151
+ requireEffectRuntime: true,
152
+ requireVersionFields: true
153
+ });
154
+ if (!validation.ok) throw new Error(`[backend-federation-build] Invalid backend federation manifest: ${formatBackendFederationValidationErrors(validation.errors)}.`);
155
+ return manifest;
156
+ };
157
+ export { createBackendManifest, createBackendRemoteEntrySource };
@@ -0,0 +1,141 @@
1
+ import { execFile } from "node:child_process";
2
+ import { existsSync } from "node:fs";
3
+ import promises from "node:fs/promises";
4
+ import node_path from "node:path";
5
+ import { promisify } from "node:util";
6
+ import { BACKEND_FEDERATION_MANIFEST_FILE, BACKEND_FEDERATION_REMOTE_ENTRY_FILE, DELIVERY_UNIT_DEPLOY_PROFILE, DELIVERY_UNIT_KIND, DELIVERY_UNIT_SCHEMA_VERSION, ULTRAMODERN_BUILD_ARTIFACT_FILE, ULTRAMODERN_BUILD_ARTIFACT_PATH, ULTRAMODERN_BUILD_MODULE_PATH, deliveryUnitContractBlock, validateDeliveryUnitRecord, validateUltramodernBuildArtifact } from "@modern-js/utils/universal";
7
+ const COMPACT_CONFIG_PATH = '.modernjs/ultramodern.json';
8
+ const execFileAsync = promisify(execFile);
9
+ const normalizeRelativePath = (value)=>value.replace(/\\/gu, '/').replace(/^\.\/+/u, '');
10
+ const isRecord = (value)=>'object' == typeof value && null !== value;
11
+ const readJsonFile = async (filePath)=>JSON.parse(await promises.readFile(filePath, 'utf8'));
12
+ const buildArtifactPathFor = (appDirectory)=>node_path.join(appDirectory, ULTRAMODERN_BUILD_ARTIFACT_PATH);
13
+ const buildModulePathFor = (appDirectory)=>node_path.join(appDirectory, ULTRAMODERN_BUILD_MODULE_PATH);
14
+ const readBuildIdentity = async (appDirectory)=>{
15
+ const buildArtifactPath = buildArtifactPathFor(appDirectory);
16
+ if (existsSync(buildArtifactPath)) {
17
+ const artifact = await readJsonFile(buildArtifactPath);
18
+ const artifactValidation = validateUltramodernBuildArtifact(artifact, buildArtifactPath);
19
+ if (!artifactValidation.ok) throw new Error(`[backend-federation-build] Invalid delivery-unit build artifact at ${buildArtifactPath}.`);
20
+ const deliveryUnit = artifact.deliveryUnit;
21
+ return {
22
+ artifact,
23
+ appId: deliveryUnit.appId,
24
+ buildVersion: deliveryUnit.buildMarker,
25
+ packageName: deliveryUnit.packageName,
26
+ version: deliveryUnit.version,
27
+ unitId: deliveryUnit.unitId,
28
+ sourceRevision: deliveryUnit.sourceRevision
29
+ };
30
+ }
31
+ const buildModulePath = buildModulePathFor(appDirectory);
32
+ if (!existsSync(buildModulePath)) return {};
33
+ console.warn(`[backend-federation-build] ${buildArtifactPath} missing; falling back to legacy regex parsing of ${buildModulePath}. Regenerate the workspace to emit ${ULTRAMODERN_BUILD_ARTIFACT_FILE}.`);
34
+ const source = await promises.readFile(buildModulePath, 'utf8');
35
+ return {
36
+ appId: source.match(/\bappId:\s*['"]([^'"]+)['"]/u)?.[1],
37
+ buildVersion: source.match(/\bbuild:\s*['"]([^'"]+)['"]/u)?.[1],
38
+ packageName: source.match(/\bpackageName:\s*['"]([^'"]+)['"]/u)?.[1],
39
+ version: source.match(/\bversion:\s*['"]([^'"]+)['"]/u)?.[1],
40
+ unitId: source.match(/\bunitId:\s*['"]([^'"]+)['"]/u)?.[1],
41
+ sourceRevision: source.match(/\bsourceRevision:\s*['"]([^'"]+)['"]/u)?.[1]
42
+ };
43
+ };
44
+ const toPascalCase = (value)=>value.split(/[^a-zA-Z0-9]+/u).filter(Boolean).map((part)=>part.charAt(0).toUpperCase() + part.slice(1)).join('');
45
+ const stringValue = (value)=>'string' == typeof value && value.length > 0 ? value : void 0;
46
+ const createBackendName = (app, id)=>{
47
+ const configuredName = stringValue(app.backendFederation?.name) ?? stringValue(app.backendFederation?.executionSurfaces?.node?.remoteName) ?? stringValue(app.serverExecution?.node?.remoteName);
48
+ if (configuredName) return configuredName;
49
+ const mfName = stringValue(app.moduleFederation?.name);
50
+ return mfName ? `${mfName}Backend` : `vertical${toPascalCase(id)}Backend`;
51
+ };
52
+ const findWorkspaceRoot = (appDirectory)=>{
53
+ let current = appDirectory;
54
+ while(true){
55
+ if (existsSync(node_path.join(current, COMPACT_CONFIG_PATH))) return current;
56
+ const parent = node_path.dirname(current);
57
+ if (parent === current) return;
58
+ current = parent;
59
+ }
60
+ };
61
+ const resolveWorkspaceSourceRevision = async (workspaceRoot)=>{
62
+ const envRevision = process.env.ULTRAMODERN_SOURCE_REVISION?.trim();
63
+ if (envRevision) return envRevision;
64
+ try {
65
+ const { stdout } = await execFileAsync('git', [
66
+ '-C',
67
+ workspaceRoot,
68
+ 'rev-parse',
69
+ 'HEAD'
70
+ ]);
71
+ const revision = stdout.trim();
72
+ if (revision.length > 0) return revision;
73
+ } catch {}
74
+ console.warn('[backend-federation-build] Could not resolve git source revision; stamping sourceRevision as "workspace". Set ULTRAMODERN_SOURCE_REVISION to override.');
75
+ return 'workspace';
76
+ };
77
+ const createStampedDeliveryUnit = (input)=>{
78
+ if (!input.unitId || !input.buildMarker || !input.packageName || !input.version) return;
79
+ const record = {
80
+ schemaVersion: DELIVERY_UNIT_SCHEMA_VERSION,
81
+ kind: DELIVERY_UNIT_KIND,
82
+ appId: input.appId,
83
+ unitId: input.unitId,
84
+ packageName: input.packageName,
85
+ version: input.version,
86
+ buildMarker: input.buildMarker,
87
+ sourceRevision: input.sourceRevision,
88
+ deployProfile: DELIVERY_UNIT_DEPLOY_PROFILE
89
+ };
90
+ const validation = validateDeliveryUnitRecord(record);
91
+ if (!validation.ok) return;
92
+ return deliveryUnitContractBlock(record);
93
+ };
94
+ const createAppFromCompactMetadata = (workspaceRoot, appDirectory, compactApp)=>{
95
+ const id = stringValue(compactApp.id);
96
+ const appPath = stringValue(compactApp.path);
97
+ if (!id || 'vertical' !== compactApp.kind || !isRecord(compactApp.api)) return;
98
+ const directory = appPath ? normalizeRelativePath(appPath) : `verticals/${id}`;
99
+ if (node_path.resolve(workspaceRoot, directory) !== node_path.resolve(appDirectory)) return;
100
+ const port = 'number' == typeof compactApp.port ? compactApp.port : void 0;
101
+ if (void 0 === port) return;
102
+ const apiPrefix = stringValue(compactApp.api.prefix) ?? `/${id}-api`;
103
+ const apiStem = stringValue(compactApp.api.stem) ?? id;
104
+ const backendName = createBackendName(compactApp, id);
105
+ const manifestUrl = stringValue(compactApp.backendFederation?.executionSurfaces?.node?.manifestUrl) ?? stringValue(compactApp.serverExecution?.node?.manifestUrl) ?? `http://localhost:${port}/${BACKEND_FEDERATION_MANIFEST_FILE}`;
106
+ const containerEntry = stringValue(compactApp.backendFederation?.executionSurfaces?.node?.containerEntry) ?? stringValue(compactApp.serverExecution?.node?.containerEntry) ?? `http://localhost:${port}/${BACKEND_FEDERATION_REMOTE_ENTRY_FILE}`;
107
+ const remoteType = stringValue(compactApp.backendFederation?.executionSurfaces?.node?.remoteType) ?? stringValue(compactApp.serverExecution?.node?.remoteType) ?? 'module';
108
+ const packageName = stringValue(compactApp.package);
109
+ const uiManifestUrl = stringValue(compactApp.backendFederation?.versionBoundary?.ui?.manifestUrl) ?? stringValue(compactApp.moduleFederation?.manifestUrl);
110
+ const compactDeliveryUnit = isRecord(compactApp.deliveryUnit) ? {
111
+ unitId: stringValue(compactApp.deliveryUnit.unitId),
112
+ buildMarker: stringValue(compactApp.deliveryUnit.buildMarker),
113
+ sourceRevision: stringValue(compactApp.deliveryUnit.sourceRevision),
114
+ packageName: stringValue(compactApp.deliveryUnit.packageName),
115
+ version: stringValue(compactApp.deliveryUnit.version)
116
+ } : void 0;
117
+ return {
118
+ id,
119
+ directory,
120
+ packageName,
121
+ port,
122
+ apiPrefix,
123
+ apiStem,
124
+ backendName,
125
+ manifestUrl,
126
+ containerEntry,
127
+ remoteType,
128
+ uiManifestUrl,
129
+ compactDeliveryUnit
130
+ };
131
+ };
132
+ const findBackendFederationApp = async (workspaceRoot, appDirectory)=>{
133
+ const compactConfigPath = node_path.join(workspaceRoot, COMPACT_CONFIG_PATH);
134
+ const compactConfig = await readJsonFile(compactConfigPath);
135
+ const apps = Array.isArray(compactConfig.topology?.apps) ? compactConfig.topology.apps : [];
136
+ for (const app of apps){
137
+ const resolved = createAppFromCompactMetadata(workspaceRoot, appDirectory, app);
138
+ if (resolved) return resolved;
139
+ }
140
+ };
141
+ export { COMPACT_CONFIG_PATH, buildArtifactPathFor, buildModulePathFor, createAppFromCompactMetadata, createStampedDeliveryUnit, findBackendFederationApp, findWorkspaceRoot, normalizeRelativePath, readBuildIdentity, resolveWorkspaceSourceRevision };
@@ -0,0 +1,88 @@
1
+ import { existsSync } from "node:fs";
2
+ import promises from "node:fs/promises";
3
+ import node_path from "node:path";
4
+ import { BACKEND_FEDERATION_MANIFEST_FILE, BACKEND_FEDERATION_REMOTE_ENTRY_FILE, ULTRAMODERN_BUILD_ARTIFACT_FILE, stampUltramodernBuildArtifactSourceRevision } from "@modern-js/utils/universal";
5
+ import { createBackendManifest, createBackendRemoteEntrySource } from "./codegen.mjs";
6
+ import { COMPACT_CONFIG_PATH, buildArtifactPathFor, buildModulePathFor, createStampedDeliveryUnit, findBackendFederationApp, findWorkspaceRoot, readBuildIdentity, resolveWorkspaceSourceRevision } from "./config.mjs";
7
+ const emitBackendFederationArtifacts = async (appDirectory, distDirectory)=>{
8
+ const workspaceRoot = findWorkspaceRoot(appDirectory);
9
+ if (!workspaceRoot) return;
10
+ const effectApiPath = node_path.join(appDirectory, 'api/effect-api.ts');
11
+ const backendFederationConfigPath = node_path.join(appDirectory, 'backend-federation.config.ts');
12
+ if (!existsSync(effectApiPath) || !existsSync(backendFederationConfigPath)) return;
13
+ const app = await findBackendFederationApp(workspaceRoot, appDirectory);
14
+ if (!app) return;
15
+ const buildIdentity = await readBuildIdentity(appDirectory);
16
+ const compactDeliveryUnit = app.compactDeliveryUnit;
17
+ const hasCompactDeliveryUnit = void 0 !== compactDeliveryUnit && (void 0 !== compactDeliveryUnit.unitId || void 0 !== compactDeliveryUnit.buildMarker || void 0 !== compactDeliveryUnit.packageName || void 0 !== compactDeliveryUnit.version);
18
+ const hasBuildIdentity = void 0 !== buildIdentity.unitId || void 0 !== buildIdentity.buildVersion || void 0 !== buildIdentity.packageName || void 0 !== buildIdentity.version;
19
+ if (hasBuildIdentity) {
20
+ const compactConfigPath = node_path.join(workspaceRoot, COMPACT_CONFIG_PATH);
21
+ const buildIdentityPath = existsSync(buildArtifactPathFor(appDirectory)) ? buildArtifactPathFor(appDirectory) : buildModulePathFor(appDirectory);
22
+ const mismatches = [];
23
+ const compare = (label, a, b, leftLabel = 'deliveryUnit')=>{
24
+ if (void 0 !== a && void 0 !== b && a !== b) mismatches.push(`${label}: ${leftLabel}=${a} vs ultramodern-build=${b}`);
25
+ };
26
+ compare('appId', app.id, buildIdentity.appId, 'topology');
27
+ if (hasCompactDeliveryUnit) {
28
+ compare('unitId', compactDeliveryUnit?.unitId, buildIdentity.unitId);
29
+ compare('buildMarker/build', compactDeliveryUnit?.buildMarker, buildIdentity.buildVersion);
30
+ compare('packageName', compactDeliveryUnit?.packageName, buildIdentity.packageName);
31
+ compare('version', compactDeliveryUnit?.version, buildIdentity.version);
32
+ }
33
+ if (mismatches.length > 0) throw new Error(`[backend-federation-build] Delivery-unit identity drift between ${compactConfigPath} (topology) and ${buildIdentityPath}: ${mismatches.join('; ')}`);
34
+ }
35
+ const unitId = compactDeliveryUnit?.unitId ?? buildIdentity.unitId;
36
+ const buildVersion = compactDeliveryUnit?.buildMarker ?? buildIdentity.buildVersion;
37
+ const sourceRevision = await resolveWorkspaceSourceRevision(workspaceRoot);
38
+ const packageName = compactDeliveryUnit?.packageName ?? buildIdentity.packageName ?? app.packageName;
39
+ const version = compactDeliveryUnit?.version ?? buildIdentity.version;
40
+ const deliveryUnit = createStampedDeliveryUnit({
41
+ appId: app.id,
42
+ unitId,
43
+ buildMarker: buildVersion,
44
+ packageName,
45
+ version,
46
+ sourceRevision
47
+ });
48
+ const stampedBuildArtifact = buildIdentity.artifact ? stampUltramodernBuildArtifactSourceRevision(buildIdentity.artifact, sourceRevision) : void 0;
49
+ const resolvedApp = {
50
+ ...app,
51
+ packageName,
52
+ version,
53
+ buildVersion,
54
+ unitId,
55
+ sourceRevision,
56
+ deliveryUnit
57
+ };
58
+ const manifestPath = node_path.join(distDirectory, BACKEND_FEDERATION_MANIFEST_FILE);
59
+ const entryPath = node_path.join(distDirectory, BACKEND_FEDERATION_REMOTE_ENTRY_FILE);
60
+ const deliveryUnitArtifactPath = stampedBuildArtifact ? node_path.join(distDirectory, ULTRAMODERN_BUILD_ARTIFACT_FILE) : void 0;
61
+ await promises.mkdir(distDirectory, {
62
+ recursive: true
63
+ });
64
+ await promises.writeFile(entryPath, createBackendRemoteEntrySource(workspaceRoot, resolvedApp, effectApiPath, entryPath));
65
+ await promises.writeFile(manifestPath, `${JSON.stringify(createBackendManifest(workspaceRoot, distDirectory, resolvedApp), null, 2)}\n`);
66
+ if (stampedBuildArtifact && deliveryUnitArtifactPath) await promises.writeFile(deliveryUnitArtifactPath, `${JSON.stringify(stampedBuildArtifact, null, 2)}\n`);
67
+ return {
68
+ appId: resolvedApp.id,
69
+ manifestPath,
70
+ containerPath: entryPath,
71
+ ...deliveryUnitArtifactPath ? {
72
+ deliveryUnitArtifactPath
73
+ } : {},
74
+ remoteName: resolvedApp.backendName,
75
+ remoteType: resolvedApp.remoteType
76
+ };
77
+ };
78
+ const emit = ()=>({
79
+ name: '@modern-js/backend-federation-build',
80
+ setup (api) {
81
+ api.onAfterBuild(async ()=>{
82
+ const { appDirectory, distDirectory } = api.getAppContext();
83
+ await emitBackendFederationArtifacts(appDirectory, distDirectory);
84
+ });
85
+ }
86
+ });
87
+ export default emit;
88
+ export { emitBackendFederationArtifacts };