@bleedingdev/modern-js-app-tools-extensions 3.9.0-ultramodern.3 → 3.9.0-ultramodern.5
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/backend-federation-build/codegen.js +256 -0
- package/dist/cjs/backend-federation-build/config.js +204 -0
- package/dist/cjs/backend-federation-build/emit.js +150 -0
- package/dist/cjs/backend-federation-build/index.js +53 -0
- package/dist/cjs/build-config/build-environment.js +268 -0
- package/dist/cjs/build-config/precompress/plugin.js +124 -0
- package/dist/cjs/build-config/precompress/types.js +18 -0
- package/dist/cjs/build-config/public.js +46 -0
- package/dist/cjs/cloudflare/constants.js +1 -1
- package/dist/cjs/cloudflare/delivery-unit.js +9 -9
- package/dist/cjs/cloudflare-output-contract.js +37 -1
- package/dist/cjs/cloudflare-output-verifier/identity.js +6 -6
- package/dist/cjs/cloudflare-output-verifier/worker-bundles.js +4 -1
- package/dist/cjs/config.js +29 -1
- package/dist/cjs/deploy-output/npmAliases.js +226 -0
- package/dist/cjs/deploy-output/plugin.js +76 -0
- package/dist/cjs/deploy-output/target.js +48 -0
- package/dist/cjs/native-type-checker.js +150 -0
- package/dist/cjs/release-envelope/framework-output.js +27 -27
- package/dist/cjs/release-envelope/plugin.js +2 -1
- package/dist/cjs/release-identity.js +3 -6
- package/dist/esm/backend-federation-build/codegen.mjs +205 -0
- package/dist/esm/backend-federation-build/config.mjs +128 -0
- package/dist/esm/backend-federation-build/emit.mjs +99 -0
- package/dist/esm/backend-federation-build/index.mjs +1 -0
- package/dist/esm/build-config/build-environment.mjs +224 -0
- package/dist/esm/build-config/precompress/plugin.mjs +76 -0
- package/dist/esm/build-config/precompress/types.mjs +0 -0
- package/dist/esm/build-config/public.mjs +1 -0
- package/dist/esm/cloudflare/constants.mjs +1 -1
- package/dist/esm/cloudflare/delivery-unit.mjs +1 -1
- package/dist/esm/cloudflare-output-contract.mjs +35 -2
- package/dist/esm/cloudflare-output-verifier/identity.mjs +1 -1
- package/dist/esm/cloudflare-output-verifier/worker-bundles.mjs +5 -2
- package/dist/esm/config.mjs +1 -0
- package/dist/esm/deploy-output/npmAliases.mjs +174 -0
- package/dist/esm/deploy-output/plugin.mjs +28 -0
- package/dist/esm/deploy-output/target.mjs +10 -0
- package/dist/esm/native-type-checker.mjs +99 -0
- package/dist/esm/release-envelope/framework-output.mjs +1 -1
- package/dist/esm/release-envelope/plugin.mjs +2 -1
- package/dist/esm/release-identity.mjs +3 -6
- package/dist/esm-node/backend-federation-build/codegen.mjs +206 -0
- package/dist/esm-node/backend-federation-build/config.mjs +129 -0
- package/dist/esm-node/backend-federation-build/emit.mjs +100 -0
- package/dist/esm-node/backend-federation-build/index.mjs +2 -0
- package/dist/esm-node/build-config/build-environment.mjs +225 -0
- package/dist/esm-node/build-config/precompress/plugin.mjs +77 -0
- package/dist/esm-node/build-config/precompress/types.mjs +1 -0
- package/dist/esm-node/build-config/public.mjs +2 -0
- package/dist/esm-node/cloudflare/constants.mjs +1 -1
- package/dist/esm-node/cloudflare/delivery-unit.mjs +1 -1
- package/dist/esm-node/cloudflare-output-contract.mjs +35 -2
- package/dist/esm-node/cloudflare-output-verifier/identity.mjs +1 -1
- package/dist/esm-node/cloudflare-output-verifier/worker-bundles.mjs +5 -2
- package/dist/esm-node/config.mjs +1 -0
- package/dist/esm-node/deploy-output/npmAliases.mjs +175 -0
- package/dist/esm-node/deploy-output/plugin.mjs +31 -0
- package/dist/esm-node/deploy-output/target.mjs +11 -0
- package/dist/esm-node/native-type-checker.mjs +100 -0
- package/dist/esm-node/release-envelope/framework-output.mjs +1 -1
- package/dist/esm-node/release-envelope/plugin.mjs +2 -1
- package/dist/esm-node/release-identity.mjs +3 -6
- package/dist/types/backend-federation-build/codegen.d.ts +86 -0
- package/dist/types/backend-federation-build/config.d.ts +103 -0
- package/dist/types/backend-federation-build/emit.d.ts +21 -0
- package/dist/types/backend-federation-build/index.d.ts +2 -0
- package/dist/types/build-config/build-environment.d.ts +22 -0
- package/dist/types/build-config/precompress/plugin.d.ts +4 -0
- package/dist/types/build-config/precompress/types.d.ts +19 -0
- package/dist/types/build-config/public.d.ts +2 -0
- package/dist/types/cloudflare/constants.d.ts +1 -1
- package/dist/types/cloudflare/delivery-unit.d.ts +1 -1
- package/dist/types/cloudflare/worker-manifest.d.ts +2 -2
- package/dist/types/cloudflare-output-contract.d.ts +2 -1
- package/dist/types/cloudflare-output-verifier/identity.d.ts +1 -1
- package/dist/types/config.d.ts +2 -0
- package/dist/types/deploy-output/npmAliases.d.ts +14 -0
- package/dist/types/deploy-output/plugin.d.ts +15 -0
- package/dist/types/deploy-output/target.d.ts +6 -0
- package/dist/types/native-type-checker.d.ts +15 -0
- package/dist/types/precompress/LICENSE +20 -0
- package/dist/types/precompress/index.d.ts +76 -0
- package/package.json +55 -8
- package/src/backend-federation-build/codegen.ts +245 -0
- package/src/backend-federation-build/config.ts +376 -0
- package/src/backend-federation-build/emit.ts +205 -0
- package/src/backend-federation-build/index.ts +2 -0
- package/src/build-config/build-environment.ts +404 -0
- package/src/build-config/precompress/plugin.ts +140 -0
- package/src/build-config/precompress/types.ts +26 -0
- package/src/build-config/public.ts +6 -0
- package/src/cloudflare/constants.ts +1 -1
- package/src/cloudflare/delivery-unit.ts +2 -2
- package/src/cloudflare/worker-manifest.ts +1 -0
- package/src/cloudflare-builder.ts +1 -0
- package/src/cloudflare-output-contract.ts +37 -0
- package/src/cloudflare-output-verifier/identity.ts +1 -1
- package/src/cloudflare-output-verifier/worker-bundles.ts +5 -3
- package/src/config.ts +7 -0
- package/src/deploy-output/npmAliases.ts +333 -0
- package/src/deploy-output/plugin.ts +49 -0
- package/src/deploy-output/target.ts +23 -0
- package/src/native-type-checker.ts +122 -0
- package/src/release-envelope/framework-output.ts +1 -1
- package/src/release-envelope/plugin.ts +5 -1
- package/src/release-identity.ts +3 -2
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { realpathSync } from "node:fs";
|
|
3
|
+
import node_path from "node:path";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
const execute = promisify(execFile);
|
|
6
|
+
const native_type_checker_name = 'UltramodernNativeTypeChecker';
|
|
7
|
+
const watchDependencyPath = (file)=>'win32' === process.platform ? realpathSync.native(file) : file;
|
|
8
|
+
class UltramodernNativeTypeChecker {
|
|
9
|
+
async check() {
|
|
10
|
+
const { configFile, build } = this.options;
|
|
11
|
+
const args = build ? [
|
|
12
|
+
'--build',
|
|
13
|
+
configFile,
|
|
14
|
+
'--stopBuildOnErrors'
|
|
15
|
+
] : [
|
|
16
|
+
'--project',
|
|
17
|
+
configFile,
|
|
18
|
+
'--noEmit'
|
|
19
|
+
];
|
|
20
|
+
await this.run([
|
|
21
|
+
...args,
|
|
22
|
+
'--pretty',
|
|
23
|
+
'false'
|
|
24
|
+
]);
|
|
25
|
+
}
|
|
26
|
+
async run(args) {
|
|
27
|
+
try {
|
|
28
|
+
const result = await execute(this.options.compiler(), args, {
|
|
29
|
+
cwd: node_path.dirname(this.options.configFile),
|
|
30
|
+
maxBuffer: 16777216
|
|
31
|
+
});
|
|
32
|
+
return result.stdout;
|
|
33
|
+
} catch (cause) {
|
|
34
|
+
const output = cause;
|
|
35
|
+
const diagnostics = `${output.stdout ?? ''}${output.stderr ?? ''}`;
|
|
36
|
+
throw new Error(`${native_type_checker_name} failed:\n${diagnostics || (cause instanceof Error ? cause.message : String(cause))}`, {
|
|
37
|
+
cause
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async watchInputs() {
|
|
42
|
+
const inputs = new Set();
|
|
43
|
+
const projects = [
|
|
44
|
+
this.options.configFile
|
|
45
|
+
];
|
|
46
|
+
const visited = new Set();
|
|
47
|
+
for (const configFile of projects){
|
|
48
|
+
if (visited.has(configFile)) continue;
|
|
49
|
+
visited.add(configFile);
|
|
50
|
+
inputs.add(configFile);
|
|
51
|
+
const config = JSON.parse(await this.run([
|
|
52
|
+
'--showConfig',
|
|
53
|
+
'--project',
|
|
54
|
+
configFile
|
|
55
|
+
]));
|
|
56
|
+
const directory = node_path.dirname(configFile);
|
|
57
|
+
for (const file of config.files ?? [])inputs.add(node_path.resolve(directory, file));
|
|
58
|
+
for (const reference of config.references ?? []){
|
|
59
|
+
const target = node_path.resolve(directory, reference.path);
|
|
60
|
+
projects.push('.json' === node_path.extname(target) ? target : node_path.join(target, 'tsconfig.json'));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return inputs;
|
|
64
|
+
}
|
|
65
|
+
apply(compiler) {
|
|
66
|
+
compiler.hooks.thisCompilation.tap(native_type_checker_name, (compilation)=>{
|
|
67
|
+
compilation.hooks.processAssets.tapPromise(native_type_checker_name, async ()=>{
|
|
68
|
+
try {
|
|
69
|
+
compilation.fileDependencies.add(watchDependencyPath(this.options.configFile));
|
|
70
|
+
for (const file of (await this.watchInputs()))compilation.fileDependencies.add(watchDependencyPath(file));
|
|
71
|
+
await this.check();
|
|
72
|
+
} catch (cause) {
|
|
73
|
+
compilation.errors.push(new Error(cause instanceof Error ? cause.message : String(cause), {
|
|
74
|
+
cause
|
|
75
|
+
}));
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
constructor(options){
|
|
81
|
+
this.options = options;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function configureUltramodernTypeChecker(chain, pluginId, resolveCompiler) {
|
|
85
|
+
if (!chain.plugins.has(pluginId)) return;
|
|
86
|
+
const options = chain.plugin(pluginId).get('args')?.[0];
|
|
87
|
+
const typescript = options?.typescript;
|
|
88
|
+
if (typescript?.tsgo === false) return;
|
|
89
|
+
if (!typescript?.configFile) throw new Error(`${native_type_checker_name} requires the configured TypeScript project.`);
|
|
90
|
+
const configFile = node_path.resolve(typescript.configFile);
|
|
91
|
+
chain.plugin(pluginId).use(UltramodernNativeTypeChecker, [
|
|
92
|
+
{
|
|
93
|
+
build: true === typescript.build,
|
|
94
|
+
compiler: ()=>resolveCompiler(configFile),
|
|
95
|
+
configFile
|
|
96
|
+
}
|
|
97
|
+
]);
|
|
98
|
+
}
|
|
99
|
+
export { UltramodernNativeTypeChecker, configureUltramodernTypeChecker };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import promises from "node:fs/promises";
|
|
3
3
|
import node_path from "node:path";
|
|
4
|
-
import { BACKEND_FEDERATION_MANIFEST_FILE, BACKEND_FEDERATION_REMOTE_ENTRY_FILE, ULTRAMODERN_BUILD_ARTIFACT_FILE, isUltramodernBuildArtifact } from "@modern-js/
|
|
4
|
+
import { BACKEND_FEDERATION_MANIFEST_FILE, BACKEND_FEDERATION_REMOTE_ENTRY_FILE, ULTRAMODERN_BUILD_ARTIFACT_FILE, isUltramodernBuildArtifact } from "@modern-js/backend-federation-contracts";
|
|
5
5
|
import { canonicalSerializeMicroVerticalReleaseEnvelope, createMicroVerticalReleaseEnvelope, verifyMicroVerticalReleaseEnvelope } from "./index.mjs";
|
|
6
6
|
const MICROVERTICAL_RELEASE_ENVELOPE_PATH = 'release/microvertical-release-envelope.json';
|
|
7
7
|
const MICROVERTICAL_RELEASE_IDENTITY_CARRIERS_PATH = 'release/microvertical-release-identity-carriers.json';
|
|
@@ -10,7 +10,8 @@ const createUltramodernReleaseEnvelopePlugin = ({ resolveDeployTarget })=>({
|
|
|
10
10
|
name: '@modern-js/ultramodern-release-envelope',
|
|
11
11
|
pre: [
|
|
12
12
|
'@modern-js/backend-federation-build',
|
|
13
|
-
'@modern-js/plugin-bff'
|
|
13
|
+
'@modern-js/plugin-bff',
|
|
14
|
+
'@modern-js/deploy-output-aliases'
|
|
14
15
|
],
|
|
15
16
|
post: [
|
|
16
17
|
'@modern-js/plugin-deploy'
|
|
@@ -4,11 +4,8 @@ const BUILD_MARKER_NAMESPACE = 'ultramodern-delivery-unit-release-build-marker:v
|
|
|
4
4
|
const PROMOTABLE_SOURCE_REVISION_PATTERN = /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/u;
|
|
5
5
|
const gitOutput = (workspaceRoot, args)=>{
|
|
6
6
|
try {
|
|
7
|
-
return execFileSync('git',
|
|
8
|
-
|
|
9
|
-
workspaceRoot,
|
|
10
|
-
...args
|
|
11
|
-
], {
|
|
7
|
+
return execFileSync('git', args, {
|
|
8
|
+
cwd: workspaceRoot,
|
|
12
9
|
encoding: 'utf8',
|
|
13
10
|
stdio: [
|
|
14
11
|
'ignore',
|
|
@@ -43,7 +40,7 @@ const resolveUltramodernSourceRevision = (workspaceRoot = process.cwd(), configu
|
|
|
43
40
|
]);
|
|
44
41
|
const status = gitOutput(gitRoot, [
|
|
45
42
|
'status',
|
|
46
|
-
'--porcelain
|
|
43
|
+
'--porcelain',
|
|
47
44
|
'--untracked-files=all'
|
|
48
45
|
]);
|
|
49
46
|
if (!revision || void 0 === status || status.length > 0) return 'workspace';
|
|
@@ -0,0 +1,206 @@
|
|
|
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/backend-federation-contracts";
|
|
4
|
+
import { build } from "esbuild";
|
|
5
|
+
import { normalizeRelativePath } from "./config.mjs";
|
|
6
|
+
const createRelativeImportSpecifier = (fromDirectory, toFile)=>{
|
|
7
|
+
const relativePath = normalizeRelativePath(node_path.relative(fromDirectory, toFile));
|
|
8
|
+
return relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
|
|
9
|
+
};
|
|
10
|
+
const createPublicPath = (containerEntry, fallbackPort)=>{
|
|
11
|
+
try {
|
|
12
|
+
return new URL('.', containerEntry).href;
|
|
13
|
+
} catch {
|
|
14
|
+
const slashIndex = containerEntry.lastIndexOf('/');
|
|
15
|
+
if (slashIndex >= 0) return containerEntry.slice(0, slashIndex + 1);
|
|
16
|
+
return `http://localhost:${fallbackPort}/`;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const createBackendRemoteEntrySource = async (workspaceRoot, app, effectApiPath, entryPath)=>{
|
|
20
|
+
const effectApiSpecifier = createRelativeImportSpecifier(node_path.dirname(effectApiPath), effectApiPath);
|
|
21
|
+
const containerMetadata = {
|
|
22
|
+
buildVersion: app.buildVersion,
|
|
23
|
+
expose: BACKEND_FEDERATION_EFFECT_EXPOSE,
|
|
24
|
+
name: app.backendName,
|
|
25
|
+
packageName: app.packageName,
|
|
26
|
+
remoteType: app.remoteType,
|
|
27
|
+
source: normalizeRelativePath(node_path.relative(workspaceRoot, effectApiPath)),
|
|
28
|
+
version: app.version,
|
|
29
|
+
...app.unitId ? {
|
|
30
|
+
unitId: app.unitId
|
|
31
|
+
} : {},
|
|
32
|
+
...app.sourceRevision ? {
|
|
33
|
+
sourceRevision: app.sourceRevision
|
|
34
|
+
} : {}
|
|
35
|
+
};
|
|
36
|
+
const compatibility = {
|
|
37
|
+
build: app.buildVersion,
|
|
38
|
+
contractVersion: BACKEND_FEDERATION_CONTRACT_VERSION,
|
|
39
|
+
nodeAdapterVersion: BACKEND_FEDERATION_NODE_ADAPTER_VERSION,
|
|
40
|
+
packageName: app.packageName,
|
|
41
|
+
...app.unitId ? {
|
|
42
|
+
unitId: app.unitId
|
|
43
|
+
} : {},
|
|
44
|
+
...app.sourceRevision ? {
|
|
45
|
+
sourceRevision: app.sourceRevision
|
|
46
|
+
} : {}
|
|
47
|
+
};
|
|
48
|
+
const source = `// Generated by Modern.js backend federation build.
|
|
49
|
+
import * as exposedNamespace from ${JSON.stringify(effectApiSpecifier)};
|
|
50
|
+
|
|
51
|
+
const exposedModule = {
|
|
52
|
+
...exposedNamespace,
|
|
53
|
+
backendFederationContract: {
|
|
54
|
+
...(exposedNamespace.backendFederationContract ?? {}),
|
|
55
|
+
compatibility: {
|
|
56
|
+
...(exposedNamespace.backendFederationContract?.compatibility ?? {}),
|
|
57
|
+
...${JSON.stringify(compatibility)},
|
|
58
|
+
},
|
|
59
|
+
name: ${JSON.stringify(app.backendName)},
|
|
60
|
+
role: 'microvertical-server',
|
|
61
|
+
runtimeFramework: 'effect',
|
|
62
|
+
strictEffectApproach: true,
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
const exposedModules = {
|
|
66
|
+
${JSON.stringify(BACKEND_FEDERATION_EFFECT_EXPOSE)}: () => Promise.resolve(exposedModule),
|
|
67
|
+
};
|
|
68
|
+
const __modernjsBackendFederation = ${JSON.stringify(containerMetadata)};
|
|
69
|
+
|
|
70
|
+
function init() {}
|
|
71
|
+
|
|
72
|
+
function get(id) {
|
|
73
|
+
const load = exposedModules[id];
|
|
74
|
+
if (!load) {
|
|
75
|
+
throw new Error(\`Unexpected backend federation expose: \${id}\`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return async () => load();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
module.exports = { __modernjsBackendFederation, get, init };
|
|
82
|
+
`;
|
|
83
|
+
const result = await build({
|
|
84
|
+
absWorkingDir: node_path.dirname(effectApiPath),
|
|
85
|
+
bundle: true,
|
|
86
|
+
format: 'cjs',
|
|
87
|
+
logLevel: 'silent',
|
|
88
|
+
platform: 'node',
|
|
89
|
+
stdin: {
|
|
90
|
+
contents: source,
|
|
91
|
+
loader: 'ts',
|
|
92
|
+
resolveDir: node_path.dirname(effectApiPath),
|
|
93
|
+
sourcefile: node_path.basename(entryPath)
|
|
94
|
+
},
|
|
95
|
+
target: 'node20',
|
|
96
|
+
write: false
|
|
97
|
+
});
|
|
98
|
+
const output = result.outputFiles[0];
|
|
99
|
+
if (!output) throw new Error('[backend-federation-build] CommonJS backend federation container emitted no output.');
|
|
100
|
+
return output.text;
|
|
101
|
+
};
|
|
102
|
+
const createBackendManifest = (workspaceRoot, distDirectory, app, entryIntegrity)=>{
|
|
103
|
+
const sourceModule = `${app.directory}/api/effect-api.ts`;
|
|
104
|
+
const publicPath = createPublicPath(app.containerEntry, app.port);
|
|
105
|
+
const manifest = {
|
|
106
|
+
schemaVersion: 1,
|
|
107
|
+
id: app.backendName,
|
|
108
|
+
name: app.backendName,
|
|
109
|
+
version: app.version,
|
|
110
|
+
buildVersion: app.buildVersion,
|
|
111
|
+
metaData: {
|
|
112
|
+
name: app.backendName,
|
|
113
|
+
type: 'backend',
|
|
114
|
+
buildInfo: {
|
|
115
|
+
buildVersion: app.buildVersion,
|
|
116
|
+
buildName: app.packageName
|
|
117
|
+
},
|
|
118
|
+
remoteEntry: {
|
|
119
|
+
name: BACKEND_FEDERATION_REMOTE_ENTRY_FILE,
|
|
120
|
+
path: '',
|
|
121
|
+
type: app.remoteType
|
|
122
|
+
},
|
|
123
|
+
globalName: app.backendName,
|
|
124
|
+
publicPath,
|
|
125
|
+
ssrRemoteEntry: {
|
|
126
|
+
name: BACKEND_FEDERATION_REMOTE_ENTRY_FILE,
|
|
127
|
+
path: '',
|
|
128
|
+
type: app.remoteType
|
|
129
|
+
},
|
|
130
|
+
ssrPublicPath: publicPath
|
|
131
|
+
},
|
|
132
|
+
entry: {
|
|
133
|
+
...entryIntegrity,
|
|
134
|
+
file: BACKEND_FEDERATION_REMOTE_ENTRY_FILE,
|
|
135
|
+
path: normalizeRelativePath(node_path.relative(workspaceRoot, node_path.join(distDirectory, BACKEND_FEDERATION_REMOTE_ENTRY_FILE))),
|
|
136
|
+
type: app.remoteType,
|
|
137
|
+
url: app.containerEntry
|
|
138
|
+
},
|
|
139
|
+
exposes: [
|
|
140
|
+
{
|
|
141
|
+
id: `${app.backendName}:${BACKEND_FEDERATION_EFFECT_EXPOSE}`,
|
|
142
|
+
name: BACKEND_FEDERATION_EFFECT_EXPOSE,
|
|
143
|
+
path: '',
|
|
144
|
+
assets: {
|
|
145
|
+
js: {
|
|
146
|
+
async: [],
|
|
147
|
+
sync: [
|
|
148
|
+
BACKEND_FEDERATION_REMOTE_ENTRY_FILE
|
|
149
|
+
]
|
|
150
|
+
},
|
|
151
|
+
css: {
|
|
152
|
+
async: [],
|
|
153
|
+
sync: []
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
],
|
|
158
|
+
shared: [],
|
|
159
|
+
backendFederation: {
|
|
160
|
+
role: 'microvertical-server',
|
|
161
|
+
appId: app.id,
|
|
162
|
+
name: app.backendName,
|
|
163
|
+
runtimeFramework: 'effect',
|
|
164
|
+
strictEffectApproach: true,
|
|
165
|
+
contractVersion: BACKEND_FEDERATION_CONTRACT_VERSION,
|
|
166
|
+
nodeAdapterVersion: BACKEND_FEDERATION_NODE_ADAPTER_VERSION,
|
|
167
|
+
remoteType: app.remoteType,
|
|
168
|
+
expose: BACKEND_FEDERATION_EFFECT_EXPOSE,
|
|
169
|
+
manifestUrl: app.manifestUrl,
|
|
170
|
+
containerEntry: app.containerEntry,
|
|
171
|
+
source: {
|
|
172
|
+
module: sourceModule
|
|
173
|
+
},
|
|
174
|
+
readinessPath: `${app.apiPrefix}/${app.apiStem}/readiness`,
|
|
175
|
+
openapiPath: `${app.apiPrefix}/openapi.json`,
|
|
176
|
+
...app.deliveryUnit ? {
|
|
177
|
+
deliveryUnit: app.deliveryUnit
|
|
178
|
+
} : {},
|
|
179
|
+
versionBoundary: {
|
|
180
|
+
invariant: 'web-and-api-same-build',
|
|
181
|
+
packageName: app.packageName,
|
|
182
|
+
version: app.version,
|
|
183
|
+
buildVersion: app.buildVersion,
|
|
184
|
+
...app.uiManifestUrl ? {
|
|
185
|
+
uiManifestUrl: app.uiManifestUrl
|
|
186
|
+
} : {},
|
|
187
|
+
...app.deliveryUnit ? {
|
|
188
|
+
deliveryUnit: {
|
|
189
|
+
unitId: app.deliveryUnit.unitId,
|
|
190
|
+
buildMarker: app.deliveryUnit.buildMarker,
|
|
191
|
+
sourceRevision: app.deliveryUnit.sourceRevision
|
|
192
|
+
}
|
|
193
|
+
} : {}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
const validation = validateBackendFederationManifest(manifest, {
|
|
198
|
+
path: 'backendFederationManifest',
|
|
199
|
+
requireEffectExpose: true,
|
|
200
|
+
requireEffectRuntime: true,
|
|
201
|
+
requireVersionFields: true
|
|
202
|
+
});
|
|
203
|
+
if (!validation.ok) throw new Error(`[backend-federation-build] Invalid backend federation manifest: ${formatBackendFederationValidationErrors(validation.errors)}.`);
|
|
204
|
+
return manifest;
|
|
205
|
+
};
|
|
206
|
+
export { createBackendManifest, createBackendRemoteEntrySource };
|
|
@@ -0,0 +1,129 @@
|
|
|
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 { resolveUltramodernSourceRevision } from "@modern-js/app-tools-extensions/release-identity";
|
|
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, isUltramodernBuildArtifact, validateDeliveryUnitRecord, validateUltramodernBuildArtifact } from "@modern-js/backend-federation-contracts";
|
|
7
|
+
const COMPACT_CONFIG_PATH = '.modernjs/ultramodern.json';
|
|
8
|
+
const normalizeRelativePath = (value)=>value.replace(/\\/gu, '/').replace(/^\.\/+/u, '');
|
|
9
|
+
const isRecord = (value)=>'object' == typeof value && null !== value;
|
|
10
|
+
const readJsonFile = async (filePath)=>JSON.parse(await promises.readFile(filePath, 'utf8'));
|
|
11
|
+
const buildArtifactPathFor = (appDirectory)=>node_path.join(appDirectory, ULTRAMODERN_BUILD_ARTIFACT_PATH);
|
|
12
|
+
const buildModulePathFor = (appDirectory)=>node_path.join(appDirectory, ULTRAMODERN_BUILD_MODULE_PATH);
|
|
13
|
+
const readBuildIdentity = async (appDirectory)=>{
|
|
14
|
+
const buildArtifactPath = buildArtifactPathFor(appDirectory);
|
|
15
|
+
if (existsSync(buildArtifactPath)) {
|
|
16
|
+
const artifact = await readJsonFile(buildArtifactPath);
|
|
17
|
+
const artifactValidation = validateUltramodernBuildArtifact(artifact, buildArtifactPath);
|
|
18
|
+
if (!artifactValidation.ok) throw new Error(`[backend-federation-build] Invalid delivery-unit build artifact at ${buildArtifactPath}.`);
|
|
19
|
+
if (!isUltramodernBuildArtifact(artifact)) 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)=>resolveUltramodernSourceRevision(workspaceRoot);
|
|
62
|
+
const createStampedDeliveryUnit = (input)=>{
|
|
63
|
+
if (!input.unitId || !input.buildMarker || !input.packageName || !input.version) return;
|
|
64
|
+
const record = {
|
|
65
|
+
schemaVersion: DELIVERY_UNIT_SCHEMA_VERSION,
|
|
66
|
+
kind: DELIVERY_UNIT_KIND,
|
|
67
|
+
appId: input.appId,
|
|
68
|
+
unitId: input.unitId,
|
|
69
|
+
packageName: input.packageName,
|
|
70
|
+
version: input.version,
|
|
71
|
+
buildMarker: input.buildMarker,
|
|
72
|
+
sourceRevision: input.sourceRevision,
|
|
73
|
+
deployProfile: DELIVERY_UNIT_DEPLOY_PROFILE
|
|
74
|
+
};
|
|
75
|
+
const validation = validateDeliveryUnitRecord(record);
|
|
76
|
+
if (!validation.ok) return;
|
|
77
|
+
return deliveryUnitContractBlock(record);
|
|
78
|
+
};
|
|
79
|
+
const createAppFromCompactMetadata = (workspaceRoot, appDirectory, compactApp)=>{
|
|
80
|
+
const id = stringValue(compactApp.id);
|
|
81
|
+
const appPath = stringValue(compactApp.path);
|
|
82
|
+
if (!id || 'vertical' !== compactApp.kind || !isRecord(compactApp.api)) return;
|
|
83
|
+
const directory = appPath ? normalizeRelativePath(appPath) : `verticals/${id}`;
|
|
84
|
+
if (node_path.resolve(workspaceRoot, directory) !== node_path.resolve(appDirectory)) return;
|
|
85
|
+
const port = 'number' == typeof compactApp.port ? compactApp.port : void 0;
|
|
86
|
+
if (void 0 === port) return;
|
|
87
|
+
const apiPrefix = stringValue(compactApp.api.prefix) ?? `/${id}-api`;
|
|
88
|
+
const apiStem = stringValue(compactApp.api.stem) ?? id;
|
|
89
|
+
const backendName = createBackendName(compactApp, id);
|
|
90
|
+
const manifestUrl = stringValue(compactApp.backendFederation?.executionSurfaces?.node?.manifestUrl) ?? stringValue(compactApp.serverExecution?.node?.manifestUrl) ?? `http://localhost:${port}/${BACKEND_FEDERATION_MANIFEST_FILE}`;
|
|
91
|
+
const containerEntry = stringValue(compactApp.backendFederation?.executionSurfaces?.node?.containerEntry) ?? stringValue(compactApp.serverExecution?.node?.containerEntry) ?? `http://localhost:${port}/${BACKEND_FEDERATION_REMOTE_ENTRY_FILE}`;
|
|
92
|
+
const configuredRemoteType = stringValue(compactApp.backendFederation?.executionSurfaces?.node?.remoteType) ?? stringValue(compactApp.serverExecution?.node?.remoteType);
|
|
93
|
+
if (void 0 !== configuredRemoteType && 'commonjs-module' !== configuredRemoteType) throw new Error(`[backend-federation-build] Node backend federation remoteType must be "commonjs-module"; received "${configuredRemoteType}" for ${id}.`);
|
|
94
|
+
if (!new URL(containerEntry).pathname.endsWith(`/${BACKEND_FEDERATION_REMOTE_ENTRY_FILE}`)) throw new Error(`[backend-federation-build] Node backend federation containerEntry must end with "/${BACKEND_FEDERATION_REMOTE_ENTRY_FILE}"; received "${containerEntry}" for ${id}.`);
|
|
95
|
+
const remoteType = 'commonjs-module';
|
|
96
|
+
const packageName = stringValue(compactApp.package);
|
|
97
|
+
const uiManifestUrl = stringValue(compactApp.backendFederation?.versionBoundary?.ui?.manifestUrl) ?? stringValue(compactApp.moduleFederation?.manifestUrl);
|
|
98
|
+
const compactDeliveryUnit = isRecord(compactApp.deliveryUnit) ? {
|
|
99
|
+
unitId: stringValue(compactApp.deliveryUnit.unitId),
|
|
100
|
+
buildMarker: stringValue(compactApp.deliveryUnit.buildMarker),
|
|
101
|
+
sourceRevision: stringValue(compactApp.deliveryUnit.sourceRevision),
|
|
102
|
+
packageName: stringValue(compactApp.deliveryUnit.packageName),
|
|
103
|
+
version: stringValue(compactApp.deliveryUnit.version)
|
|
104
|
+
} : void 0;
|
|
105
|
+
return {
|
|
106
|
+
id,
|
|
107
|
+
directory,
|
|
108
|
+
packageName,
|
|
109
|
+
port,
|
|
110
|
+
apiPrefix,
|
|
111
|
+
apiStem,
|
|
112
|
+
backendName,
|
|
113
|
+
manifestUrl,
|
|
114
|
+
containerEntry,
|
|
115
|
+
remoteType,
|
|
116
|
+
uiManifestUrl,
|
|
117
|
+
compactDeliveryUnit
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
const findBackendFederationApp = async (workspaceRoot, appDirectory)=>{
|
|
121
|
+
const compactConfigPath = node_path.join(workspaceRoot, COMPACT_CONFIG_PATH);
|
|
122
|
+
const compactConfig = await readJsonFile(compactConfigPath);
|
|
123
|
+
const apps = Array.isArray(compactConfig.topology?.apps) ? compactConfig.topology.apps : [];
|
|
124
|
+
for (const app of apps){
|
|
125
|
+
const resolved = createAppFromCompactMetadata(workspaceRoot, appDirectory, app);
|
|
126
|
+
if (resolved) return resolved;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
export { COMPACT_CONFIG_PATH, buildArtifactPathFor, buildModulePathFor, createAppFromCompactMetadata, createStampedDeliveryUnit, findBackendFederationApp, findWorkspaceRoot, normalizeRelativePath, readBuildIdentity, resolveWorkspaceSourceRevision };
|
|
@@ -0,0 +1,100 @@
|
|
|
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 { resolveUltramodernReleaseIdentity } from "@modern-js/app-tools-extensions/release-identity";
|
|
6
|
+
import { BACKEND_FEDERATION_MANIFEST_FILE, BACKEND_FEDERATION_REMOTE_ENTRY_FILE, ULTRAMODERN_BUILD_ARTIFACT_FILE, stampUltramodernBuildArtifactIdentity } from "@modern-js/backend-federation-contracts";
|
|
7
|
+
import { createBackendFederationEntryIntegrity } from "@modern-js/server-runtime-extensions/backend-federation-security/node";
|
|
8
|
+
import { createBackendManifest, createBackendRemoteEntrySource } from "./codegen.mjs";
|
|
9
|
+
import { COMPACT_CONFIG_PATH, buildArtifactPathFor, buildModulePathFor, createStampedDeliveryUnit, findBackendFederationApp, findWorkspaceRoot, readBuildIdentity, resolveWorkspaceSourceRevision } from "./config.mjs";
|
|
10
|
+
const emitBackendFederationArtifacts = async (appDirectory, distDirectory)=>{
|
|
11
|
+
const workspaceRoot = findWorkspaceRoot(appDirectory);
|
|
12
|
+
if (!workspaceRoot) return;
|
|
13
|
+
const effectApiPath = node_path.join(appDirectory, 'api/effect-api.ts');
|
|
14
|
+
const backendFederationConfigPath = node_path.join(appDirectory, 'backend-federation.config.ts');
|
|
15
|
+
if (!existsSync(effectApiPath) || !existsSync(backendFederationConfigPath)) return;
|
|
16
|
+
const app = await findBackendFederationApp(workspaceRoot, appDirectory);
|
|
17
|
+
if (!app) return;
|
|
18
|
+
const buildIdentity = await readBuildIdentity(appDirectory);
|
|
19
|
+
const compactDeliveryUnit = app.compactDeliveryUnit;
|
|
20
|
+
const hasCompactDeliveryUnit = void 0 !== compactDeliveryUnit && (void 0 !== compactDeliveryUnit.unitId || void 0 !== compactDeliveryUnit.buildMarker || void 0 !== compactDeliveryUnit.packageName || void 0 !== compactDeliveryUnit.version);
|
|
21
|
+
const hasBuildIdentity = void 0 !== buildIdentity.unitId || void 0 !== buildIdentity.buildVersion || void 0 !== buildIdentity.packageName || void 0 !== buildIdentity.version;
|
|
22
|
+
if (hasBuildIdentity) {
|
|
23
|
+
const compactConfigPath = node_path.join(workspaceRoot, COMPACT_CONFIG_PATH);
|
|
24
|
+
const buildIdentityPath = existsSync(buildArtifactPathFor(appDirectory)) ? buildArtifactPathFor(appDirectory) : buildModulePathFor(appDirectory);
|
|
25
|
+
const mismatches = [];
|
|
26
|
+
const compare = (label, a, b, leftLabel = 'deliveryUnit')=>{
|
|
27
|
+
if (void 0 !== a && void 0 !== b && a !== b) mismatches.push(`${label}: ${leftLabel}=${a} vs ultramodern-build=${b}`);
|
|
28
|
+
};
|
|
29
|
+
compare('appId', app.id, buildIdentity.appId, 'topology');
|
|
30
|
+
if (hasCompactDeliveryUnit) {
|
|
31
|
+
compare('unitId', compactDeliveryUnit?.unitId, buildIdentity.unitId);
|
|
32
|
+
compare('buildMarker/build', compactDeliveryUnit?.buildMarker, buildIdentity.buildVersion);
|
|
33
|
+
compare('packageName', compactDeliveryUnit?.packageName, buildIdentity.packageName);
|
|
34
|
+
compare('version', compactDeliveryUnit?.version, buildIdentity.version);
|
|
35
|
+
}
|
|
36
|
+
if (mismatches.length > 0) throw new Error(`[backend-federation-build] Delivery-unit identity drift between ${compactConfigPath} (topology) and ${buildIdentityPath}: ${mismatches.join('; ')}`);
|
|
37
|
+
}
|
|
38
|
+
const unitId = compactDeliveryUnit?.unitId ?? buildIdentity.unitId;
|
|
39
|
+
const generationBuildMarker = compactDeliveryUnit?.buildMarker ?? buildIdentity.buildVersion;
|
|
40
|
+
const sourceRevision = await resolveWorkspaceSourceRevision(workspaceRoot);
|
|
41
|
+
const buildVersion = generationBuildMarker && unitId ? resolveUltramodernReleaseIdentity({
|
|
42
|
+
generationBuildMarker,
|
|
43
|
+
unitId,
|
|
44
|
+
workspaceRoot
|
|
45
|
+
}).buildMarker : void 0;
|
|
46
|
+
const packageName = compactDeliveryUnit?.packageName ?? buildIdentity.packageName ?? app.packageName;
|
|
47
|
+
const version = compactDeliveryUnit?.version ?? buildIdentity.version;
|
|
48
|
+
const deliveryUnit = createStampedDeliveryUnit({
|
|
49
|
+
appId: app.id,
|
|
50
|
+
unitId,
|
|
51
|
+
buildMarker: buildVersion,
|
|
52
|
+
packageName,
|
|
53
|
+
version,
|
|
54
|
+
sourceRevision
|
|
55
|
+
});
|
|
56
|
+
const stampedBuildArtifact = buildIdentity.artifact ? stampUltramodernBuildArtifactIdentity(buildIdentity.artifact, {
|
|
57
|
+
buildMarker: buildVersion ?? buildIdentity.artifact.deliveryUnit.buildMarker,
|
|
58
|
+
sourceRevision
|
|
59
|
+
}) : void 0;
|
|
60
|
+
const resolvedApp = {
|
|
61
|
+
...app,
|
|
62
|
+
packageName,
|
|
63
|
+
version,
|
|
64
|
+
buildVersion,
|
|
65
|
+
unitId,
|
|
66
|
+
sourceRevision,
|
|
67
|
+
deliveryUnit
|
|
68
|
+
};
|
|
69
|
+
const manifestPath = node_path.join(distDirectory, BACKEND_FEDERATION_MANIFEST_FILE);
|
|
70
|
+
const entryPath = node_path.join(distDirectory, BACKEND_FEDERATION_REMOTE_ENTRY_FILE);
|
|
71
|
+
const deliveryUnitArtifactPath = stampedBuildArtifact ? node_path.join(distDirectory, ULTRAMODERN_BUILD_ARTIFACT_FILE) : void 0;
|
|
72
|
+
const entrySource = await createBackendRemoteEntrySource(workspaceRoot, resolvedApp, effectApiPath, entryPath);
|
|
73
|
+
await promises.mkdir(distDirectory, {
|
|
74
|
+
recursive: true
|
|
75
|
+
});
|
|
76
|
+
await promises.writeFile(entryPath, entrySource);
|
|
77
|
+
await promises.writeFile(manifestPath, `${JSON.stringify(createBackendManifest(workspaceRoot, distDirectory, resolvedApp, createBackendFederationEntryIntegrity(entrySource)), null, 2)}\n`);
|
|
78
|
+
if (stampedBuildArtifact && deliveryUnitArtifactPath) await promises.writeFile(deliveryUnitArtifactPath, `${JSON.stringify(stampedBuildArtifact, null, 2)}\n`);
|
|
79
|
+
return {
|
|
80
|
+
appId: resolvedApp.id,
|
|
81
|
+
manifestPath,
|
|
82
|
+
containerPath: entryPath,
|
|
83
|
+
...deliveryUnitArtifactPath ? {
|
|
84
|
+
deliveryUnitArtifactPath
|
|
85
|
+
} : {},
|
|
86
|
+
remoteName: resolvedApp.backendName,
|
|
87
|
+
remoteType: resolvedApp.remoteType
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
const emit = ()=>({
|
|
91
|
+
name: '@modern-js/backend-federation-build',
|
|
92
|
+
setup (api) {
|
|
93
|
+
api.onAfterBuild(async ()=>{
|
|
94
|
+
const { appDirectory, distDirectory } = api.getAppContext();
|
|
95
|
+
await emitBackendFederationArtifacts(appDirectory, distDirectory);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
export default emit;
|
|
100
|
+
export { emitBackendFederationArtifacts };
|