@bleedingdev/modern-js-plugin-bff-build-extensions 0.0.0 → 3.9.0-ultramodern.10
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 +130 -0
- package/dist/cjs/self-module.js +55 -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 +90 -0
- package/dist/esm/self-module.mjs +14 -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 +92 -0
- package/dist/esm-node/self-module.mjs +15 -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/self-module.d.ts +12 -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 +127 -0
- package/src/self-module.ts +26 -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,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,92 @@
|
|
|
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
|
+
import { resolveSelfModule } from "./self-module.mjs";
|
|
10
|
+
const src_bffPlugin = ()=>({
|
|
11
|
+
name: '@modern-js/plugin-bff-build-extensions',
|
|
12
|
+
usePlugins: [
|
|
13
|
+
bffPlugin()
|
|
14
|
+
],
|
|
15
|
+
pre: [
|
|
16
|
+
'@modern-js/plugin-bff'
|
|
17
|
+
],
|
|
18
|
+
setup: (api)=>{
|
|
19
|
+
const hooks = api.getHooks();
|
|
20
|
+
for (const name of [
|
|
21
|
+
'onBeforeBffCompile',
|
|
22
|
+
'onAfterBffCompile',
|
|
23
|
+
'modifyBffClientArtifacts',
|
|
24
|
+
'modifyBffGeneratedEntries'
|
|
25
|
+
])if ('function' != typeof hooks[name]?.tap) throw new Error(`Native BFF build hook ${name} is unavailable.`);
|
|
26
|
+
api.config(()=>({
|
|
27
|
+
bff: {
|
|
28
|
+
requestCreator: BFF_REQUEST_RUNTIME,
|
|
29
|
+
runtimeCreateRequest: BFF_REQUEST_RUNTIME,
|
|
30
|
+
clientCodegenPlugin: resolveSelfModule('hono-client-codegen')
|
|
31
|
+
}
|
|
32
|
+
}));
|
|
33
|
+
const runtimeFramework = api.getConfig()?.bff?.runtimeFramework;
|
|
34
|
+
if (void 0 !== runtimeFramework && 'hono' !== runtimeFramework && 'effect' !== runtimeFramework) throw new Error(`Unsupported Effect BFF runtime "${runtimeFramework}".`);
|
|
35
|
+
api.updateAppContext({
|
|
36
|
+
bffRuntimeFramework: runtimeFramework ?? 'effect'
|
|
37
|
+
});
|
|
38
|
+
api._internalServerPlugins(({ plugins })=>{
|
|
39
|
+
const nativePlugins = plugins.filter((plugin)=>'@modern-js/plugin-bff/server-plugin' === plugin.name);
|
|
40
|
+
if (1 !== nativePlugins.length) throw new Error('Expected exactly one native BFF server plugin.');
|
|
41
|
+
const native = nativePlugins[0];
|
|
42
|
+
const runtimeAdapters = native.options?.runtimeAdapters ?? {};
|
|
43
|
+
const effect = '@modern-js/plugin-bff-extensions/effect-adapter';
|
|
44
|
+
const honoRouteBinder = '@modern-js/plugin-bff-extensions/hono/node';
|
|
45
|
+
if (native.options?.honoRouteBinder !== void 0 && native.options.honoRouteBinder !== honoRouteBinder) throw new Error('The Hono BFF route binder is already configured.');
|
|
46
|
+
if (void 0 !== runtimeAdapters.effect && runtimeAdapters.effect !== effect) throw new Error('The Effect BFF runtime adapter is already configured.');
|
|
47
|
+
native.options = {
|
|
48
|
+
...native.options,
|
|
49
|
+
honoRouteBinder,
|
|
50
|
+
runtimeAdapters: {
|
|
51
|
+
...runtimeAdapters,
|
|
52
|
+
effect
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
plugins
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
const metadata = new WeakMap();
|
|
60
|
+
registerBffCompilation(api);
|
|
61
|
+
registerBffClientArtifacts(api, metadata);
|
|
62
|
+
registerBffGeneratedEntries(api, metadata);
|
|
63
|
+
api.modifyBundlerChain(async (chain, { CHAIN_ID })=>{
|
|
64
|
+
if ('effect' !== api.getAppContext().bffRuntimeFramework) return;
|
|
65
|
+
const { appDirectory, apiDirectory } = api.getAppContext();
|
|
66
|
+
const bff = api.getNormalizedConfig().bff;
|
|
67
|
+
if (!fs.existsSync(apiDirectory) && bff?.effect?.entry === void 0) return;
|
|
68
|
+
const { resolveEffectEntryFile } = await import("@modern-js/plugin-bff-extensions/effect-source-loader");
|
|
69
|
+
const entry = resolveEffectEntryFile({
|
|
70
|
+
appDir: appDirectory,
|
|
71
|
+
apiDir: apiDirectory,
|
|
72
|
+
effectEntry: bff?.effect?.entry
|
|
73
|
+
});
|
|
74
|
+
if (!entry) throw new Error(`Cannot resolve Effect BFF entry in ${apiDirectory}.`);
|
|
75
|
+
const canonical = (file)=>upath.normalize(fs.existsSync(file) ? fs.realpathSync(file) : upath.resolve(file));
|
|
76
|
+
const entryPath = canonical(entry);
|
|
77
|
+
const isEntry = (resource)=>canonical(resource) === entryPath;
|
|
78
|
+
chain.module.rule(CHAIN_ID.RULE.JS).exclude.add(isEntry);
|
|
79
|
+
chain.module.rule('js-bff-api').exclude.add(isEntry);
|
|
80
|
+
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({
|
|
81
|
+
appDir: appDirectory,
|
|
82
|
+
apiDir: apiDirectory,
|
|
83
|
+
effectEntry: entry,
|
|
84
|
+
prefix: Array.isArray(bff?.prefix) ? bff.prefix[0] : bff?.prefix || '/api',
|
|
85
|
+
requestId: bff?.requestId
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
const src = src_bffPlugin;
|
|
91
|
+
export default src;
|
|
92
|
+
export { src_bffPlugin as bffPlugin };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
const selfRequire = createRequire(import.meta.url);
|
|
4
|
+
const resolveSelfModule = (name)=>{
|
|
5
|
+
for (const extension of [
|
|
6
|
+
'.js',
|
|
7
|
+
'.mjs',
|
|
8
|
+
'.cjs',
|
|
9
|
+
'.ts'
|
|
10
|
+
])try {
|
|
11
|
+
return selfRequire.resolve(`./${name}${extension}`);
|
|
12
|
+
} catch {}
|
|
13
|
+
return selfRequire.resolve(`@modern-js/plugin-bff-build-extensions/${name}`);
|
|
14
|
+
};
|
|
15
|
+
export { resolveSelfModule };
|
|
@@ -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,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve a file this package ships alongside the calling module.
|
|
3
|
+
*
|
|
4
|
+
* Resolving it by this package's own public name (`@modern-js/plugin-bff-build-extensions/<name>`)
|
|
5
|
+
* only works where the package can see itself in a `node_modules` directory —
|
|
6
|
+
* under pnpm's isolated layout it is anchored in its own `.pnpm` directory and
|
|
7
|
+
* the lookup fails for a consumer, surfacing as an opaque `require.resolve`
|
|
8
|
+
* error in the middle of a build. A sibling lookup works in every layout:
|
|
9
|
+
* source (`src/*.ts`), CommonJS (`dist/cjs/*.js`) and ESM (`dist/esm-node/*.mjs`)
|
|
10
|
+
* all place these files next to each other.
|
|
11
|
+
*/
|
|
12
|
+
export declare const resolveSelfModule: (name: string) => string;
|
|
@@ -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.10",
|
|
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.10",
|
|
69
|
+
"@modern-js/plugin": "npm:@bleedingdev/modern-js-plugin@3.9.0-ultramodern.10",
|
|
70
|
+
"@modern-js/plugin-bff": "npm:@bleedingdev/modern-js-plugin-bff@3.9.0-ultramodern.10",
|
|
71
|
+
"@modern-js/plugin-bff-extensions": "npm:@bleedingdev/modern-js-plugin-bff-extensions@3.9.0-ultramodern.10",
|
|
72
|
+
"@modern-js/runtime-extensions": "npm:@bleedingdev/modern-js-runtime-extensions@3.9.0-ultramodern.10",
|
|
73
|
+
"@modern-js/server-runtime-extensions": "npm:@bleedingdev/modern-js-server-runtime-extensions@3.9.0-ultramodern.10",
|
|
74
|
+
"@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.9.0-ultramodern.10",
|
|
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.10",
|
|
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.10",
|
|
93
|
+
"@modern-js/bff-effect": "npm:@bleedingdev/modern-js-bff-effect@3.9.0-ultramodern.10",
|
|
94
|
+
"@modern-js/builder": "npm:@bleedingdev/modern-js-builder@3.9.0-ultramodern.10",
|
|
95
|
+
"@modern-js/server-core": "npm:@bleedingdev/modern-js-server-core@3.9.0-ultramodern.10",
|
|
96
|
+
"@modern-js/tsconfig": "npm:@bleedingdev/modern-js-tsconfig@3.9.0-ultramodern.10",
|
|
97
|
+
"@modern-js/types": "npm:@bleedingdev/modern-js-types@3.9.0-ultramodern.10",
|
|
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
|
+
}
|