@bleedingdev/modern-js-plugin-bff-build-extensions 0.0.0 → 3.9.0-ultramodern.6
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 +94 -0
- package/dist/cjs/compile.js +64 -0
- package/dist/cjs/cross-project.js +92 -0
- package/dist/cjs/generated-entries.js +96 -0
- package/dist/cjs/hono-client-codegen.js +107 -0
- package/dist/cjs/index.js +129 -0
- package/dist/cjs/server-global-vars.js +113 -0
- package/dist/esm/client-artifacts.mjs +53 -0
- package/dist/esm/compile.mjs +26 -0
- package/dist/esm/cross-project.mjs +44 -0
- package/dist/esm/generated-entries.mjs +58 -0
- package/dist/esm/hono-client-codegen.mjs +59 -0
- package/dist/esm/index.mjs +89 -0
- package/dist/esm/server-global-vars.mjs +72 -0
- package/dist/esm-node/client-artifacts.mjs +54 -0
- package/dist/esm-node/compile.mjs +27 -0
- package/dist/esm-node/cross-project.mjs +45 -0
- package/dist/esm-node/generated-entries.mjs +60 -0
- package/dist/esm-node/hono-client-codegen.mjs +61 -0
- package/dist/esm-node/index.mjs +91 -0
- package/dist/esm-node/server-global-vars.mjs +73 -0
- package/dist/types/client-artifacts.d.ts +10 -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 +87 -0
- package/src/compile.ts +39 -0
- package/src/cross-project.ts +72 -0
- package/src/generated-entries.ts +105 -0
- package/src/hono-client-codegen.ts +83 -0
- package/src/index.ts +128 -0
- package/src/server-global-vars.ts +145 -0
- package/README.md +0 -3
|
@@ -0,0 +1,60 @@
|
|
|
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 operation contract collection.');
|
|
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 = 'hono' === details.runtimeFramework && generation.apiFiles.length > 0;
|
|
19
|
+
if ('hono' === details.runtimeFramework && (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
|
+
if ('effect' === details.runtimeFramework) {
|
|
45
|
+
entries.runtime = null;
|
|
46
|
+
return entries;
|
|
47
|
+
}
|
|
48
|
+
const runtime = JSON.stringify(runtimeSpecifier);
|
|
49
|
+
const esmRuntime = JSON.stringify(upath.isAbsolute(runtimeSpecifier) ? pathToFileURL(runtimeSpecifier).href : runtimeSpecifier);
|
|
50
|
+
entries.runtime = {
|
|
51
|
+
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`,
|
|
52
|
+
declaration: `type Runtime = typeof import(${runtime});
|
|
53
|
+
export declare const initProducerClient: (options?: Parameters<Runtime['configure']>[0]) => ReturnType<Runtime['configure']>;
|
|
54
|
+
export declare const configure: typeof initProducerClient;
|
|
55
|
+
`
|
|
56
|
+
};
|
|
57
|
+
return entries;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
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,91 @@
|
|
|
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, { CHAIN_ID })=>{
|
|
63
|
+
if ('effect' !== api.getAppContext().bffRuntimeFramework) return;
|
|
64
|
+
const { appDirectory, apiDirectory } = api.getAppContext();
|
|
65
|
+
const bff = api.getNormalizedConfig().bff;
|
|
66
|
+
if (!fs.existsSync(apiDirectory) && bff?.effect?.entry === void 0) return;
|
|
67
|
+
const { resolveEffectEntryFile } = await import("@modern-js/plugin-bff-extensions/effect-source-loader");
|
|
68
|
+
const entry = resolveEffectEntryFile({
|
|
69
|
+
appDir: appDirectory,
|
|
70
|
+
apiDir: apiDirectory,
|
|
71
|
+
effectEntry: bff?.effect?.entry
|
|
72
|
+
});
|
|
73
|
+
if (!entry) throw new Error(`Cannot resolve Effect BFF entry in ${apiDirectory}.`);
|
|
74
|
+
const canonical = (file)=>upath.normalize(fs.existsSync(file) ? fs.realpathSync(file) : upath.resolve(file));
|
|
75
|
+
const entryPath = canonical(entry);
|
|
76
|
+
const isEntry = (resource)=>canonical(resource) === entryPath;
|
|
77
|
+
chain.module.rule(CHAIN_ID.RULE.JS).exclude.add(isEntry);
|
|
78
|
+
chain.module.rule('js-bff-api').exclude.add(isEntry);
|
|
79
|
+
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({
|
|
80
|
+
appDir: appDirectory,
|
|
81
|
+
apiDir: apiDirectory,
|
|
82
|
+
effectEntry: entry,
|
|
83
|
+
prefix: Array.isArray(bff?.prefix) ? bff.prefix[0] : bff?.prefix || '/api',
|
|
84
|
+
requestId: bff?.requestId
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
const src = src_bffPlugin;
|
|
90
|
+
export default src;
|
|
91
|
+
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,10 @@
|
|
|
1
|
+
import type { AppTools, BffGeneration } from '@modern-js/app-tools';
|
|
2
|
+
import type { CLIPluginAPI } from '@modern-js/plugin';
|
|
3
|
+
import { type OperationContractMap } from '@modern-js/server-runtime-extensions/bff-policy/node';
|
|
4
|
+
export declare const BFF_REQUEST_RUNTIME = "@modern-js/runtime-extensions/request-policy";
|
|
5
|
+
export interface BffGenerationMetadata {
|
|
6
|
+
runtimeFramework: 'effect' | 'hono';
|
|
7
|
+
relativeEffectEntry: string;
|
|
8
|
+
operationContracts: OperationContractMap;
|
|
9
|
+
}
|
|
10
|
+
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.6",
|
|
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.6",
|
|
69
|
+
"@modern-js/plugin": "npm:@bleedingdev/modern-js-plugin@3.9.0-ultramodern.6",
|
|
70
|
+
"@modern-js/plugin-bff": "npm:@bleedingdev/modern-js-plugin-bff@3.9.0-ultramodern.6",
|
|
71
|
+
"@modern-js/plugin-bff-extensions": "npm:@bleedingdev/modern-js-plugin-bff-extensions@3.9.0-ultramodern.6",
|
|
72
|
+
"@modern-js/runtime-extensions": "npm:@bleedingdev/modern-js-runtime-extensions@3.9.0-ultramodern.6",
|
|
73
|
+
"@modern-js/server-runtime-extensions": "npm:@bleedingdev/modern-js-server-runtime-extensions@3.9.0-ultramodern.6",
|
|
74
|
+
"@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.9.0-ultramodern.6",
|
|
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.6",
|
|
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.6",
|
|
93
|
+
"@modern-js/bff-effect": "npm:@bleedingdev/modern-js-bff-effect@3.9.0-ultramodern.6",
|
|
94
|
+
"@modern-js/builder": "npm:@bleedingdev/modern-js-builder@3.9.0-ultramodern.6",
|
|
95
|
+
"@modern-js/server-core": "npm:@bleedingdev/modern-js-server-core@3.9.0-ultramodern.6",
|
|
96
|
+
"@modern-js/tsconfig": "npm:@bleedingdev/modern-js-tsconfig@3.9.0-ultramodern.6",
|
|
97
|
+
"@modern-js/types": "npm:@bleedingdev/modern-js-types@3.9.0-ultramodern.6",
|
|
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,87 @@
|
|
|
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 {
|
|
9
|
+
buildOperationContractMap,
|
|
10
|
+
deriveOperationVersion,
|
|
11
|
+
type OperationContractMap,
|
|
12
|
+
} from '@modern-js/server-runtime-extensions/bff-policy/node';
|
|
13
|
+
import { fs, upath as path } from '@modern-js/utils';
|
|
14
|
+
|
|
15
|
+
export const BFF_REQUEST_RUNTIME =
|
|
16
|
+
'@modern-js/runtime-extensions/request-policy';
|
|
17
|
+
|
|
18
|
+
export interface BffGenerationMetadata {
|
|
19
|
+
runtimeFramework: 'effect' | 'hono';
|
|
20
|
+
relativeEffectEntry: string;
|
|
21
|
+
operationContracts: OperationContractMap;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function registerBffClientArtifacts(
|
|
25
|
+
api: CLIPluginAPI<AppTools>,
|
|
26
|
+
metadata: WeakMap<BffGeneration, BffGenerationMetadata>,
|
|
27
|
+
) {
|
|
28
|
+
api.modifyBffClientArtifacts(async (context: BffClientArtifacts) => {
|
|
29
|
+
const { generation } = context;
|
|
30
|
+
const config = api.getNormalizedConfig();
|
|
31
|
+
if (api.getAppContext().bffRuntimeFramework === 'effect') {
|
|
32
|
+
const { resolveEffectOperationContracts, resolveEffectEntryPaths } =
|
|
33
|
+
await import('@modern-js/plugin-bff-extensions/effect-source-loader');
|
|
34
|
+
const { sourceEffectEntry, relativeEffectEntry } =
|
|
35
|
+
resolveEffectEntryPaths({
|
|
36
|
+
appDir: generation.appDirectory,
|
|
37
|
+
apiDir: generation.apiDirectory,
|
|
38
|
+
effectEntry: config.bff?.effect?.entry,
|
|
39
|
+
});
|
|
40
|
+
if (!sourceEffectEntry)
|
|
41
|
+
throw new Error(
|
|
42
|
+
`Cannot resolve Effect BFF entry in ${generation.apiDirectory}.`,
|
|
43
|
+
);
|
|
44
|
+
const operationContracts = await resolveEffectOperationContracts({
|
|
45
|
+
appDir: generation.appDirectory,
|
|
46
|
+
resourcePath: sourceEffectEntry,
|
|
47
|
+
prefix: generation.prefix,
|
|
48
|
+
requestId: generation.requestId,
|
|
49
|
+
});
|
|
50
|
+
if (operationContracts === null) {
|
|
51
|
+
throw new Error(
|
|
52
|
+
`Cannot resolve exported Effect HttpApi in ${sourceEffectEntry}.`,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
metadata.set(generation, {
|
|
56
|
+
runtimeFramework: 'effect',
|
|
57
|
+
relativeEffectEntry,
|
|
58
|
+
operationContracts,
|
|
59
|
+
});
|
|
60
|
+
} else {
|
|
61
|
+
const router = new ApiRouter({
|
|
62
|
+
appDir: generation.appDirectory,
|
|
63
|
+
apiDir: generation.apiDirectory,
|
|
64
|
+
lambdaDir: generation.lambdaDirectory,
|
|
65
|
+
prefix: generation.prefix,
|
|
66
|
+
httpMethodDecider: generation.httpMethodDecider,
|
|
67
|
+
isBuild: true,
|
|
68
|
+
});
|
|
69
|
+
const packageJson = await fs.readJSON(
|
|
70
|
+
path.join(generation.appDirectory, 'package.json'),
|
|
71
|
+
);
|
|
72
|
+
const operationContracts = generation.existLambda
|
|
73
|
+
? buildOperationContractMap({
|
|
74
|
+
handlers: await router.getApiHandlers(),
|
|
75
|
+
requestId: generation.requestId,
|
|
76
|
+
operationVersion: deriveOperationVersion(packageJson.version),
|
|
77
|
+
})
|
|
78
|
+
: {};
|
|
79
|
+
metadata.set(generation, {
|
|
80
|
+
runtimeFramework: 'hono',
|
|
81
|
+
relativeEffectEntry: '',
|
|
82
|
+
operationContracts,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return context;
|
|
86
|
+
});
|
|
87
|
+
}
|
package/src/compile.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
// Hosted APIs use the producer's compiled entry; it is not emitted by this app.
|
|
27
|
+
if (api.getNormalizedConfig().bff?.isCrossProjectServer === true) return;
|
|
28
|
+
const { bundleBuiltEffectEntryForNode } = await import(
|
|
29
|
+
'@modern-js/plugin-bff-extensions/effect-source-loader'
|
|
30
|
+
);
|
|
31
|
+
await bundleBuiltEffectEntryForNode({
|
|
32
|
+
appDir: context.appDirectory,
|
|
33
|
+
apiDir: context.apiDirectory,
|
|
34
|
+
distDir: context.distDirectory,
|
|
35
|
+
effectEntry: api.getNormalizedConfig().bff?.effect?.entry,
|
|
36
|
+
format: context.moduleType === 'module' ? 'esm' : 'cjs',
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
}
|