@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,72 @@
|
|
|
1
|
+
import '@modern-js/server-runtime-extensions/server-config';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import {
|
|
4
|
+
type CrossProjectApiPluginOptions,
|
|
5
|
+
createCrossProjectApiPlugin,
|
|
6
|
+
} from '@modern-js/plugin-bff/cross-project';
|
|
7
|
+
import type { OperationContractMap } from '@modern-js/server-runtime-extensions/bff-policy/node';
|
|
8
|
+
|
|
9
|
+
export interface CrossProjectBffDescriptor {
|
|
10
|
+
native: Omit<CrossProjectApiPluginOptions, 'modifyResolvedConfig'>;
|
|
11
|
+
runtimeFramework: 'hono' | 'effect';
|
|
12
|
+
relativeEffectEntry: string;
|
|
13
|
+
requestId: string;
|
|
14
|
+
operationContracts: OperationContractMap;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function createCrossProjectBffPlugin(
|
|
18
|
+
descriptor: CrossProjectBffDescriptor,
|
|
19
|
+
) {
|
|
20
|
+
return createCrossProjectApiPlugin({
|
|
21
|
+
...descriptor.native,
|
|
22
|
+
modifyResolvedConfig: (resolvedConfig, { sdkDistDirectory, api }) => {
|
|
23
|
+
const config = api.getConfig();
|
|
24
|
+
const configuredRuntimeFramework = config?.bff?.runtimeFramework;
|
|
25
|
+
if (
|
|
26
|
+
configuredRuntimeFramework &&
|
|
27
|
+
configuredRuntimeFramework !== descriptor.runtimeFramework
|
|
28
|
+
) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
`[${descriptor.native.packageName}] Runtime framework mismatch for cross-project BFF. Detected "${configuredRuntimeFramework}", but producer SDK requires "${descriptor.runtimeFramework}".`,
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
const effectEntry = descriptor.relativeEffectEntry
|
|
34
|
+
? path.join(sdkDistDirectory, descriptor.relativeEffectEntry)
|
|
35
|
+
: undefined;
|
|
36
|
+
api.updateAppContext({
|
|
37
|
+
...(effectEntry ? { apiDirectory: path.dirname(effectEntry) } : {}),
|
|
38
|
+
bffRuntimeFramework: descriptor.runtimeFramework,
|
|
39
|
+
});
|
|
40
|
+
resolvedConfig.bff ??= {};
|
|
41
|
+
resolvedConfig.bff.runtimeFramework = descriptor.runtimeFramework;
|
|
42
|
+
if (effectEntry)
|
|
43
|
+
resolvedConfig.bff.effect = {
|
|
44
|
+
...resolvedConfig.bff.effect,
|
|
45
|
+
entry: effectEntry,
|
|
46
|
+
};
|
|
47
|
+
resolvedConfig.bff.requestId =
|
|
48
|
+
resolvedConfig.bff.requestId ||
|
|
49
|
+
config?.bff?.requestId ||
|
|
50
|
+
descriptor.requestId ||
|
|
51
|
+
descriptor.native.packageName ||
|
|
52
|
+
'default';
|
|
53
|
+
const policy = resolvedConfig.bff.crossProjectPolicy;
|
|
54
|
+
resolvedConfig.bff.crossProjectPolicy = {
|
|
55
|
+
...policy,
|
|
56
|
+
enabled: policy?.enabled ?? true,
|
|
57
|
+
requireEnvelope: policy?.requireEnvelope ?? true,
|
|
58
|
+
requireOperationContext: policy?.requireOperationContext ?? true,
|
|
59
|
+
requireOperationContextDetails:
|
|
60
|
+
policy?.requireOperationContextDetails ?? true,
|
|
61
|
+
requireOperationSchemaHash: policy?.requireOperationSchemaHash ?? true,
|
|
62
|
+
requireOperationVersion: policy?.requireOperationVersion ?? true,
|
|
63
|
+
allowUnknownOperations: policy?.allowUnknownOperations ?? false,
|
|
64
|
+
expectedOperationContracts: {
|
|
65
|
+
...policy?.expectedOperationContracts,
|
|
66
|
+
...descriptor.operationContracts,
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
return resolvedConfig;
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { pathToFileURL } from 'node:url';
|
|
2
|
+
import type {
|
|
3
|
+
AppTools,
|
|
4
|
+
BffGeneratedEntries,
|
|
5
|
+
BffGeneration,
|
|
6
|
+
} from '@modern-js/app-tools';
|
|
7
|
+
import type { CLIPluginAPI } from '@modern-js/plugin';
|
|
8
|
+
import { fs, upath as path } from '@modern-js/utils';
|
|
9
|
+
import {
|
|
10
|
+
BFF_REQUEST_RUNTIME,
|
|
11
|
+
type BffGenerationMetadata,
|
|
12
|
+
} from './client-artifacts';
|
|
13
|
+
import type { CrossProjectBffDescriptor } from './cross-project';
|
|
14
|
+
|
|
15
|
+
export function registerBffGeneratedEntries(
|
|
16
|
+
api: CLIPluginAPI<AppTools>,
|
|
17
|
+
metadata: WeakMap<BffGeneration, BffGenerationMetadata>,
|
|
18
|
+
) {
|
|
19
|
+
api.modifyBffGeneratedEntries(async (entries: BffGeneratedEntries) => {
|
|
20
|
+
const { generation } = entries;
|
|
21
|
+
const details = metadata.get(generation);
|
|
22
|
+
if (!details)
|
|
23
|
+
throw new Error(
|
|
24
|
+
'BFF entries require completed client artifact generation.',
|
|
25
|
+
);
|
|
26
|
+
metadata.delete(generation);
|
|
27
|
+
const packageJson = await fs.readJSON(
|
|
28
|
+
path.join(generation.appDirectory, 'package.json'),
|
|
29
|
+
);
|
|
30
|
+
const runtimeSpecifier =
|
|
31
|
+
api.getNormalizedConfig().bff?.runtimeCreateRequest ||
|
|
32
|
+
BFF_REQUEST_RUNTIME;
|
|
33
|
+
const dependencies = [
|
|
34
|
+
'@modern-js/plugin-bff-build-extensions',
|
|
35
|
+
'@modern-js/plugin-bff-extensions',
|
|
36
|
+
];
|
|
37
|
+
const emitsClient =
|
|
38
|
+
details.runtimeFramework === 'effect' || generation.apiFiles.length > 0;
|
|
39
|
+
if (
|
|
40
|
+
runtimeSpecifier === BFF_REQUEST_RUNTIME ||
|
|
41
|
+
(emitsClient &&
|
|
42
|
+
(generation.requestCreator || BFF_REQUEST_RUNTIME) ===
|
|
43
|
+
BFF_REQUEST_RUNTIME)
|
|
44
|
+
)
|
|
45
|
+
dependencies.push('@modern-js/runtime-extensions');
|
|
46
|
+
for (const name of dependencies) {
|
|
47
|
+
const specifier =
|
|
48
|
+
packageJson.dependencies?.[name] ||
|
|
49
|
+
packageJson.devDependencies?.[name] ||
|
|
50
|
+
fs.readJSONSync(require.resolve(`${name}/package.json`)).version;
|
|
51
|
+
entries.packageDependencies[name] = specifier;
|
|
52
|
+
}
|
|
53
|
+
const descriptor: CrossProjectBffDescriptor = {
|
|
54
|
+
native: {
|
|
55
|
+
packageName: packageJson.name,
|
|
56
|
+
prefix: generation.prefix,
|
|
57
|
+
relativeDistPath: path.normalize(generation.relativeDistPath),
|
|
58
|
+
relativeApiPath: path.relative(
|
|
59
|
+
generation.appDirectory,
|
|
60
|
+
generation.apiDirectory,
|
|
61
|
+
),
|
|
62
|
+
relativeLambdaPath: path.relative(
|
|
63
|
+
generation.appDirectory,
|
|
64
|
+
generation.lambdaDirectory,
|
|
65
|
+
),
|
|
66
|
+
},
|
|
67
|
+
runtimeFramework: details.runtimeFramework,
|
|
68
|
+
relativeEffectEntry: details.relativeEffectEntry,
|
|
69
|
+
requestId: generation.requestId,
|
|
70
|
+
operationContracts: details.operationContracts,
|
|
71
|
+
};
|
|
72
|
+
const isModule =
|
|
73
|
+
api.getAppContext().moduleType === 'module' ||
|
|
74
|
+
packageJson.type === 'module';
|
|
75
|
+
entries.plugin = {
|
|
76
|
+
code: isModule
|
|
77
|
+
? `import { createCrossProjectBffPlugin } from '@modern-js/plugin-bff-build-extensions/cross-project';\nexport const crossProjectApiPlugin = () => createCrossProjectBffPlugin(${JSON.stringify(descriptor)});\n`
|
|
78
|
+
: `'use strict';\nconst { createCrossProjectBffPlugin } = require('@modern-js/plugin-bff-build-extensions/cross-project');\nexports.crossProjectApiPlugin = () => createCrossProjectBffPlugin(${JSON.stringify(descriptor)});\n`,
|
|
79
|
+
declaration: `import type { AppTools, CliPlugin } from '@modern-js/app-tools';
|
|
80
|
+
export declare const crossProjectApiPlugin: () => CliPlugin<AppTools>;
|
|
81
|
+
`,
|
|
82
|
+
};
|
|
83
|
+
const runtime = JSON.stringify(runtimeSpecifier);
|
|
84
|
+
const esmRuntime = JSON.stringify(
|
|
85
|
+
path.isAbsolute(runtimeSpecifier)
|
|
86
|
+
? pathToFileURL(runtimeSpecifier).href
|
|
87
|
+
: runtimeSpecifier,
|
|
88
|
+
);
|
|
89
|
+
entries.runtime = {
|
|
90
|
+
code: isModule
|
|
91
|
+
? `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`
|
|
92
|
+
: `'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`,
|
|
93
|
+
declaration: `type Runtime = typeof import(${runtime});
|
|
94
|
+
export declare const initProducerClient: (options?: Parameters<Runtime['configure']>[0]) => ReturnType<Runtime['configure']>;
|
|
95
|
+
export declare const configure: typeof initProducerClient;
|
|
96
|
+
`,
|
|
97
|
+
};
|
|
98
|
+
return entries;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import type { ClientCodegenPlugin } from '@modern-js/bff-core';
|
|
3
|
+
import {
|
|
4
|
+
buildOperationContractMap,
|
|
5
|
+
createOperationSchemaHash,
|
|
6
|
+
deriveOperationVersion,
|
|
7
|
+
} from '@modern-js/server-runtime-extensions/bff-policy/node';
|
|
8
|
+
|
|
9
|
+
const REQUEST_RUNTIME = '@modern-js/runtime-extensions/request-policy';
|
|
10
|
+
|
|
11
|
+
export const modifyClient: ClientCodegenPlugin = (draft, context) => {
|
|
12
|
+
const { appDir, requestId } = context.options;
|
|
13
|
+
draft.requestCreator = context.options.requestCreator || REQUEST_RUNTIME;
|
|
14
|
+
let version: string | undefined;
|
|
15
|
+
try {
|
|
16
|
+
version = require(path.resolve(appDir, 'package.json')).version;
|
|
17
|
+
} catch {
|
|
18
|
+
// Missing or invalid package metadata uses the established version fallback.
|
|
19
|
+
}
|
|
20
|
+
const operationVersion = deriveOperationVersion(version);
|
|
21
|
+
const normalizedRequestId = requestId || 'default';
|
|
22
|
+
const operationContracts = buildOperationContractMap({
|
|
23
|
+
handlers: [...context.handlerInfos],
|
|
24
|
+
requestId: normalizedRequestId,
|
|
25
|
+
operationVersion,
|
|
26
|
+
});
|
|
27
|
+
const operationEntries = context.handlerInfos
|
|
28
|
+
.map(handlerInfo => {
|
|
29
|
+
const httpMethod = handlerInfo.httpMethod.toUpperCase();
|
|
30
|
+
return {
|
|
31
|
+
name: handlerInfo.name,
|
|
32
|
+
httpMethod,
|
|
33
|
+
routePath: handlerInfo.routePath,
|
|
34
|
+
schemaHash:
|
|
35
|
+
operationContracts[`${httpMethod}:${handlerInfo.routePath}`]
|
|
36
|
+
?.schemaHash ?? '',
|
|
37
|
+
};
|
|
38
|
+
})
|
|
39
|
+
.sort((a, b) =>
|
|
40
|
+
`${a.routePath}:${a.httpMethod}:${a.name}`.localeCompare(
|
|
41
|
+
`${b.routePath}:${b.httpMethod}:${b.name}`,
|
|
42
|
+
),
|
|
43
|
+
);
|
|
44
|
+
const schemaHash = createOperationSchemaHash(
|
|
45
|
+
operationEntries,
|
|
46
|
+
normalizedRequestId,
|
|
47
|
+
);
|
|
48
|
+
for (const { handlerInfo, optionProperties } of draft.handlers) {
|
|
49
|
+
if (handlerInfo.action === 'upload' && !requestId) continue;
|
|
50
|
+
const method = handlerInfo.httpMethod.toUpperCase();
|
|
51
|
+
const operationContext = {
|
|
52
|
+
operationId: handlerInfo.name,
|
|
53
|
+
routePath: handlerInfo.routePath,
|
|
54
|
+
method,
|
|
55
|
+
schemaHash:
|
|
56
|
+
operationContracts[`${method}:${handlerInfo.routePath}`]?.schemaHash ??
|
|
57
|
+
'',
|
|
58
|
+
operationVersion,
|
|
59
|
+
};
|
|
60
|
+
optionProperties.push(
|
|
61
|
+
`operationContext: ${JSON.stringify(operationContext)}`,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
if (requestId) {
|
|
65
|
+
draft.imports.push(
|
|
66
|
+
`import * as requestRuntime from ${JSON.stringify(draft.requestCreator)};`,
|
|
67
|
+
'import { createProducerClient } from "@modern-js/plugin-bff-extensions/producer-runtime";',
|
|
68
|
+
);
|
|
69
|
+
draft.statements.push(`export const initProducerClient = (options = {}) => {
|
|
70
|
+
const configure = requestRuntime.configure;
|
|
71
|
+
if (typeof configure !== 'function') {
|
|
72
|
+
console.warn('[modernjs] Compatibility request creator path does not expose configure(); use default @modern-js/create-request or migrate the compatibility path.');
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
return createProducerClient(configure, { requestId: ${JSON.stringify(requestId)} })(options);
|
|
76
|
+
};`);
|
|
77
|
+
}
|
|
78
|
+
draft.statements.push(
|
|
79
|
+
`export const operationVersion = ${String(operationVersion)};`,
|
|
80
|
+
`export const operationSchemaHash = ${JSON.stringify(schemaHash)};`,
|
|
81
|
+
`export const operationManifest = ${JSON.stringify({ operationVersion, schemaHash, operations: operationEntries }, null, 2)};`,
|
|
82
|
+
);
|
|
83
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import '@modern-js/server-runtime-extensions/server-config';
|
|
2
|
+
import type { AppTools, BffGeneration, CliPlugin } from '@modern-js/app-tools';
|
|
3
|
+
import { bffPlugin as nativeBffPlugin } from '@modern-js/plugin-bff';
|
|
4
|
+
import { fs, upath as path } from '@modern-js/utils';
|
|
5
|
+
import {
|
|
6
|
+
BFF_REQUEST_RUNTIME,
|
|
7
|
+
type BffGenerationMetadata,
|
|
8
|
+
registerBffClientArtifacts,
|
|
9
|
+
} from './client-artifacts';
|
|
10
|
+
import { registerBffCompilation } from './compile';
|
|
11
|
+
import { registerBffGeneratedEntries } from './generated-entries';
|
|
12
|
+
|
|
13
|
+
export const bffPlugin = (): CliPlugin<AppTools> => ({
|
|
14
|
+
name: '@modern-js/plugin-bff-build-extensions',
|
|
15
|
+
usePlugins: [nativeBffPlugin()],
|
|
16
|
+
pre: ['@modern-js/plugin-bff'],
|
|
17
|
+
setup: api => {
|
|
18
|
+
const hooks = api.getHooks();
|
|
19
|
+
for (const name of [
|
|
20
|
+
'onBeforeBffCompile',
|
|
21
|
+
'onAfterBffCompile',
|
|
22
|
+
'modifyBffClientArtifacts',
|
|
23
|
+
'modifyBffGeneratedEntries',
|
|
24
|
+
] as const) {
|
|
25
|
+
if (typeof hooks[name]?.tap !== 'function')
|
|
26
|
+
throw new Error(`Native BFF build hook ${name} is unavailable.`);
|
|
27
|
+
}
|
|
28
|
+
api.config(() => ({
|
|
29
|
+
bff: {
|
|
30
|
+
requestCreator: BFF_REQUEST_RUNTIME,
|
|
31
|
+
runtimeCreateRequest: BFF_REQUEST_RUNTIME,
|
|
32
|
+
clientCodegenPlugin: require.resolve(
|
|
33
|
+
'@modern-js/plugin-bff-build-extensions/hono-client-codegen',
|
|
34
|
+
),
|
|
35
|
+
},
|
|
36
|
+
}));
|
|
37
|
+
const runtimeFramework = api.getConfig()?.bff?.runtimeFramework;
|
|
38
|
+
if (
|
|
39
|
+
runtimeFramework !== undefined &&
|
|
40
|
+
runtimeFramework !== 'hono' &&
|
|
41
|
+
runtimeFramework !== 'effect'
|
|
42
|
+
) {
|
|
43
|
+
throw new Error(`Unsupported Effect BFF runtime "${runtimeFramework}".`);
|
|
44
|
+
}
|
|
45
|
+
api.updateAppContext({ bffRuntimeFramework: runtimeFramework ?? 'effect' });
|
|
46
|
+
api._internalServerPlugins(({ plugins }) => {
|
|
47
|
+
const nativePlugins = plugins.filter(
|
|
48
|
+
plugin => plugin.name === '@modern-js/plugin-bff/server-plugin',
|
|
49
|
+
);
|
|
50
|
+
if (nativePlugins.length !== 1) {
|
|
51
|
+
throw new Error('Expected exactly one native BFF server plugin.');
|
|
52
|
+
}
|
|
53
|
+
const native = nativePlugins[0]!;
|
|
54
|
+
const runtimeAdapters = native.options?.runtimeAdapters ?? {};
|
|
55
|
+
const effect = '@modern-js/plugin-bff-extensions/effect-adapter';
|
|
56
|
+
const honoRouteBinder = '@modern-js/plugin-bff-extensions/hono/node';
|
|
57
|
+
if (
|
|
58
|
+
native.options?.honoRouteBinder !== undefined &&
|
|
59
|
+
native.options.honoRouteBinder !== honoRouteBinder
|
|
60
|
+
) {
|
|
61
|
+
throw new Error('The Hono BFF route binder is already configured.');
|
|
62
|
+
}
|
|
63
|
+
if (
|
|
64
|
+
runtimeAdapters.effect !== undefined &&
|
|
65
|
+
runtimeAdapters.effect !== effect
|
|
66
|
+
) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
'The Effect BFF runtime adapter is already configured.',
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
native.options = {
|
|
72
|
+
...native.options,
|
|
73
|
+
honoRouteBinder,
|
|
74
|
+
runtimeAdapters: { ...runtimeAdapters, effect },
|
|
75
|
+
};
|
|
76
|
+
return { plugins };
|
|
77
|
+
});
|
|
78
|
+
const metadata = new WeakMap<BffGeneration, BffGenerationMetadata>();
|
|
79
|
+
registerBffCompilation(api);
|
|
80
|
+
registerBffClientArtifacts(api, metadata);
|
|
81
|
+
registerBffGeneratedEntries(api, metadata);
|
|
82
|
+
api.modifyBundlerChain(async (chain, { isServer, CHAIN_ID }) => {
|
|
83
|
+
if (api.getAppContext().bffRuntimeFramework !== 'effect') return;
|
|
84
|
+
const { appDirectory, apiDirectory, port } = api.getAppContext();
|
|
85
|
+
const bff = api.getNormalizedConfig().bff;
|
|
86
|
+
const { resolveEffectEntryFile } = await import(
|
|
87
|
+
'@modern-js/plugin-bff-extensions/client-generator'
|
|
88
|
+
);
|
|
89
|
+
const entry = resolveEffectEntryFile({
|
|
90
|
+
appDir: appDirectory,
|
|
91
|
+
apiDir: apiDirectory,
|
|
92
|
+
effectEntry: bff?.effect?.entry,
|
|
93
|
+
});
|
|
94
|
+
if (!entry)
|
|
95
|
+
throw new Error(`Cannot resolve Effect BFF entry in ${apiDirectory}.`);
|
|
96
|
+
const canonical = (file: string) =>
|
|
97
|
+
path.normalize(
|
|
98
|
+
fs.existsSync(file) ? fs.realpathSync(file) : path.resolve(file),
|
|
99
|
+
);
|
|
100
|
+
const entryPath = canonical(entry);
|
|
101
|
+
const isEntry = (resource: string) => canonical(resource) === entryPath;
|
|
102
|
+
chain.module.rule(CHAIN_ID.RULE.JS).exclude.add(isEntry);
|
|
103
|
+
chain.module.rule('js-bff-api').exclude.add(isEntry);
|
|
104
|
+
chain.module
|
|
105
|
+
.rule('js-bff-effect-entry')
|
|
106
|
+
.test(isEntry)
|
|
107
|
+
.use('effect-bff-loader')
|
|
108
|
+
.loader(
|
|
109
|
+
require.resolve(
|
|
110
|
+
'@modern-js/plugin-bff-extensions/effect-source-loader/rspack-loader',
|
|
111
|
+
),
|
|
112
|
+
)
|
|
113
|
+
.options({
|
|
114
|
+
appDir: appDirectory,
|
|
115
|
+
apiDir: apiDirectory,
|
|
116
|
+
effectEntry: entry,
|
|
117
|
+
prefix: Array.isArray(bff?.prefix)
|
|
118
|
+
? bff.prefix[0]
|
|
119
|
+
: bff?.prefix || '/api',
|
|
120
|
+
port,
|
|
121
|
+
target: isServer ? 'server' : 'client',
|
|
122
|
+
requestId: bff?.requestId,
|
|
123
|
+
requestCreator: bff?.requestCreator || BFF_REQUEST_RUNTIME,
|
|
124
|
+
httpMethodDecider: bff?.httpMethodDecider,
|
|
125
|
+
effectDataPlatformBatch: bff?.effect?.dataPlatform?.batch,
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
export default bffPlugin;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { AppTools } from '@modern-js/app-tools';
|
|
2
|
+
import type { RsbuildTarget } from '@modern-js/builder';
|
|
3
|
+
import { applyOptionsChain, fs, upath as path } from '@modern-js/utils';
|
|
4
|
+
import { transform } from '@swc/core';
|
|
5
|
+
|
|
6
|
+
type SerializedGlobalVars = Record<string, string>;
|
|
7
|
+
type ServerGlobalVarsOptions = NonNullable<
|
|
8
|
+
NonNullable<AppTools['config']>['source']
|
|
9
|
+
>['globalVars'];
|
|
10
|
+
|
|
11
|
+
const RELEASE_IDENTITY_GLOBAL_NAMES = [
|
|
12
|
+
'ULTRAMODERN_BUILD_MARKER',
|
|
13
|
+
'ULTRAMODERN_SOURCE_REVISION',
|
|
14
|
+
'ULTRAMODERN_RELEASE_VERSION',
|
|
15
|
+
] as const;
|
|
16
|
+
|
|
17
|
+
const collectJavaScriptFiles = (directory: string): Promise<string[]> =>
|
|
18
|
+
Promise.resolve()
|
|
19
|
+
.then(() => fs.readdir(directory, { withFileTypes: true }))
|
|
20
|
+
.then(entries =>
|
|
21
|
+
Promise.all(
|
|
22
|
+
entries.map(entry => {
|
|
23
|
+
const absolutePath = path.join(directory, entry.name);
|
|
24
|
+
if (entry.isDirectory()) {
|
|
25
|
+
return collectJavaScriptFiles(absolutePath);
|
|
26
|
+
}
|
|
27
|
+
return /\.(?:c|m)?js$/u.test(entry.name) ? [absolutePath] : [];
|
|
28
|
+
}),
|
|
29
|
+
).then(nested => nested.flat()),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
export const serializeServerGlobalVars = (
|
|
33
|
+
options: ServerGlobalVarsOptions,
|
|
34
|
+
): SerializedGlobalVars => {
|
|
35
|
+
const globalVars = applyOptionsChain<
|
|
36
|
+
Record<string, unknown>,
|
|
37
|
+
{ env: string; target: RsbuildTarget }
|
|
38
|
+
>({}, options, {
|
|
39
|
+
env: 'server',
|
|
40
|
+
target: 'node',
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return Object.fromEntries(
|
|
44
|
+
Object.entries(globalVars).map(([key, value]) => {
|
|
45
|
+
let serialized: string | undefined;
|
|
46
|
+
try {
|
|
47
|
+
serialized = JSON.stringify(value);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`source.globalVars["${key}"] cannot be serialized exactly for BFF compilation.`,
|
|
51
|
+
{ cause: error },
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
if (serialized === undefined) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
`source.globalVars["${key}"] cannot be serialized exactly for BFF compilation.`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
return [key, serialized];
|
|
60
|
+
}),
|
|
61
|
+
);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export const transformServerGlobalVars = (
|
|
65
|
+
outputDirectories: string[],
|
|
66
|
+
globalVars: SerializedGlobalVars,
|
|
67
|
+
) =>
|
|
68
|
+
Promise.resolve().then(() => {
|
|
69
|
+
if (Object.keys(globalVars).length === 0) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return Promise.all(
|
|
74
|
+
outputDirectories.map(directory =>
|
|
75
|
+
fs
|
|
76
|
+
.pathExists(directory)
|
|
77
|
+
.then(directoryExists => (directoryExists ? directory : undefined)),
|
|
78
|
+
),
|
|
79
|
+
)
|
|
80
|
+
.then(existingDirectories => {
|
|
81
|
+
const filesPromise = Promise.all(
|
|
82
|
+
existingDirectories
|
|
83
|
+
.filter((directory): directory is string => directory !== undefined)
|
|
84
|
+
.map(collectJavaScriptFiles),
|
|
85
|
+
).then(files => files.flat());
|
|
86
|
+
|
|
87
|
+
const releaseIdentityBanner = RELEASE_IDENTITY_GLOBAL_NAMES.every(
|
|
88
|
+
name => Object.hasOwn(globalVars, name),
|
|
89
|
+
)
|
|
90
|
+
? `${RELEASE_IDENTITY_GLOBAL_NAMES.map(name => `void ${globalVars[name]};`).join('')}\n`
|
|
91
|
+
: '';
|
|
92
|
+
|
|
93
|
+
return filesPromise.then(files =>
|
|
94
|
+
Promise.all(
|
|
95
|
+
files.map(filename =>
|
|
96
|
+
fs.readFile(filename, 'utf8').then(source => {
|
|
97
|
+
const sourceMapFilename = `${filename}.map`;
|
|
98
|
+
return fs
|
|
99
|
+
.pathExists(sourceMapFilename)
|
|
100
|
+
.then(sourceMapExists => {
|
|
101
|
+
const inputSourceMapPromise: Promise<string | undefined> =
|
|
102
|
+
sourceMapExists
|
|
103
|
+
? fs.readFile(sourceMapFilename, 'utf8')
|
|
104
|
+
: Promise.resolve(undefined);
|
|
105
|
+
|
|
106
|
+
return inputSourceMapPromise.then(inputSourceMap =>
|
|
107
|
+
transform(`${releaseIdentityBanner}${source}`, {
|
|
108
|
+
filename,
|
|
109
|
+
inputSourceMap,
|
|
110
|
+
sourceMaps: sourceMapExists,
|
|
111
|
+
jsc: {
|
|
112
|
+
parser: {
|
|
113
|
+
syntax: 'ecmascript',
|
|
114
|
+
},
|
|
115
|
+
preserveAllComments: true,
|
|
116
|
+
target: 'es2024',
|
|
117
|
+
transform: {
|
|
118
|
+
optimizer: {
|
|
119
|
+
globals: {
|
|
120
|
+
vars: globalVars,
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
}).then(result =>
|
|
126
|
+
fs.writeFile(filename, result.code).then(() => {
|
|
127
|
+
if (
|
|
128
|
+
sourceMapExists &&
|
|
129
|
+
result.map !== undefined &&
|
|
130
|
+
result.map.length > 0
|
|
131
|
+
) {
|
|
132
|
+
return fs.writeFile(sourceMapFilename, result.map);
|
|
133
|
+
}
|
|
134
|
+
return undefined;
|
|
135
|
+
}),
|
|
136
|
+
),
|
|
137
|
+
);
|
|
138
|
+
});
|
|
139
|
+
}),
|
|
140
|
+
),
|
|
141
|
+
),
|
|
142
|
+
);
|
|
143
|
+
})
|
|
144
|
+
.then(() => undefined);
|
|
145
|
+
});
|
package/README.md
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
# @bleedingdev/modern-js-plugin-bff-build-extensions
|
|
2
|
-
|
|
3
|
-
This 0.0.0 version only registers the package name so npm trusted publishing can be configured. It contains no runtime implementation. Use the subsequent UltraModern.js release, published from the repository workflow with provenance.
|