@bleedingdev/modern-js-plugin-bff-build-extensions 0.0.0 → 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/LICENSE +21 -0
- package/dist/cjs/client-artifacts.js +106 -0
- package/dist/cjs/compile.js +63 -0
- package/dist/cjs/cross-project.js +92 -0
- package/dist/cjs/generated-entries.js +92 -0
- package/dist/cjs/hono-client-codegen.js +107 -0
- package/dist/cjs/index.js +133 -0
- package/dist/cjs/server-global-vars.js +113 -0
- package/dist/esm/client-artifacts.mjs +65 -0
- package/dist/esm/compile.mjs +25 -0
- package/dist/esm/cross-project.mjs +44 -0
- package/dist/esm/generated-entries.mjs +54 -0
- package/dist/esm/hono-client-codegen.mjs +59 -0
- package/dist/esm/index.mjs +93 -0
- package/dist/esm/server-global-vars.mjs +72 -0
- package/dist/esm-node/client-artifacts.mjs +66 -0
- package/dist/esm-node/compile.mjs +26 -0
- package/dist/esm-node/cross-project.mjs +45 -0
- package/dist/esm-node/generated-entries.mjs +56 -0
- package/dist/esm-node/hono-client-codegen.mjs +61 -0
- package/dist/esm-node/index.mjs +95 -0
- package/dist/esm-node/server-global-vars.mjs +73 -0
- package/dist/types/client-artifacts.d.ts +12 -0
- package/dist/types/compile.d.ts +3 -0
- package/dist/types/cross-project.d.ts +11 -0
- package/dist/types/generated-entries.d.ts +4 -0
- package/dist/types/hono-client-codegen.d.ts +2 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/server-global-vars.d.ts +6 -0
- package/package.json +104 -8
- package/src/client-artifacts.ts +101 -0
- package/src/compile.ts +37 -0
- package/src/cross-project.ts +72 -0
- package/src/generated-entries.ts +100 -0
- package/src/hono-client-codegen.ts +83 -0
- package/src/index.ts +131 -0
- package/src/server-global-vars.ts +145 -0
- package/README.md +0 -3
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import "@modern-js/server-runtime-extensions/server-config";
|
|
3
|
+
import node_path from "node:path";
|
|
4
|
+
import { createCrossProjectApiPlugin } from "@modern-js/plugin-bff/cross-project";
|
|
5
|
+
function createCrossProjectBffPlugin(descriptor) {
|
|
6
|
+
return createCrossProjectApiPlugin({
|
|
7
|
+
...descriptor.native,
|
|
8
|
+
modifyResolvedConfig: (resolvedConfig, { sdkDistDirectory, api })=>{
|
|
9
|
+
const config = api.getConfig();
|
|
10
|
+
const configuredRuntimeFramework = config?.bff?.runtimeFramework;
|
|
11
|
+
if (configuredRuntimeFramework && configuredRuntimeFramework !== descriptor.runtimeFramework) throw new Error(`[${descriptor.native.packageName}] Runtime framework mismatch for cross-project BFF. Detected "${configuredRuntimeFramework}", but producer SDK requires "${descriptor.runtimeFramework}".`);
|
|
12
|
+
const effectEntry = descriptor.relativeEffectEntry ? node_path.join(sdkDistDirectory, descriptor.relativeEffectEntry) : void 0;
|
|
13
|
+
api.updateAppContext({
|
|
14
|
+
...effectEntry ? {
|
|
15
|
+
apiDirectory: node_path.dirname(effectEntry)
|
|
16
|
+
} : {},
|
|
17
|
+
bffRuntimeFramework: descriptor.runtimeFramework
|
|
18
|
+
});
|
|
19
|
+
resolvedConfig.bff ??= {};
|
|
20
|
+
resolvedConfig.bff.runtimeFramework = descriptor.runtimeFramework;
|
|
21
|
+
if (effectEntry) resolvedConfig.bff.effect = {
|
|
22
|
+
...resolvedConfig.bff.effect,
|
|
23
|
+
entry: effectEntry
|
|
24
|
+
};
|
|
25
|
+
resolvedConfig.bff.requestId = resolvedConfig.bff.requestId || config?.bff?.requestId || descriptor.requestId || descriptor.native.packageName || 'default';
|
|
26
|
+
const policy = resolvedConfig.bff.crossProjectPolicy;
|
|
27
|
+
resolvedConfig.bff.crossProjectPolicy = {
|
|
28
|
+
...policy,
|
|
29
|
+
enabled: policy?.enabled ?? true,
|
|
30
|
+
requireEnvelope: policy?.requireEnvelope ?? true,
|
|
31
|
+
requireOperationContext: policy?.requireOperationContext ?? true,
|
|
32
|
+
requireOperationContextDetails: policy?.requireOperationContextDetails ?? true,
|
|
33
|
+
requireOperationSchemaHash: policy?.requireOperationSchemaHash ?? true,
|
|
34
|
+
requireOperationVersion: policy?.requireOperationVersion ?? true,
|
|
35
|
+
allowUnknownOperations: policy?.allowUnknownOperations ?? false,
|
|
36
|
+
expectedOperationContracts: {
|
|
37
|
+
...policy?.expectedOperationContracts,
|
|
38
|
+
...descriptor.operationContracts
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
return resolvedConfig;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
export { createCrossProjectBffPlugin };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import __rslib_shim_module__ from "node:module";
|
|
2
|
+
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { fs, upath } from "@modern-js/utils";
|
|
5
|
+
import { BFF_REQUEST_RUNTIME } from "./client-artifacts.mjs";
|
|
6
|
+
function registerBffGeneratedEntries(api, metadata) {
|
|
7
|
+
api.modifyBffGeneratedEntries(async (entries)=>{
|
|
8
|
+
const { generation } = entries;
|
|
9
|
+
const details = metadata.get(generation);
|
|
10
|
+
if (!details) throw new Error('BFF entries require completed client artifact generation.');
|
|
11
|
+
metadata.delete(generation);
|
|
12
|
+
const packageJson = await fs.readJSON(upath.join(generation.appDirectory, 'package.json'));
|
|
13
|
+
const runtimeSpecifier = api.getNormalizedConfig().bff?.runtimeCreateRequest || BFF_REQUEST_RUNTIME;
|
|
14
|
+
const dependencies = [
|
|
15
|
+
'@modern-js/plugin-bff-build-extensions',
|
|
16
|
+
'@modern-js/plugin-bff-extensions'
|
|
17
|
+
];
|
|
18
|
+
const emitsClient = 'effect' === details.runtimeFramework || generation.apiFiles.length > 0;
|
|
19
|
+
if (runtimeSpecifier === BFF_REQUEST_RUNTIME || emitsClient && (generation.requestCreator || BFF_REQUEST_RUNTIME) === BFF_REQUEST_RUNTIME) dependencies.push('@modern-js/runtime-extensions');
|
|
20
|
+
for (const name of dependencies){
|
|
21
|
+
const specifier = packageJson.dependencies?.[name] || packageJson.devDependencies?.[name] || fs.readJSONSync(require.resolve(`${name}/package.json`)).version;
|
|
22
|
+
entries.packageDependencies[name] = specifier;
|
|
23
|
+
}
|
|
24
|
+
const descriptor = {
|
|
25
|
+
native: {
|
|
26
|
+
packageName: packageJson.name,
|
|
27
|
+
prefix: generation.prefix,
|
|
28
|
+
relativeDistPath: upath.normalize(generation.relativeDistPath),
|
|
29
|
+
relativeApiPath: upath.relative(generation.appDirectory, generation.apiDirectory),
|
|
30
|
+
relativeLambdaPath: upath.relative(generation.appDirectory, generation.lambdaDirectory)
|
|
31
|
+
},
|
|
32
|
+
runtimeFramework: details.runtimeFramework,
|
|
33
|
+
relativeEffectEntry: details.relativeEffectEntry,
|
|
34
|
+
requestId: generation.requestId,
|
|
35
|
+
operationContracts: details.operationContracts
|
|
36
|
+
};
|
|
37
|
+
const isModule = 'module' === api.getAppContext().moduleType || 'module' === packageJson.type;
|
|
38
|
+
entries.plugin = {
|
|
39
|
+
code: isModule ? `import { createCrossProjectBffPlugin } from '@modern-js/plugin-bff-build-extensions/cross-project';\nexport const crossProjectApiPlugin = () => createCrossProjectBffPlugin(${JSON.stringify(descriptor)});\n` : `'use strict';\nconst { createCrossProjectBffPlugin } = require('@modern-js/plugin-bff-build-extensions/cross-project');\nexports.crossProjectApiPlugin = () => createCrossProjectBffPlugin(${JSON.stringify(descriptor)});\n`,
|
|
40
|
+
declaration: `import type { AppTools, CliPlugin } from '@modern-js/app-tools';
|
|
41
|
+
export declare const crossProjectApiPlugin: () => CliPlugin<AppTools>;
|
|
42
|
+
`
|
|
43
|
+
};
|
|
44
|
+
const runtime = JSON.stringify(runtimeSpecifier);
|
|
45
|
+
const esmRuntime = JSON.stringify(upath.isAbsolute(runtimeSpecifier) ? pathToFileURL(runtimeSpecifier).href : runtimeSpecifier);
|
|
46
|
+
entries.runtime = {
|
|
47
|
+
code: isModule ? `import { configure as _configure } from ${esmRuntime};\nimport { createProducerClient } from '@modern-js/plugin-bff-extensions/producer-runtime';\nexport const configure = createProducerClient(_configure, { requestId: ${JSON.stringify(generation.requestId)} });\nexport const initProducerClient = configure;\n` : `'use strict';\nconst { configure: _configure } = require(${runtime});\nconst { createProducerClient } = require('@modern-js/plugin-bff-extensions/producer-runtime');\nconst configure = createProducerClient(_configure, { requestId: ${JSON.stringify(generation.requestId)} });\nexports.configure = configure;\nexports.initProducerClient = configure;\n`,
|
|
48
|
+
declaration: `type Runtime = typeof import(${runtime});
|
|
49
|
+
export declare const initProducerClient: (options?: Parameters<Runtime['configure']>[0]) => ReturnType<Runtime['configure']>;
|
|
50
|
+
export declare const configure: typeof initProducerClient;
|
|
51
|
+
`
|
|
52
|
+
};
|
|
53
|
+
return entries;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
export { registerBffGeneratedEntries };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import __rslib_shim_module__ from "node:module";
|
|
2
|
+
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
|
|
3
|
+
import node_path from "node:path";
|
|
4
|
+
import { buildOperationContractMap, createOperationSchemaHash, deriveOperationVersion } from "@modern-js/server-runtime-extensions/bff-policy/node";
|
|
5
|
+
const REQUEST_RUNTIME = '@modern-js/runtime-extensions/request-policy';
|
|
6
|
+
const modifyClient = (draft, context)=>{
|
|
7
|
+
const { appDir, requestId } = context.options;
|
|
8
|
+
draft.requestCreator = context.options.requestCreator || REQUEST_RUNTIME;
|
|
9
|
+
let version;
|
|
10
|
+
try {
|
|
11
|
+
version = require(node_path.resolve(appDir, 'package.json')).version;
|
|
12
|
+
} catch {}
|
|
13
|
+
const operationVersion = deriveOperationVersion(version);
|
|
14
|
+
const normalizedRequestId = requestId || 'default';
|
|
15
|
+
const operationContracts = buildOperationContractMap({
|
|
16
|
+
handlers: [
|
|
17
|
+
...context.handlerInfos
|
|
18
|
+
],
|
|
19
|
+
requestId: normalizedRequestId,
|
|
20
|
+
operationVersion
|
|
21
|
+
});
|
|
22
|
+
const operationEntries = context.handlerInfos.map((handlerInfo)=>{
|
|
23
|
+
const httpMethod = handlerInfo.httpMethod.toUpperCase();
|
|
24
|
+
return {
|
|
25
|
+
name: handlerInfo.name,
|
|
26
|
+
httpMethod,
|
|
27
|
+
routePath: handlerInfo.routePath,
|
|
28
|
+
schemaHash: operationContracts[`${httpMethod}:${handlerInfo.routePath}`]?.schemaHash ?? ''
|
|
29
|
+
};
|
|
30
|
+
}).sort((a, b)=>`${a.routePath}:${a.httpMethod}:${a.name}`.localeCompare(`${b.routePath}:${b.httpMethod}:${b.name}`));
|
|
31
|
+
const schemaHash = createOperationSchemaHash(operationEntries, normalizedRequestId);
|
|
32
|
+
for (const { handlerInfo, optionProperties } of draft.handlers){
|
|
33
|
+
if ('upload' === handlerInfo.action && !requestId) continue;
|
|
34
|
+
const method = handlerInfo.httpMethod.toUpperCase();
|
|
35
|
+
const operationContext = {
|
|
36
|
+
operationId: handlerInfo.name,
|
|
37
|
+
routePath: handlerInfo.routePath,
|
|
38
|
+
method,
|
|
39
|
+
schemaHash: operationContracts[`${method}:${handlerInfo.routePath}`]?.schemaHash ?? '',
|
|
40
|
+
operationVersion
|
|
41
|
+
};
|
|
42
|
+
optionProperties.push(`operationContext: ${JSON.stringify(operationContext)}`);
|
|
43
|
+
}
|
|
44
|
+
if (requestId) {
|
|
45
|
+
draft.imports.push(`import * as requestRuntime from ${JSON.stringify(draft.requestCreator)};`, 'import { createProducerClient } from "@modern-js/plugin-bff-extensions/producer-runtime";');
|
|
46
|
+
draft.statements.push(`export const initProducerClient = (options = {}) => {
|
|
47
|
+
const configure = requestRuntime.configure;
|
|
48
|
+
if (typeof configure !== 'function') {
|
|
49
|
+
console.warn('[modernjs] Compatibility request creator path does not expose configure(); use default @modern-js/create-request or migrate the compatibility path.');
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
return createProducerClient(configure, { requestId: ${JSON.stringify(requestId)} })(options);
|
|
53
|
+
};`);
|
|
54
|
+
}
|
|
55
|
+
draft.statements.push(`export const operationVersion = ${String(operationVersion)};`, `export const operationSchemaHash = ${JSON.stringify(schemaHash)};`, `export const operationManifest = ${JSON.stringify({
|
|
56
|
+
operationVersion,
|
|
57
|
+
schemaHash,
|
|
58
|
+
operations: operationEntries
|
|
59
|
+
}, null, 2)};`);
|
|
60
|
+
};
|
|
61
|
+
export { modifyClient };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import __rslib_shim_module__ from "node:module";
|
|
2
|
+
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
|
|
3
|
+
import "@modern-js/server-runtime-extensions/server-config";
|
|
4
|
+
import { bffPlugin } from "@modern-js/plugin-bff";
|
|
5
|
+
import { fs, upath } from "@modern-js/utils";
|
|
6
|
+
import { BFF_REQUEST_RUNTIME, registerBffClientArtifacts } from "./client-artifacts.mjs";
|
|
7
|
+
import { registerBffCompilation } from "./compile.mjs";
|
|
8
|
+
import { registerBffGeneratedEntries } from "./generated-entries.mjs";
|
|
9
|
+
const src_bffPlugin = ()=>({
|
|
10
|
+
name: '@modern-js/plugin-bff-build-extensions',
|
|
11
|
+
usePlugins: [
|
|
12
|
+
bffPlugin()
|
|
13
|
+
],
|
|
14
|
+
pre: [
|
|
15
|
+
'@modern-js/plugin-bff'
|
|
16
|
+
],
|
|
17
|
+
setup: (api)=>{
|
|
18
|
+
const hooks = api.getHooks();
|
|
19
|
+
for (const name of [
|
|
20
|
+
'onBeforeBffCompile',
|
|
21
|
+
'onAfterBffCompile',
|
|
22
|
+
'modifyBffClientArtifacts',
|
|
23
|
+
'modifyBffGeneratedEntries'
|
|
24
|
+
])if ('function' != typeof hooks[name]?.tap) throw new Error(`Native BFF build hook ${name} is unavailable.`);
|
|
25
|
+
api.config(()=>({
|
|
26
|
+
bff: {
|
|
27
|
+
requestCreator: BFF_REQUEST_RUNTIME,
|
|
28
|
+
runtimeCreateRequest: BFF_REQUEST_RUNTIME,
|
|
29
|
+
clientCodegenPlugin: require.resolve('@modern-js/plugin-bff-build-extensions/hono-client-codegen')
|
|
30
|
+
}
|
|
31
|
+
}));
|
|
32
|
+
const runtimeFramework = api.getConfig()?.bff?.runtimeFramework;
|
|
33
|
+
if (void 0 !== runtimeFramework && 'hono' !== runtimeFramework && 'effect' !== runtimeFramework) throw new Error(`Unsupported Effect BFF runtime "${runtimeFramework}".`);
|
|
34
|
+
api.updateAppContext({
|
|
35
|
+
bffRuntimeFramework: runtimeFramework ?? 'effect'
|
|
36
|
+
});
|
|
37
|
+
api._internalServerPlugins(({ plugins })=>{
|
|
38
|
+
const nativePlugins = plugins.filter((plugin)=>'@modern-js/plugin-bff/server-plugin' === plugin.name);
|
|
39
|
+
if (1 !== nativePlugins.length) throw new Error('Expected exactly one native BFF server plugin.');
|
|
40
|
+
const native = nativePlugins[0];
|
|
41
|
+
const runtimeAdapters = native.options?.runtimeAdapters ?? {};
|
|
42
|
+
const effect = '@modern-js/plugin-bff-extensions/effect-adapter';
|
|
43
|
+
const honoRouteBinder = '@modern-js/plugin-bff-extensions/hono/node';
|
|
44
|
+
if (native.options?.honoRouteBinder !== void 0 && native.options.honoRouteBinder !== honoRouteBinder) throw new Error('The Hono BFF route binder is already configured.');
|
|
45
|
+
if (void 0 !== runtimeAdapters.effect && runtimeAdapters.effect !== effect) throw new Error('The Effect BFF runtime adapter is already configured.');
|
|
46
|
+
native.options = {
|
|
47
|
+
...native.options,
|
|
48
|
+
honoRouteBinder,
|
|
49
|
+
runtimeAdapters: {
|
|
50
|
+
...runtimeAdapters,
|
|
51
|
+
effect
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
return {
|
|
55
|
+
plugins
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
const metadata = new WeakMap();
|
|
59
|
+
registerBffCompilation(api);
|
|
60
|
+
registerBffClientArtifacts(api, metadata);
|
|
61
|
+
registerBffGeneratedEntries(api, metadata);
|
|
62
|
+
api.modifyBundlerChain(async (chain, { isServer, CHAIN_ID })=>{
|
|
63
|
+
if ('effect' !== api.getAppContext().bffRuntimeFramework) return;
|
|
64
|
+
const { appDirectory, apiDirectory, port } = api.getAppContext();
|
|
65
|
+
const bff = api.getNormalizedConfig().bff;
|
|
66
|
+
const { resolveEffectEntryFile } = await import("@modern-js/plugin-bff-extensions/client-generator");
|
|
67
|
+
const entry = resolveEffectEntryFile({
|
|
68
|
+
appDir: appDirectory,
|
|
69
|
+
apiDir: apiDirectory,
|
|
70
|
+
effectEntry: bff?.effect?.entry
|
|
71
|
+
});
|
|
72
|
+
if (!entry) throw new Error(`Cannot resolve Effect BFF entry in ${apiDirectory}.`);
|
|
73
|
+
const canonical = (file)=>upath.normalize(fs.existsSync(file) ? fs.realpathSync(file) : upath.resolve(file));
|
|
74
|
+
const entryPath = canonical(entry);
|
|
75
|
+
const isEntry = (resource)=>canonical(resource) === entryPath;
|
|
76
|
+
chain.module.rule(CHAIN_ID.RULE.JS).exclude.add(isEntry);
|
|
77
|
+
chain.module.rule('js-bff-api').exclude.add(isEntry);
|
|
78
|
+
chain.module.rule('js-bff-effect-entry').test(isEntry).use('effect-bff-loader').loader(require.resolve('@modern-js/plugin-bff-extensions/effect-source-loader/rspack-loader')).options({
|
|
79
|
+
appDir: appDirectory,
|
|
80
|
+
apiDir: apiDirectory,
|
|
81
|
+
effectEntry: entry,
|
|
82
|
+
prefix: Array.isArray(bff?.prefix) ? bff.prefix[0] : bff?.prefix || '/api',
|
|
83
|
+
port,
|
|
84
|
+
target: isServer ? 'server' : 'client',
|
|
85
|
+
requestId: bff?.requestId,
|
|
86
|
+
requestCreator: bff?.requestCreator || BFF_REQUEST_RUNTIME,
|
|
87
|
+
httpMethodDecider: bff?.httpMethodDecider,
|
|
88
|
+
effectDataPlatformBatch: bff?.effect?.dataPlatform?.batch
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
const src = src_bffPlugin;
|
|
94
|
+
export default src;
|
|
95
|
+
export { src_bffPlugin as bffPlugin };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { applyOptionsChain, fs, upath } from "@modern-js/utils";
|
|
3
|
+
import { transform } from "@swc/core";
|
|
4
|
+
const RELEASE_IDENTITY_GLOBAL_NAMES = [
|
|
5
|
+
'ULTRAMODERN_BUILD_MARKER',
|
|
6
|
+
'ULTRAMODERN_SOURCE_REVISION',
|
|
7
|
+
'ULTRAMODERN_RELEASE_VERSION'
|
|
8
|
+
];
|
|
9
|
+
const collectJavaScriptFiles = (directory)=>Promise.resolve().then(()=>fs.readdir(directory, {
|
|
10
|
+
withFileTypes: true
|
|
11
|
+
})).then((entries)=>Promise.all(entries.map((entry)=>{
|
|
12
|
+
const absolutePath = upath.join(directory, entry.name);
|
|
13
|
+
if (entry.isDirectory()) return collectJavaScriptFiles(absolutePath);
|
|
14
|
+
return /\.(?:c|m)?js$/u.test(entry.name) ? [
|
|
15
|
+
absolutePath
|
|
16
|
+
] : [];
|
|
17
|
+
})).then((nested)=>nested.flat()));
|
|
18
|
+
const serializeServerGlobalVars = (options)=>{
|
|
19
|
+
const globalVars = applyOptionsChain({}, options, {
|
|
20
|
+
env: 'server',
|
|
21
|
+
target: 'node'
|
|
22
|
+
});
|
|
23
|
+
return Object.fromEntries(Object.entries(globalVars).map(([key, value])=>{
|
|
24
|
+
let serialized;
|
|
25
|
+
try {
|
|
26
|
+
serialized = JSON.stringify(value);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
throw new Error(`source.globalVars["${key}"] cannot be serialized exactly for BFF compilation.`, {
|
|
29
|
+
cause: error
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
if (void 0 === serialized) throw new Error(`source.globalVars["${key}"] cannot be serialized exactly for BFF compilation.`);
|
|
33
|
+
return [
|
|
34
|
+
key,
|
|
35
|
+
serialized
|
|
36
|
+
];
|
|
37
|
+
}));
|
|
38
|
+
};
|
|
39
|
+
const transformServerGlobalVars = (outputDirectories, globalVars)=>Promise.resolve().then(()=>{
|
|
40
|
+
if (0 === Object.keys(globalVars).length) return;
|
|
41
|
+
return Promise.all(outputDirectories.map((directory)=>fs.pathExists(directory).then((directoryExists)=>directoryExists ? directory : void 0))).then((existingDirectories)=>{
|
|
42
|
+
const filesPromise = Promise.all(existingDirectories.filter((directory)=>void 0 !== directory).map(collectJavaScriptFiles)).then((files)=>files.flat());
|
|
43
|
+
const releaseIdentityBanner = RELEASE_IDENTITY_GLOBAL_NAMES.every((name)=>Object.hasOwn(globalVars, name)) ? `${RELEASE_IDENTITY_GLOBAL_NAMES.map((name)=>`void ${globalVars[name]};`).join('')}\n` : '';
|
|
44
|
+
return filesPromise.then((files)=>Promise.all(files.map((filename)=>fs.readFile(filename, 'utf8').then((source)=>{
|
|
45
|
+
const sourceMapFilename = `${filename}.map`;
|
|
46
|
+
return fs.pathExists(sourceMapFilename).then((sourceMapExists)=>{
|
|
47
|
+
const inputSourceMapPromise = sourceMapExists ? fs.readFile(sourceMapFilename, 'utf8') : Promise.resolve(void 0);
|
|
48
|
+
return inputSourceMapPromise.then((inputSourceMap)=>transform(`${releaseIdentityBanner}${source}`, {
|
|
49
|
+
filename,
|
|
50
|
+
inputSourceMap,
|
|
51
|
+
sourceMaps: sourceMapExists,
|
|
52
|
+
jsc: {
|
|
53
|
+
parser: {
|
|
54
|
+
syntax: "ecmascript"
|
|
55
|
+
},
|
|
56
|
+
preserveAllComments: true,
|
|
57
|
+
target: 'es2024',
|
|
58
|
+
transform: {
|
|
59
|
+
optimizer: {
|
|
60
|
+
globals: {
|
|
61
|
+
vars: globalVars
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}).then((result)=>fs.writeFile(filename, result.code).then(()=>{
|
|
67
|
+
if (sourceMapExists && void 0 !== result.map && result.map.length > 0) return fs.writeFile(sourceMapFilename, result.map);
|
|
68
|
+
})));
|
|
69
|
+
});
|
|
70
|
+
}))));
|
|
71
|
+
}).then(()=>void 0);
|
|
72
|
+
});
|
|
73
|
+
export { serializeServerGlobalVars, transformServerGlobalVars };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { AppTools, BffGeneration } from '@modern-js/app-tools';
|
|
2
|
+
import type { CLIPluginAPI } from '@modern-js/plugin';
|
|
3
|
+
import type { GeneratedEffectClientArtifacts } from '@modern-js/plugin-bff-extensions/client-generator';
|
|
4
|
+
import { type OperationContractMap } from '@modern-js/server-runtime-extensions/bff-policy/node';
|
|
5
|
+
export declare const BFF_REQUEST_RUNTIME = "@modern-js/runtime-extensions/request-policy";
|
|
6
|
+
export interface BffGenerationMetadata {
|
|
7
|
+
runtimeFramework: 'effect' | 'hono';
|
|
8
|
+
relativeEffectEntry: string;
|
|
9
|
+
operationContracts: OperationContractMap;
|
|
10
|
+
effectArtifacts?: GeneratedEffectClientArtifacts;
|
|
11
|
+
}
|
|
12
|
+
export declare function registerBffClientArtifacts(api: CLIPluginAPI<AppTools>, metadata: WeakMap<BffGeneration, BffGenerationMetadata>): void;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import '@modern-js/server-runtime-extensions/server-config';
|
|
2
|
+
import { type CrossProjectApiPluginOptions } from '@modern-js/plugin-bff/cross-project';
|
|
3
|
+
import type { OperationContractMap } from '@modern-js/server-runtime-extensions/bff-policy/node';
|
|
4
|
+
export interface CrossProjectBffDescriptor {
|
|
5
|
+
native: Omit<CrossProjectApiPluginOptions, 'modifyResolvedConfig'>;
|
|
6
|
+
runtimeFramework: 'hono' | 'effect';
|
|
7
|
+
relativeEffectEntry: string;
|
|
8
|
+
requestId: string;
|
|
9
|
+
operationContracts: OperationContractMap;
|
|
10
|
+
}
|
|
11
|
+
export declare function createCrossProjectBffPlugin(descriptor: CrossProjectBffDescriptor): import("@modern-js/app-tools").CliPlugin<Required<import("@modern-js/plugin").CLIPluginExtends<import("@modern-js/app-tools").AppToolsUserConfig, import("@modern-js/app-tools").AppToolsNormalizedConfig<import("@modern-js/app-tools").AppToolsUserConfig>, import("@modern-js/app-tools").AppToolsExtendContext, import("@modern-js/app-tools").AppToolsExtendAPI, import("@modern-js/app-tools").AppToolsExtendHooks, {}, {}>>>;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { AppTools, BffGeneration } from '@modern-js/app-tools';
|
|
2
|
+
import type { CLIPluginAPI } from '@modern-js/plugin';
|
|
3
|
+
import { type BffGenerationMetadata } from './client-artifacts.js';
|
|
4
|
+
export declare function registerBffGeneratedEntries(api: CLIPluginAPI<AppTools>, metadata: WeakMap<BffGeneration, BffGenerationMetadata>): void;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { AppTools } from '@modern-js/app-tools';
|
|
2
|
+
type SerializedGlobalVars = Record<string, string>;
|
|
3
|
+
type ServerGlobalVarsOptions = NonNullable<NonNullable<AppTools['config']>['source']>['globalVars'];
|
|
4
|
+
export declare const serializeServerGlobalVars: (options: ServerGlobalVarsOptions) => SerializedGlobalVars;
|
|
5
|
+
export declare const transformServerGlobalVars: (outputDirectories: string[], globalVars: SerializedGlobalVars) => Promise<undefined>;
|
|
6
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,16 +1,112 @@
|
|
|
1
1
|
{
|
|
2
|
-
"
|
|
3
|
-
"
|
|
4
|
-
|
|
5
|
-
"license": "MIT",
|
|
6
|
-
"repository": {
|
|
7
|
-
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/BleedingDev/ultramodern.js.git"
|
|
2
|
+
"homepage": "https://github.com/BleedingDev/ultramodern.js#readme",
|
|
3
|
+
"bugs": {
|
|
4
|
+
"url": "https://github.com/BleedingDev/ultramodern.js/issues"
|
|
9
5
|
},
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"version": "3.9.0-ultramodern.5",
|
|
10
8
|
"files": [
|
|
11
|
-
"
|
|
9
|
+
"dist",
|
|
10
|
+
"src"
|
|
12
11
|
],
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=26.7.0"
|
|
14
|
+
},
|
|
15
|
+
"sideEffects": false,
|
|
13
16
|
"publishConfig": {
|
|
14
17
|
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"name": "@bleedingdev/modern-js-plugin-bff-build-extensions",
|
|
20
|
+
"description": "Fork-owned BFF build lifecycle, instrumentation, and producer SDK composition.",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/BleedingDev/ultramodern.js.git",
|
|
24
|
+
"directory": "packages/cli/plugin-bff-build-extensions"
|
|
25
|
+
},
|
|
26
|
+
"main": "./dist/cjs/index.js",
|
|
27
|
+
"module": "./dist/esm-node/index.mjs",
|
|
28
|
+
"types": "./dist/types/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/types/index.d.ts",
|
|
32
|
+
"node": {
|
|
33
|
+
"import": "./dist/esm-node/index.mjs",
|
|
34
|
+
"require": "./dist/cjs/index.js"
|
|
35
|
+
},
|
|
36
|
+
"default": "./dist/cjs/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./cross-project": {
|
|
39
|
+
"types": "./dist/types/cross-project.d.ts",
|
|
40
|
+
"node": {
|
|
41
|
+
"import": "./dist/esm-node/cross-project.mjs",
|
|
42
|
+
"require": "./dist/cjs/cross-project.js"
|
|
43
|
+
},
|
|
44
|
+
"default": "./dist/cjs/cross-project.js"
|
|
45
|
+
},
|
|
46
|
+
"./package.json": "./package.json",
|
|
47
|
+
"./hono-client-codegen": {
|
|
48
|
+
"types": "./dist/types/hono-client-codegen.d.ts",
|
|
49
|
+
"node": {
|
|
50
|
+
"import": "./dist/esm-node/hono-client-codegen.mjs",
|
|
51
|
+
"require": "./dist/cjs/hono-client-codegen.js"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"typesVersions": {
|
|
56
|
+
"*": {
|
|
57
|
+
"cross-project": [
|
|
58
|
+
"./dist/types/cross-project.d.ts",
|
|
59
|
+
"./src/cross-project.ts"
|
|
60
|
+
],
|
|
61
|
+
"hono-client-codegen": [
|
|
62
|
+
"./dist/types/hono-client-codegen.d.ts",
|
|
63
|
+
"./src/hono-client-codegen.ts"
|
|
64
|
+
]
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
"dependencies": {
|
|
68
|
+
"@modern-js/bff-core": "npm:@bleedingdev/modern-js-bff-core@3.9.0-ultramodern.5",
|
|
69
|
+
"@modern-js/plugin": "npm:@bleedingdev/modern-js-plugin@3.9.0-ultramodern.5",
|
|
70
|
+
"@modern-js/plugin-bff": "npm:@bleedingdev/modern-js-plugin-bff@3.9.0-ultramodern.5",
|
|
71
|
+
"@modern-js/plugin-bff-extensions": "npm:@bleedingdev/modern-js-plugin-bff-extensions@3.9.0-ultramodern.5",
|
|
72
|
+
"@modern-js/runtime-extensions": "npm:@bleedingdev/modern-js-runtime-extensions@3.9.0-ultramodern.5",
|
|
73
|
+
"@modern-js/server-runtime-extensions": "npm:@bleedingdev/modern-js-server-runtime-extensions@3.9.0-ultramodern.5",
|
|
74
|
+
"@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.9.0-ultramodern.5",
|
|
75
|
+
"@swc/core": "1.16.2"
|
|
76
|
+
},
|
|
77
|
+
"peerDependencies": {
|
|
78
|
+
"@effect/opentelemetry": "4.0.0-rc.112",
|
|
79
|
+
"@modern-js/app-tools": "3.9.0-ultramodern.5",
|
|
80
|
+
"effect": "4.0.0-rc.112"
|
|
81
|
+
},
|
|
82
|
+
"peerDependenciesMeta": {
|
|
83
|
+
"@effect/opentelemetry": {
|
|
84
|
+
"optional": true
|
|
85
|
+
},
|
|
86
|
+
"effect": {
|
|
87
|
+
"optional": true
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
"devDependencies": {
|
|
91
|
+
"@effect/opentelemetry": "4.0.0-rc.112",
|
|
92
|
+
"@modern-js/app-tools": "npm:@bleedingdev/modern-js-app-tools@3.9.0-ultramodern.5",
|
|
93
|
+
"@modern-js/bff-effect": "npm:@bleedingdev/modern-js-bff-effect@3.9.0-ultramodern.5",
|
|
94
|
+
"@modern-js/builder": "npm:@bleedingdev/modern-js-builder@3.9.0-ultramodern.5",
|
|
95
|
+
"@modern-js/server-core": "npm:@bleedingdev/modern-js-server-core@3.9.0-ultramodern.5",
|
|
96
|
+
"@modern-js/tsconfig": "npm:@bleedingdev/modern-js-tsconfig@3.9.0-ultramodern.5",
|
|
97
|
+
"@modern-js/types": "npm:@bleedingdev/modern-js-types@3.9.0-ultramodern.5",
|
|
98
|
+
"@rsbuild/core": "2.2.3",
|
|
99
|
+
"@rslib/core": "1.0.0",
|
|
100
|
+
"@scripts/rstest-config": "2.66.0",
|
|
101
|
+
"@types/node": "^26.4.1",
|
|
102
|
+
"@typescript/native-preview": "7.0.0-dev.20260707.2",
|
|
103
|
+
"effect": "4.0.0-rc.112",
|
|
104
|
+
"esbuild": "^0.28.2",
|
|
105
|
+
"zod": "^4.5.4"
|
|
106
|
+
},
|
|
107
|
+
"scripts": {
|
|
108
|
+
"dev": "rslib build --watch",
|
|
109
|
+
"build": "rslib build",
|
|
110
|
+
"test": "rstest"
|
|
15
111
|
}
|
|
16
112
|
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AppTools,
|
|
3
|
+
BffClientArtifacts,
|
|
4
|
+
BffGeneration,
|
|
5
|
+
} from '@modern-js/app-tools';
|
|
6
|
+
import { ApiRouter } from '@modern-js/bff-core';
|
|
7
|
+
import type { CLIPluginAPI } from '@modern-js/plugin';
|
|
8
|
+
import type { GeneratedEffectClientArtifacts } from '@modern-js/plugin-bff-extensions/client-generator';
|
|
9
|
+
import {
|
|
10
|
+
buildOperationContractMap,
|
|
11
|
+
deriveOperationVersion,
|
|
12
|
+
type OperationContractMap,
|
|
13
|
+
} from '@modern-js/server-runtime-extensions/bff-policy/node';
|
|
14
|
+
import { fs, upath as path } from '@modern-js/utils';
|
|
15
|
+
|
|
16
|
+
export const BFF_REQUEST_RUNTIME =
|
|
17
|
+
'@modern-js/runtime-extensions/request-policy';
|
|
18
|
+
|
|
19
|
+
export interface BffGenerationMetadata {
|
|
20
|
+
runtimeFramework: 'effect' | 'hono';
|
|
21
|
+
relativeEffectEntry: string;
|
|
22
|
+
operationContracts: OperationContractMap;
|
|
23
|
+
effectArtifacts?: GeneratedEffectClientArtifacts;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function registerBffClientArtifacts(
|
|
27
|
+
api: CLIPluginAPI<AppTools>,
|
|
28
|
+
metadata: WeakMap<BffGeneration, BffGenerationMetadata>,
|
|
29
|
+
) {
|
|
30
|
+
api.modifyBffClientArtifacts(async (context: BffClientArtifacts) => {
|
|
31
|
+
const { generation } = context;
|
|
32
|
+
const config = api.getNormalizedConfig();
|
|
33
|
+
if (api.getAppContext().bffRuntimeFramework === 'effect') {
|
|
34
|
+
const { generateEffectClient, resolveEffectEntryPaths } = await import(
|
|
35
|
+
'@modern-js/plugin-bff-extensions/client-generator'
|
|
36
|
+
);
|
|
37
|
+
const { sourceEffectEntry, relativeEffectEntry } =
|
|
38
|
+
resolveEffectEntryPaths({
|
|
39
|
+
appDir: generation.appDirectory,
|
|
40
|
+
apiDir: generation.apiDirectory,
|
|
41
|
+
effectEntry: config.bff?.effect?.entry,
|
|
42
|
+
});
|
|
43
|
+
if (!sourceEffectEntry)
|
|
44
|
+
throw new Error(
|
|
45
|
+
`Cannot resolve Effect BFF entry in ${generation.apiDirectory}.`,
|
|
46
|
+
);
|
|
47
|
+
const artifacts = await generateEffectClient({
|
|
48
|
+
appDir: generation.appDirectory,
|
|
49
|
+
apiDir: generation.apiDirectory,
|
|
50
|
+
resourcePath: sourceEffectEntry,
|
|
51
|
+
prefix: generation.prefix,
|
|
52
|
+
port: Number(generation.port),
|
|
53
|
+
target: 'bundle',
|
|
54
|
+
requestId: generation.requestId,
|
|
55
|
+
requestCreator: generation.requestCreator || BFF_REQUEST_RUNTIME,
|
|
56
|
+
httpMethodDecider: generation.httpMethodDecider,
|
|
57
|
+
dataPlatformBatch: config.bff?.effect?.dataPlatform?.batch,
|
|
58
|
+
});
|
|
59
|
+
if (!artifacts)
|
|
60
|
+
throw new Error(
|
|
61
|
+
`Effect cross-project client generation failed for ${sourceEffectEntry}.`,
|
|
62
|
+
);
|
|
63
|
+
context.additionalArtifacts.push({
|
|
64
|
+
sourcePath: path.relative(generation.apiDirectory, sourceEffectEntry),
|
|
65
|
+
code: artifacts.code,
|
|
66
|
+
declaration: artifacts.declaration,
|
|
67
|
+
});
|
|
68
|
+
metadata.set(generation, {
|
|
69
|
+
runtimeFramework: 'effect',
|
|
70
|
+
relativeEffectEntry,
|
|
71
|
+
operationContracts: artifacts.operationContracts,
|
|
72
|
+
effectArtifacts: artifacts,
|
|
73
|
+
});
|
|
74
|
+
} else {
|
|
75
|
+
const router = new ApiRouter({
|
|
76
|
+
appDir: generation.appDirectory,
|
|
77
|
+
apiDir: generation.apiDirectory,
|
|
78
|
+
lambdaDir: generation.lambdaDirectory,
|
|
79
|
+
prefix: generation.prefix,
|
|
80
|
+
httpMethodDecider: generation.httpMethodDecider,
|
|
81
|
+
isBuild: true,
|
|
82
|
+
});
|
|
83
|
+
const packageJson = await fs.readJSON(
|
|
84
|
+
path.join(generation.appDirectory, 'package.json'),
|
|
85
|
+
);
|
|
86
|
+
const operationContracts = generation.existLambda
|
|
87
|
+
? buildOperationContractMap({
|
|
88
|
+
handlers: await router.getApiHandlers(),
|
|
89
|
+
requestId: generation.requestId,
|
|
90
|
+
operationVersion: deriveOperationVersion(packageJson.version),
|
|
91
|
+
})
|
|
92
|
+
: {};
|
|
93
|
+
metadata.set(generation, {
|
|
94
|
+
runtimeFramework: 'hono',
|
|
95
|
+
relativeEffectEntry: '',
|
|
96
|
+
operationContracts,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return context;
|
|
100
|
+
});
|
|
101
|
+
}
|
package/src/compile.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { AppTools, BffCompilation } from '@modern-js/app-tools';
|
|
2
|
+
import type { CLIPluginAPI } from '@modern-js/plugin';
|
|
3
|
+
import {
|
|
4
|
+
serializeServerGlobalVars,
|
|
5
|
+
transformServerGlobalVars,
|
|
6
|
+
} from './server-global-vars';
|
|
7
|
+
|
|
8
|
+
export function registerBffCompilation(api: CLIPluginAPI<AppTools>) {
|
|
9
|
+
const serialized = new WeakMap<
|
|
10
|
+
BffCompilation,
|
|
11
|
+
ReturnType<typeof serializeServerGlobalVars>
|
|
12
|
+
>();
|
|
13
|
+
api.onBeforeBffCompile(context => {
|
|
14
|
+
serialized.set(
|
|
15
|
+
context,
|
|
16
|
+
serializeServerGlobalVars(api.getNormalizedConfig().source.globalVars),
|
|
17
|
+
);
|
|
18
|
+
});
|
|
19
|
+
api.onAfterBffCompile(async context => {
|
|
20
|
+
const globals = serialized.get(context);
|
|
21
|
+
if (!globals)
|
|
22
|
+
throw new Error('BFF compilation completed without its before hook.');
|
|
23
|
+
serialized.delete(context);
|
|
24
|
+
await transformServerGlobalVars([...context.outputDirectories], globals);
|
|
25
|
+
if (api.getAppContext().bffRuntimeFramework !== 'effect') return;
|
|
26
|
+
const { bundleBuiltEffectEntryForNode } = await import(
|
|
27
|
+
'@modern-js/plugin-bff-extensions/client-generator'
|
|
28
|
+
);
|
|
29
|
+
await bundleBuiltEffectEntryForNode({
|
|
30
|
+
appDir: context.appDirectory,
|
|
31
|
+
apiDir: context.apiDirectory,
|
|
32
|
+
distDir: context.distDirectory,
|
|
33
|
+
effectEntry: api.getNormalizedConfig().bff?.effect?.entry,
|
|
34
|
+
format: context.moduleType === 'module' ? 'esm' : 'cjs',
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|