@bleedingdev/modern-js-app-tools 3.5.0-ultramodern.3 → 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.
- package/dist/cjs/builder/generator/getBuilderEnvironments.js +1 -0
- package/dist/cjs/index.js +50 -44
- package/dist/cjs/plugins/backend-federation/codegen.js +208 -0
- package/dist/cjs/plugins/backend-federation/config.js +217 -0
- package/dist/cjs/plugins/backend-federation/emit.js +139 -0
- package/dist/cjs/plugins/backendFederationBuild.js +53 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare/artifacts.js +127 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare/constants.js +137 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare/delivery-unit.js +118 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare/i18n-worker.js +135 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare/index.js +126 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare/security-policies.js +223 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare/types.js +18 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare/utils.js +66 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare/worker-manifest.js +178 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare/wrangler-config.js +189 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare-output-contract.js +91 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare-output-plan.js +93 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/identity.js +83 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/index.js +184 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/issues.js +54 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/manifest.js +83 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/mutation-policy.js +158 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/worker-bundles.js +137 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier.js +62 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare.js +2 -502
- package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.001-bootstrap-security.mjs +22 -0
- package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.002-assets-routes.mjs +0 -0
- package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.003-i18n-locales.mjs +0 -0
- package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.004-rendering-css.mjs +0 -0
- package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.005-worker-dispatch.mjs +0 -0
- package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.006-fetch-handler.mjs +32 -0
- package/dist/esm/builder/generator/getBuilderEnvironments.mjs +1 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/plugins/backend-federation/codegen.mjs +157 -0
- package/dist/esm/plugins/backend-federation/config.mjs +141 -0
- package/dist/esm/plugins/backend-federation/emit.mjs +88 -0
- package/dist/esm/plugins/backendFederationBuild.mjs +1 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare/artifacts.mjs +64 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare/constants.mjs +33 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare/delivery-unit.mjs +67 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare/i18n-worker.mjs +97 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare/index.mjs +78 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare/security-policies.mjs +167 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare/types.mjs +0 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare/utils.mjs +12 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare/worker-manifest.mjs +118 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare/wrangler-config.mjs +135 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare-output-contract.mjs +17 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare-output-plan.mjs +45 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/identity.mjs +45 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/index.mjs +129 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/issues.mjs +10 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/manifest.mjs +45 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/mutation-policy.mjs +97 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/worker-bundles.mjs +68 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier.mjs +1 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare.mjs +1 -492
- package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.001-bootstrap-security.mjs +22 -0
- package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.002-assets-routes.mjs +0 -0
- package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.003-i18n-locales.mjs +0 -0
- package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.004-rendering-css.mjs +0 -0
- package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.005-worker-dispatch.mjs +0 -0
- package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.006-fetch-handler.mjs +32 -0
- package/dist/esm-node/builder/generator/getBuilderEnvironments.mjs +1 -0
- package/dist/esm-node/index.mjs +2 -0
- package/dist/esm-node/plugins/backend-federation/codegen.mjs +158 -0
- package/dist/esm-node/plugins/backend-federation/config.mjs +142 -0
- package/dist/esm-node/plugins/backend-federation/emit.mjs +89 -0
- package/dist/esm-node/plugins/backendFederationBuild.mjs +2 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare/artifacts.mjs +65 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare/constants.mjs +34 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare/delivery-unit.mjs +68 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare/i18n-worker.mjs +98 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare/index.mjs +79 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare/security-policies.mjs +168 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare/types.mjs +1 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare/utils.mjs +13 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare/worker-manifest.mjs +119 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare/wrangler-config.mjs +136 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-contract.mjs +18 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-plan.mjs +46 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/identity.mjs +46 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/index.mjs +130 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/issues.mjs +11 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/manifest.mjs +46 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/mutation-policy.mjs +98 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/worker-bundles.mjs +69 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier.mjs +2 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare.mjs +1 -492
- package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.001-bootstrap-security.mjs +22 -0
- package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.002-assets-routes.mjs +0 -0
- package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.003-i18n-locales.mjs +0 -0
- package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.004-rendering-css.mjs +0 -0
- package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.005-worker-dispatch.mjs +0 -0
- package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.006-fetch-handler.mjs +32 -0
- package/dist/types/plugins/backend-federation/codegen.d.ts +82 -0
- package/dist/types/plugins/backend-federation/config.d.ts +103 -0
- package/dist/types/plugins/backend-federation/emit.d.ts +13 -0
- package/dist/types/plugins/backendFederationBuild.d.ts +2 -0
- package/dist/types/plugins/deploy/platforms/cloudflare/artifacts.d.ts +15 -0
- package/dist/types/plugins/deploy/platforms/cloudflare/constants.d.ts +23 -0
- package/dist/types/plugins/deploy/platforms/cloudflare/delivery-unit.d.ts +23 -0
- package/dist/types/plugins/deploy/platforms/cloudflare/i18n-worker.d.ts +21 -0
- package/dist/types/plugins/deploy/platforms/cloudflare/index.d.ts +2 -0
- package/dist/types/plugins/deploy/platforms/cloudflare/security-policies.d.ts +88 -0
- package/dist/types/plugins/deploy/platforms/cloudflare/types.d.ts +5 -0
- package/dist/types/plugins/deploy/platforms/cloudflare/utils.d.ts +6 -0
- package/dist/types/plugins/deploy/platforms/cloudflare/worker-manifest.d.ts +100 -0
- package/dist/types/plugins/deploy/platforms/cloudflare/wrangler-config.d.ts +39 -0
- package/dist/types/plugins/deploy/platforms/cloudflare-output-contract.d.ts +13 -0
- package/dist/types/plugins/deploy/platforms/cloudflare-output-plan.d.ts +33 -0
- package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/identity.d.ts +4 -0
- package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/index.d.ts +18 -0
- package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/issues.d.ts +14 -0
- package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/manifest.d.ts +2 -0
- package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/mutation-policy.d.ts +13 -0
- package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/worker-bundles.d.ts +16 -0
- package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier.d.ts +1 -0
- package/dist/types/plugins/deploy/platforms/cloudflare.d.ts +1 -2
- package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.001-bootstrap-security.d.mts +1 -0
- package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.002-assets-routes.d.mts +1 -0
- package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.003-i18n-locales.d.mts +1 -0
- package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.004-rendering-css.d.mts +1 -0
- package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.005-worker-dispatch.d.mts +1 -0
- package/dist/types/types/config/cloudflareDeploy.d.ts +22 -0
- package/dist/types/types/config/deploy.d.ts +15 -2
- package/lib/types.d.ts +2 -3
- package/package.json +17 -11
- package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.mjs +0 -576
- package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.mjs +0 -576
- package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.mjs +0 -576
- /package/dist/types/plugins/deploy/platforms/templates/{cloudflare-entry.d.mts → cloudflare-entry.006-fetch-handler.d.mts} +0 -0
|
@@ -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 };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import node_path from "node:path";
|
|
3
|
+
import { fs } from "@modern-js/utils";
|
|
4
|
+
import { RESERVED_ARTIFACT_DESTINATION_DIRECTORIES, RESERVED_ARTIFACT_DESTINATION_FILES, ROUTE_SPEC_OUTPUT } from "./constants.mjs";
|
|
5
|
+
import { normalizeRelativePath } from "./utils.mjs";
|
|
6
|
+
const normalizeCloudflareArtifact = (artifact, index)=>{
|
|
7
|
+
const from = normalizeRelativePath(artifact.from, `deploy.worker.artifacts[${index}].from`, 'app root');
|
|
8
|
+
const to = normalizeRelativePath(artifact.to, `deploy.worker.artifacts[${index}].to`, 'Cloudflare output');
|
|
9
|
+
const [topLevelDestination] = to.split('/');
|
|
10
|
+
const reservedDestination = RESERVED_ARTIFACT_DESTINATION_FILES.has(to) ? to : topLevelDestination;
|
|
11
|
+
if (RESERVED_ARTIFACT_DESTINATION_FILES.has(to) || RESERVED_ARTIFACT_DESTINATION_DIRECTORIES.has(topLevelDestination)) throw new Error(`deploy.worker.artifacts[${index}].to must not target generated Cloudflare output path ${JSON.stringify(reservedDestination)}.`);
|
|
12
|
+
return {
|
|
13
|
+
from,
|
|
14
|
+
to,
|
|
15
|
+
index
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
const getCloudflareArtifacts = (modernConfig)=>(modernConfig.deploy?.worker?.artifacts ?? []).map(normalizeCloudflareArtifact);
|
|
19
|
+
const normalizeCloudflarePublicAsset = (asset, index)=>{
|
|
20
|
+
const from = normalizeRelativePath(asset.from, `deploy.worker.publicAssets[${index}].from`, 'app root');
|
|
21
|
+
const to = normalizeRelativePath(asset.to, `deploy.worker.publicAssets[${index}].to`, 'Cloudflare public output', {
|
|
22
|
+
allowRoot: true
|
|
23
|
+
});
|
|
24
|
+
return {
|
|
25
|
+
from,
|
|
26
|
+
to,
|
|
27
|
+
index
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
const getCloudflarePublicAssets = (modernConfig)=>(modernConfig.deploy?.worker?.publicAssets ?? []).map(normalizeCloudflarePublicAsset);
|
|
31
|
+
const copyCloudflareArtifacts = async (appDirectory, outputDirectory, artifacts)=>{
|
|
32
|
+
for (const artifact of artifacts){
|
|
33
|
+
const sourcePath = node_path.join(appDirectory, artifact.from);
|
|
34
|
+
if (!await fs.pathExists(sourcePath)) throw new Error(`deploy.worker.artifacts[${artifact.index}].from does not exist: ${artifact.from}`);
|
|
35
|
+
await fs.copy(sourcePath, node_path.join(outputDirectory, artifact.to));
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const copyCloudflarePublicAssets = async (appDirectory, publicDirectory, publicAssets)=>{
|
|
39
|
+
for (const asset of publicAssets){
|
|
40
|
+
const sourcePath = node_path.join(appDirectory, asset.from);
|
|
41
|
+
if (!await fs.pathExists(sourcePath)) throw new Error(`deploy.worker.publicAssets[${asset.index}].from does not exist: ${asset.from}`);
|
|
42
|
+
await fs.copy(sourcePath, node_path.join(publicDirectory, asset.to));
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
const copyCloudflareD1Migrations = async (appDirectory, outputDirectory, modernConfig)=>{
|
|
46
|
+
for (const [index, database] of (modernConfig.deploy?.worker?.d1Databases ?? []).entries()){
|
|
47
|
+
if (!database.migrationsDir) continue;
|
|
48
|
+
const migrationsDir = normalizeRelativePath(database.migrationsDir, `deploy.worker.d1Databases[${index}].migrationsDir`, 'app root');
|
|
49
|
+
const sourcePath = node_path.join(appDirectory, migrationsDir);
|
|
50
|
+
if (!await fs.pathExists(sourcePath)) throw new Error(`deploy.worker.d1Databases[${index}].migrationsDir does not exist: ${migrationsDir}`);
|
|
51
|
+
await fs.copy(sourcePath, node_path.join(outputDirectory, migrationsDir));
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const readRouteSpec = async (outputDirectory)=>{
|
|
55
|
+
const routeSpecPath = node_path.join(outputDirectory, ROUTE_SPEC_OUTPUT);
|
|
56
|
+
if (!await fs.pathExists(routeSpecPath)) return {
|
|
57
|
+
routes: []
|
|
58
|
+
};
|
|
59
|
+
const routeSpec = await fs.readJSON(routeSpecPath);
|
|
60
|
+
return {
|
|
61
|
+
...routeSpec,
|
|
62
|
+
routes: Array.isArray(routeSpec.routes) ? routeSpec.routes : []
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
export { copyCloudflareArtifacts, copyCloudflareD1Migrations, copyCloudflarePublicAssets, getCloudflareArtifacts, getCloudflarePublicAssets, readRouteSpec };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { CLOUDFLARE_ASSETS_BINDING, CLOUDFLARE_OUTPUT_PACKAGE_FILE, CLOUDFLARE_PUBLIC_ASSETS_DIRECTORY, CLOUDFLARE_REQUIRED_COMPATIBILITY_FLAGS, CLOUDFLARE_RUNTIME_TYPE, CLOUDFLARE_WORKER_BUNDLE_DIRECTORY, CLOUDFLARE_WORKER_BUNDLE_FORMAT, CLOUDFLARE_WORKER_ENTRY, CLOUDFLARE_WORKER_MANIFEST, CLOUDFLARE_WRANGLER_CONFIG_FILE } from "../cloudflare-output-contract.mjs";
|
|
3
|
+
const WORKER_ENTRY = CLOUDFLARE_WORKER_ENTRY;
|
|
4
|
+
const WORKER_MANIFEST = CLOUDFLARE_WORKER_MANIFEST;
|
|
5
|
+
const WRANGLER_CONFIG_FILE = CLOUDFLARE_WRANGLER_CONFIG_FILE;
|
|
6
|
+
const OUTPUT_PACKAGE_FILE = CLOUDFLARE_OUTPUT_PACKAGE_FILE;
|
|
7
|
+
const ASSETS_BINDING = CLOUDFLARE_ASSETS_BINDING;
|
|
8
|
+
const ROUTE_SPEC_FILE = 'route.json';
|
|
9
|
+
const ROUTE_SPEC_OUTPUT = `server/${ROUTE_SPEC_FILE}`;
|
|
10
|
+
const LOADABLE_STATS_FILE = 'loadable-stats.json';
|
|
11
|
+
const ROUTE_MANIFEST_FILE = 'routes-manifest.json';
|
|
12
|
+
const PUBLIC_ASSETS_DIRECTORY = CLOUDFLARE_PUBLIC_ASSETS_DIRECTORY;
|
|
13
|
+
const WORKER_BUNDLE_DIRECTORY = CLOUDFLARE_WORKER_BUNDLE_DIRECTORY;
|
|
14
|
+
const SERVER_BUNDLE_DIRECTORY = 'bundles';
|
|
15
|
+
const SERVER_OUTPUT_DIRECTORY = 'server';
|
|
16
|
+
const DEFAULT_SERVER_ONLY_PUBLIC_ASSET_EXCLUDES = [
|
|
17
|
+
'api',
|
|
18
|
+
'shared'
|
|
19
|
+
];
|
|
20
|
+
const BFF_EFFECT_WORKER_ENTRY = `${WORKER_BUNDLE_DIRECTORY}/__modern_bff_effect.js`;
|
|
21
|
+
const EFFECT_BFF_CLOUDFLARE_IMPORT_GUIDANCE = 'Ensure the Effect API entry exists at api/index.ts or bff.effect.entry, and import Cloudflare edge handlers from @modern-js/plugin-bff/effect-edge instead of lambda/Hono server helpers.';
|
|
22
|
+
const DEFAULT_COMPATIBILITY_DATE = '2026-06-02';
|
|
23
|
+
const COMPATIBILITY_DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/u;
|
|
24
|
+
const REQUIRED_COMPATIBILITY_FLAGS = CLOUDFLARE_REQUIRED_COMPATIBILITY_FLAGS;
|
|
25
|
+
const RESERVED_ARTIFACT_DESTINATION_FILES = new Set([
|
|
26
|
+
WRANGLER_CONFIG_FILE,
|
|
27
|
+
OUTPUT_PACKAGE_FILE
|
|
28
|
+
]);
|
|
29
|
+
const RESERVED_ARTIFACT_DESTINATION_DIRECTORIES = new Set([
|
|
30
|
+
PUBLIC_ASSETS_DIRECTORY,
|
|
31
|
+
SERVER_OUTPUT_DIRECTORY,
|
|
32
|
+
WORKER_BUNDLE_DIRECTORY
|
|
33
|
+
]);
|
|
34
|
+
export { ASSETS_BINDING, BFF_EFFECT_WORKER_ENTRY, CLOUDFLARE_RUNTIME_TYPE, CLOUDFLARE_WORKER_BUNDLE_FORMAT, COMPATIBILITY_DATE_PATTERN, DEFAULT_COMPATIBILITY_DATE, DEFAULT_SERVER_ONLY_PUBLIC_ASSET_EXCLUDES, EFFECT_BFF_CLOUDFLARE_IMPORT_GUIDANCE, LOADABLE_STATS_FILE, OUTPUT_PACKAGE_FILE, PUBLIC_ASSETS_DIRECTORY, REQUIRED_COMPATIBILITY_FLAGS, RESERVED_ARTIFACT_DESTINATION_DIRECTORIES, RESERVED_ARTIFACT_DESTINATION_FILES, ROUTE_MANIFEST_FILE, ROUTE_SPEC_FILE, ROUTE_SPEC_OUTPUT, SERVER_BUNDLE_DIRECTORY, SERVER_OUTPUT_DIRECTORY, WORKER_BUNDLE_DIRECTORY, WORKER_ENTRY, WORKER_MANIFEST, WRANGLER_CONFIG_FILE };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import node_path from "node:path";
|
|
3
|
+
import { fs } from "@modern-js/utils";
|
|
4
|
+
import { ULTRAMODERN_BUILD_ARTIFACT_PATH, ULTRAMODERN_BUILD_MODULE_PATH, isUltramodernBuildArtifact, nonEmptyString, toDeliveryUnitIdentity } from "@modern-js/utils/universal";
|
|
5
|
+
import { isRecord } from "./utils.mjs";
|
|
6
|
+
const COMPACT_CONFIG_PATH = '.modernjs/ultramodern.json';
|
|
7
|
+
const findWorkspaceRoot = async (appDirectory)=>{
|
|
8
|
+
let current = node_path.resolve(appDirectory);
|
|
9
|
+
for(;;){
|
|
10
|
+
if (await fs.pathExists(node_path.join(current, COMPACT_CONFIG_PATH))) return current;
|
|
11
|
+
const parent = node_path.dirname(current);
|
|
12
|
+
if (parent === current) return;
|
|
13
|
+
current = parent;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const resolveTopologyDeliveryUnit = async (appDirectory)=>{
|
|
17
|
+
const workspaceRoot = await findWorkspaceRoot(appDirectory);
|
|
18
|
+
if (!workspaceRoot) return;
|
|
19
|
+
let compactConfig;
|
|
20
|
+
try {
|
|
21
|
+
compactConfig = await fs.readJSON(node_path.join(workspaceRoot, COMPACT_CONFIG_PATH));
|
|
22
|
+
} catch {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (!isRecord(compactConfig)) return;
|
|
26
|
+
const topology = isRecord(compactConfig.topology) ? compactConfig.topology : void 0;
|
|
27
|
+
const apps = Array.isArray(topology?.apps) ? topology.apps : [];
|
|
28
|
+
const resolvedAppDirectory = node_path.resolve(appDirectory);
|
|
29
|
+
for (const app of apps){
|
|
30
|
+
if (!isRecord(app)) continue;
|
|
31
|
+
const appPath = nonEmptyString(app.path);
|
|
32
|
+
if (appPath && node_path.resolve(workspaceRoot, appPath.replace(/^\.\/+/u, '')) === resolvedAppDirectory) return toDeliveryUnitIdentity(app.deliveryUnit);
|
|
33
|
+
}
|
|
34
|
+
return toDeliveryUnitIdentity(compactConfig.deliveryUnit);
|
|
35
|
+
};
|
|
36
|
+
const resolveWorkerDeliveryUnitStamp = async (appDirectory)=>{
|
|
37
|
+
const buildArtifactPath = node_path.join(appDirectory, ULTRAMODERN_BUILD_ARTIFACT_PATH);
|
|
38
|
+
const buildModulePath = node_path.join(appDirectory, ULTRAMODERN_BUILD_MODULE_PATH);
|
|
39
|
+
let identity;
|
|
40
|
+
if (await fs.pathExists(buildArtifactPath)) {
|
|
41
|
+
const artifact = await fs.readJSON(buildArtifactPath);
|
|
42
|
+
if (!isUltramodernBuildArtifact(artifact)) return;
|
|
43
|
+
identity = toDeliveryUnitIdentity(artifact.deliveryUnit);
|
|
44
|
+
} else if (await fs.pathExists(buildModulePath)) {
|
|
45
|
+
console.warn(`[cloudflare] ${buildArtifactPath} missing; falling back to legacy regex parsing of ${buildModulePath}. Regenerate the workspace to emit ultramodern-build.json.`);
|
|
46
|
+
const source = await fs.readFile(buildModulePath, 'utf8');
|
|
47
|
+
identity = toDeliveryUnitIdentity({
|
|
48
|
+
buildMarker: source.match(/\bbuild:\s*['"]([^'"]+)['"]/u)?.[1],
|
|
49
|
+
unitId: source.match(/\bunitId:\s*['"]([^'"]+)['"]/u)?.[1],
|
|
50
|
+
sourceRevision: source.match(/\bsourceRevision:\s*['"]([^'"]+)['"]/u)?.[1]
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
if (!identity) return;
|
|
54
|
+
return {
|
|
55
|
+
...identity,
|
|
56
|
+
surfaces: {
|
|
57
|
+
ui: {
|
|
58
|
+
...identity,
|
|
59
|
+
surface: 'ui'
|
|
60
|
+
},
|
|
61
|
+
api: {
|
|
62
|
+
...identity,
|
|
63
|
+
surface: 'api'
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
export { resolveTopologyDeliveryUnit, resolveWorkerDeliveryUnitStamp };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { isRecord } from "./utils.mjs";
|
|
3
|
+
const I18N_SERVER_PLUGIN_NAME = 'plugin-i18n/server';
|
|
4
|
+
const DEFAULT_I18NEXT_DETECTION_OPTIONS = {
|
|
5
|
+
order: [
|
|
6
|
+
'querystring',
|
|
7
|
+
'cookie',
|
|
8
|
+
'localStorage',
|
|
9
|
+
'header',
|
|
10
|
+
'navigator',
|
|
11
|
+
'htmlTag',
|
|
12
|
+
'path',
|
|
13
|
+
'subdomain'
|
|
14
|
+
],
|
|
15
|
+
lookupQuerystring: 'lng',
|
|
16
|
+
lookupCookie: 'i18next',
|
|
17
|
+
lookupHeader: 'accept-language'
|
|
18
|
+
};
|
|
19
|
+
const SERVER_SIDE_I18N_DETECTORS = new Set([
|
|
20
|
+
'querystring',
|
|
21
|
+
'cookie',
|
|
22
|
+
'header'
|
|
23
|
+
]);
|
|
24
|
+
const toStringArray = (value)=>Array.isArray(value) ? value.filter((entry)=>'string' == typeof entry) : [];
|
|
25
|
+
const getI18nServerPlugin = (appContext)=>appContext.serverPlugins?.find((plugin)=>plugin.name.includes(I18N_SERVER_PLUGIN_NAME));
|
|
26
|
+
const removeLocaleDetectionByEntry = (config)=>{
|
|
27
|
+
const { localeDetectionByEntry: _localeDetectionByEntry, ...rest } = config;
|
|
28
|
+
return rest;
|
|
29
|
+
};
|
|
30
|
+
const getEntryLocaleDetection = (entryName, localeDetection)=>{
|
|
31
|
+
const byEntry = isRecord(localeDetection.localeDetectionByEntry) ? localeDetection.localeDetectionByEntry : void 0;
|
|
32
|
+
const entryConfig = byEntry?.[entryName];
|
|
33
|
+
const globalConfig = removeLocaleDetectionByEntry(localeDetection);
|
|
34
|
+
return isRecord(entryConfig) ? {
|
|
35
|
+
...globalConfig,
|
|
36
|
+
...entryConfig
|
|
37
|
+
} : globalConfig;
|
|
38
|
+
};
|
|
39
|
+
const normalizeI18nDetection = (detection)=>{
|
|
40
|
+
const configured = isRecord(detection) ? detection : {};
|
|
41
|
+
const rawOrder = Array.isArray(configured.order) ? configured.order : DEFAULT_I18NEXT_DETECTION_OPTIONS.order;
|
|
42
|
+
const order = rawOrder.filter((entry)=>'string' == typeof entry && SERVER_SIDE_I18N_DETECTORS.has(entry));
|
|
43
|
+
return {
|
|
44
|
+
order: order.length > 0 ? order : [
|
|
45
|
+
'querystring',
|
|
46
|
+
'cookie',
|
|
47
|
+
'header'
|
|
48
|
+
],
|
|
49
|
+
lookupQuerystring: 'string' == typeof configured.lookupQuerystring && configured.lookupQuerystring ? configured.lookupQuerystring : DEFAULT_I18NEXT_DETECTION_OPTIONS.lookupQuerystring,
|
|
50
|
+
lookupCookie: 'string' == typeof configured.lookupCookie && configured.lookupCookie ? configured.lookupCookie : DEFAULT_I18NEXT_DETECTION_OPTIONS.lookupCookie,
|
|
51
|
+
lookupHeader: 'string' == typeof configured.lookupHeader && configured.lookupHeader ? configured.lookupHeader : DEFAULT_I18NEXT_DETECTION_OPTIONS.lookupHeader
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
const normalizeLocalisedUrls = (localisedUrls)=>{
|
|
55
|
+
if (!isRecord(localisedUrls) || 0 === Object.keys(localisedUrls).length) return;
|
|
56
|
+
const normalized = {};
|
|
57
|
+
for (const [canonicalPath, languageMap] of Object.entries(localisedUrls)){
|
|
58
|
+
if (!isRecord(languageMap)) continue;
|
|
59
|
+
const normalizedLanguageMap = Object.fromEntries(Object.entries(languageMap).filter((entry)=>'string' == typeof entry[1] && entry[1].length > 0));
|
|
60
|
+
if (Object.keys(normalizedLanguageMap).length > 0) normalized[canonicalPath] = normalizedLanguageMap;
|
|
61
|
+
}
|
|
62
|
+
return Object.keys(normalized).length > 0 ? normalized : void 0;
|
|
63
|
+
};
|
|
64
|
+
const normalizeWorkerI18nEntry = (entryName, localeDetection, staticRoutePrefixes)=>{
|
|
65
|
+
const entryConfig = getEntryLocaleDetection(entryName, localeDetection);
|
|
66
|
+
const languages = toStringArray(entryConfig.languages);
|
|
67
|
+
if (true !== entryConfig.localePathRedirect || 0 === languages.length) return;
|
|
68
|
+
const fallbackLanguage = 'string' == typeof entryConfig.fallbackLanguage && entryConfig.fallbackLanguage ? entryConfig.fallbackLanguage : 'en';
|
|
69
|
+
const ignoreRedirectRoutes = Array.isArray(entryConfig.ignoreRedirectRoutes) ? toStringArray(entryConfig.ignoreRedirectRoutes) : [];
|
|
70
|
+
const localisedUrls = normalizeLocalisedUrls(entryConfig.localisedUrls);
|
|
71
|
+
return {
|
|
72
|
+
i18nextDetector: false !== entryConfig.i18nextDetector,
|
|
73
|
+
languages,
|
|
74
|
+
fallbackLanguage,
|
|
75
|
+
detection: normalizeI18nDetection(entryConfig.detection),
|
|
76
|
+
ignoreRedirectRoutes,
|
|
77
|
+
staticRoutePrefixes,
|
|
78
|
+
...localisedUrls ? {
|
|
79
|
+
localisedUrls
|
|
80
|
+
} : {}
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
const createI18nWorkerManifest = (routeSpec, appContext)=>{
|
|
84
|
+
const i18nServerPlugin = getI18nServerPlugin(appContext);
|
|
85
|
+
const localeDetection = i18nServerPlugin?.options?.localeDetection;
|
|
86
|
+
if (!isRecord(localeDetection)) return;
|
|
87
|
+
const staticRoutePrefixes = toStringArray(i18nServerPlugin?.options?.staticRoutePrefixes);
|
|
88
|
+
const entries = {};
|
|
89
|
+
for (const route of routeSpec.routes){
|
|
90
|
+
if ('string' != typeof route.entryName || entries[route.entryName]) continue;
|
|
91
|
+
const entry = normalizeWorkerI18nEntry(route.entryName, localeDetection, staticRoutePrefixes);
|
|
92
|
+
if (entry) entries[route.entryName] = entry;
|
|
93
|
+
}
|
|
94
|
+
return Object.keys(entries).length > 0 ? {
|
|
95
|
+
entries
|
|
96
|
+
} : void 0;
|
|
97
|
+
};
|
|
98
|
+
export { createI18nWorkerManifest };
|