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

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