@bleedingdev/modern-js-plugin-bff 3.5.0-ultramodern.101 → 3.5.0-ultramodern.103

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.
Files changed (86) hide show
  1. package/dist/cjs/cli/compress.js +1 -1
  2. package/dist/cjs/cli/generator.js +106 -97
  3. package/dist/cjs/cli/serverGlobalVars.js +43 -57
  4. package/dist/cjs/cli.js +6 -2
  5. package/dist/cjs/loader.js +6 -5
  6. package/dist/cjs/runtime/effect/adapter/cross-project-policy.js +17 -12
  7. package/dist/cjs/runtime/effect/adapter/entry.js +13 -5
  8. package/dist/cjs/runtime/effect/adapter/error-response.js +11 -9
  9. package/dist/cjs/runtime/effect/adapter.js +2 -2
  10. package/dist/cjs/runtime/effect/backend-federation/load.js +26 -17
  11. package/dist/cjs/runtime/effect/backend-federation/plugin.js +12 -2
  12. package/dist/cjs/runtime/effect/backend-federation/remotes.js +5 -4
  13. package/dist/cjs/runtime/effect/backend-federation/runtime.js +80 -16
  14. package/dist/cjs/runtime/effect/edge-dispatcher.js +148 -0
  15. package/dist/cjs/runtime/effect/edge.js +19 -100
  16. package/dist/cjs/runtime/effect/handler/definition.js +1 -0
  17. package/dist/cjs/runtime/effect/handler/http.js +17 -6
  18. package/dist/cjs/utils/crossProjectApiPlugin.js +21 -2
  19. package/dist/cjs/utils/effect-client-generator/generator.js +17 -16
  20. package/dist/cjs/utils/effect-client-generator/paths.js +4 -15
  21. package/dist/cjs/utils/effect-client-generator/rendering.js +8 -19
  22. package/dist/cjs/utils/effect-client-generator/runtime.js +15 -32
  23. package/dist/cjs/utils/effectSourceLoader.js +70 -10
  24. package/dist/cjs/utils/pluginGenerator.js +4 -4
  25. package/dist/esm/cli/compress.mjs +1 -1
  26. package/dist/esm/cli/generator.mjs +108 -98
  27. package/dist/esm/cli/serverGlobalVars.mjs +44 -48
  28. package/dist/esm/cli.mjs +2 -0
  29. package/dist/esm/loader.mjs +6 -5
  30. package/dist/esm/runtime/effect/adapter/cross-project-policy.mjs +17 -12
  31. package/dist/esm/runtime/effect/adapter/entry.mjs +13 -5
  32. package/dist/esm/runtime/effect/adapter/error-response.mjs +11 -9
  33. package/dist/esm/runtime/effect/adapter.mjs +3 -3
  34. package/dist/esm/runtime/effect/backend-federation/load.mjs +26 -17
  35. package/dist/esm/runtime/effect/backend-federation/plugin.mjs +12 -2
  36. package/dist/esm/runtime/effect/backend-federation/remotes.mjs +5 -4
  37. package/dist/esm/runtime/effect/backend-federation/runtime.mjs +80 -16
  38. package/dist/esm/runtime/effect/edge-dispatcher.mjs +38 -0
  39. package/dist/esm/runtime/effect/edge.mjs +1 -38
  40. package/dist/esm/runtime/effect/handler/definition.mjs +1 -0
  41. package/dist/esm/runtime/effect/handler/http.mjs +17 -6
  42. package/dist/esm/utils/crossProjectApiPlugin.mjs +16 -3
  43. package/dist/esm/utils/effect-client-generator/generator.mjs +17 -16
  44. package/dist/esm/utils/effect-client-generator/paths.mjs +5 -6
  45. package/dist/esm/utils/effect-client-generator/rendering.mjs +9 -10
  46. package/dist/esm/utils/effect-client-generator/runtime.mjs +16 -23
  47. package/dist/esm/utils/effectSourceLoader.mjs +69 -12
  48. package/dist/esm/utils/pluginGenerator.mjs +5 -5
  49. package/dist/esm-node/cli/compress.mjs +1 -1
  50. package/dist/esm-node/cli/generator.mjs +108 -98
  51. package/dist/esm-node/cli/serverGlobalVars.mjs +44 -48
  52. package/dist/esm-node/cli.mjs +2 -0
  53. package/dist/esm-node/loader.mjs +6 -5
  54. package/dist/esm-node/runtime/effect/adapter/cross-project-policy.mjs +17 -12
  55. package/dist/esm-node/runtime/effect/adapter/entry.mjs +13 -5
  56. package/dist/esm-node/runtime/effect/adapter/error-response.mjs +11 -9
  57. package/dist/esm-node/runtime/effect/adapter.mjs +3 -3
  58. package/dist/esm-node/runtime/effect/backend-federation/load.mjs +26 -17
  59. package/dist/esm-node/runtime/effect/backend-federation/plugin.mjs +12 -2
  60. package/dist/esm-node/runtime/effect/backend-federation/remotes.mjs +5 -4
  61. package/dist/esm-node/runtime/effect/backend-federation/runtime.mjs +80 -16
  62. package/dist/esm-node/runtime/effect/edge-dispatcher.mjs +39 -0
  63. package/dist/esm-node/runtime/effect/edge.mjs +1 -38
  64. package/dist/esm-node/runtime/effect/handler/definition.mjs +1 -0
  65. package/dist/esm-node/runtime/effect/handler/http.mjs +17 -6
  66. package/dist/esm-node/utils/crossProjectApiPlugin.mjs +16 -3
  67. package/dist/esm-node/utils/effect-client-generator/generator.mjs +17 -16
  68. package/dist/esm-node/utils/effect-client-generator/paths.mjs +5 -6
  69. package/dist/esm-node/utils/effect-client-generator/rendering.mjs +9 -10
  70. package/dist/esm-node/utils/effect-client-generator/runtime.mjs +16 -23
  71. package/dist/esm-node/utils/effectSourceLoader.mjs +69 -12
  72. package/dist/esm-node/utils/pluginGenerator.mjs +5 -5
  73. package/dist/types/cli/generator.d.ts +2 -2
  74. package/dist/types/cli/serverGlobalVars.d.ts +4 -2
  75. package/dist/types/cli.d.ts +1 -0
  76. package/dist/types/runtime/effect/adapter/cross-project-policy.d.ts +1 -1
  77. package/dist/types/runtime/effect/backend-federation/runtime.d.ts +1 -1
  78. package/dist/types/runtime/effect/edge-dispatcher.d.ts +26 -0
  79. package/dist/types/runtime/effect/edge.d.ts +1 -26
  80. package/dist/types/runtime/effect/handler/definition.d.ts +2 -12
  81. package/dist/types/runtime/effect/handler/http.d.ts +2 -1
  82. package/dist/types/runtime/effect/handler/types.d.ts +5 -0
  83. package/dist/types/utils/crossProjectApiPlugin.d.ts +2 -0
  84. package/dist/types/utils/effectSourceLoader.d.ts +10 -0
  85. package/dist/types/utils/pluginGenerator.d.ts +3 -1
  86. package/package.json +34 -18
@@ -1,7 +1,6 @@
1
- import { ApiRouter } from "@modern-js/bff-core";
1
+ import { ApiRouter, buildOperationContractMap, deriveOperationVersion } from "@modern-js/bff-core";
2
2
  import { compile } from "@modern-js/server-utils";
3
- import { API_DIR, SHARED_DIR, fs, resolveServerTsconfig } from "@modern-js/utils";
4
- import path from "path";
3
+ import { API_DIR, SHARED_DIR, fs, resolveServerTsconfig, upath } from "@modern-js/utils";
5
4
  import clientGenerator from "../utils/clientGenerator.mjs";
6
5
  import { bundleEffectEntryForNode } from "../utils/effectSourceLoader.mjs";
7
6
  import pluginGenerator from "../utils/pluginGenerator.mjs";
@@ -20,110 +19,121 @@ const effectEntryExtensions = [
20
19
  '.cts'
21
20
  ];
22
21
  function resolveEffectSourceEntry(appDirectory, apiDirectory, configuredEntry) {
23
- const entry = configuredEntry ? path.isAbsolute(configuredEntry) ? configuredEntry : path.resolve(appDirectory, configuredEntry) : path.resolve(apiDirectory, 'index');
24
- return path.extname(entry) ? entry : effectEntryExtensions.map((extension)=>`${entry}${extension}`).find((candidate)=>fs.existsSync(candidate));
22
+ const entry = void 0 !== configuredEntry && configuredEntry.length > 0 ? upath.isAbsolute(configuredEntry) ? configuredEntry : upath.resolve(appDirectory, configuredEntry) : upath.resolve(apiDirectory, 'index');
23
+ return upath.extname(entry).length > 0 ? entry : effectEntryExtensions.map((extension)=>`${entry}${extension}`).find((candidate)=>fs.existsSync(candidate));
25
24
  }
26
25
  function resolveBuiltEffectEntry(appDirectory, distDirectory, sourceEntry) {
27
- if (!sourceEntry) return;
28
- const relativeEntry = path.relative(appDirectory, sourceEntry);
29
- if ('..' === relativeEntry || relativeEntry.startsWith(`..${path.sep}`)) throw new Error(`Effect BFF entry must be inside the application directory: ${sourceEntry}`);
30
- const builtEntry = path.resolve(distDirectory, relativeEntry).replace(/\.(?:[cm]?ts|tsx|jsx)$/u, '.js');
26
+ if (void 0 === sourceEntry || 0 === sourceEntry.length) return;
27
+ const relativeEntry = upath.relative(appDirectory, sourceEntry);
28
+ if ('..' === relativeEntry || relativeEntry.startsWith(`..${upath.sep}`)) throw new Error(`Effect BFF entry must be inside the application directory: ${sourceEntry}`);
29
+ const builtEntry = upath.resolve(distDirectory, relativeEntry).replace(/\.(?:[cm]?ts|tsx|jsx)$/u, '.js');
31
30
  return fs.existsSync(builtEntry) ? builtEntry : void 0;
32
31
  }
33
32
  const createBffGenerator = (api)=>{
34
- const compileApi = async ()=>{
35
- const { appDirectory, distDirectory, apiDirectory, bffRuntimeFramework, sharedDirectory, moduleType } = api.getAppContext();
36
- const modernConfig = api.getNormalizedConfig();
37
- const distDir = path.resolve(distDirectory);
38
- const apiDir = apiDirectory || path.resolve(appDirectory, API_DIR);
39
- const sharedDir = sharedDirectory || path.resolve(appDirectory, SHARED_DIR);
40
- const tsconfigPath = resolveServerTsconfig(appDirectory, modernConfig?.server?.tsconfigPath);
41
- const sourceDirs = [];
42
- if (await fs.pathExists(apiDir)) sourceDirs.push(apiDir);
43
- if (await fs.pathExists(sharedDir)) sourceDirs.push(sharedDir);
44
- const { alias, globalVars } = modernConfig.source;
45
- const { alias: resolveAlias } = modernConfig.resolve;
46
- if (sourceDirs.length > 0) {
47
- const combinedAlias = [].concat(alias ?? []).concat(resolveAlias ?? []);
48
- const serializedGlobalVars = serializeServerGlobalVars(globalVars);
49
- await compile(appDirectory, {
50
- alias: combinedAlias
51
- }, {
52
- sourceDirs,
53
- distDir,
54
- tsconfigPath,
55
- moduleType,
56
- throwErrorInsteadOfExit: true
57
- });
58
- await transformServerGlobalVars(sourceDirs.map((sourceDir)=>path.resolve(distDir, path.relative(appDirectory, sourceDir))), serializedGlobalVars);
59
- if ('effect' === bffRuntimeFramework) {
60
- const sourceEntry = resolveEffectSourceEntry(appDirectory, apiDir, modernConfig.bff?.effect?.entry);
61
- const builtEntry = resolveBuiltEffectEntry(appDirectory, distDir, sourceEntry);
62
- if (!builtEntry) throw new Error(`Effect BFF entry was not emitted into ${distDir}: ${sourceEntry ?? path.resolve(apiDir, 'index')}`);
63
- await bundleEffectEntryForNode({
64
- appDir: appDirectory,
65
- entryPath: builtEntry,
66
- format: 'module' === moduleType ? 'esm' : 'cjs'
33
+ const compileApi = ()=>Promise.resolve().then(()=>{
34
+ const { appDirectory, distDirectory, apiDirectory, bffRuntimeFramework, sharedDirectory, moduleType } = api.getAppContext();
35
+ const modernConfig = api.getNormalizedConfig();
36
+ const distDir = upath.resolve(distDirectory);
37
+ const apiDir = apiDirectory || upath.resolve(appDirectory, API_DIR);
38
+ const sharedDir = sharedDirectory || upath.resolve(appDirectory, SHARED_DIR);
39
+ const tsconfigPath = resolveServerTsconfig(appDirectory, modernConfig?.server?.tsconfigPath);
40
+ const sourceDirs = [];
41
+ return fs.pathExists(apiDir).then((apiDirectoryExists)=>{
42
+ if (apiDirectoryExists) sourceDirs.push(apiDir);
43
+ return fs.pathExists(sharedDir);
44
+ }).then((sharedDirectoryExists)=>{
45
+ if (sharedDirectoryExists) sourceDirs.push(sharedDir);
46
+ if (0 === sourceDirs.length) return;
47
+ const { alias, globalVars } = modernConfig.source;
48
+ const { alias: resolveAlias } = modernConfig.resolve;
49
+ const combinedAlias = [].concat(alias ?? []).concat(resolveAlias ?? []);
50
+ const serializedGlobalVars = serializeServerGlobalVars(globalVars);
51
+ return compile(appDirectory, {
52
+ alias: combinedAlias
53
+ }, {
54
+ sourceDirs,
55
+ distDir,
56
+ tsconfigPath,
57
+ moduleType,
58
+ throwErrorInsteadOfExit: true
59
+ }).then(()=>transformServerGlobalVars(sourceDirs.map((sourceDir)=>upath.resolve(distDir, upath.relative(appDirectory, sourceDir))), serializedGlobalVars)).then(()=>{
60
+ if ('effect' !== bffRuntimeFramework) return;
61
+ const sourceEntry = resolveEffectSourceEntry(appDirectory, apiDir, modernConfig.bff?.effect?.entry);
62
+ const builtEntry = resolveBuiltEffectEntry(appDirectory, distDir, sourceEntry);
63
+ if (void 0 === builtEntry || 0 === builtEntry.length) throw new Error(`Effect BFF entry was not emitted into ${distDir}: ${sourceEntry ?? upath.resolve(apiDir, 'index')}`);
64
+ return bundleEffectEntryForNode({
65
+ appDir: appDirectory,
66
+ entryPath: builtEntry,
67
+ format: 'module' === moduleType ? 'esm' : 'cjs'
68
+ });
67
69
  });
68
- }
69
- }
70
- };
71
- const generate = async ()=>{
72
- const { appDirectory, apiDirectory, lambdaDirectory, port, bffRuntimeFramework } = api.getAppContext();
73
- const modernConfig = api.getNormalizedConfig();
74
- const relativeDistPath = modernConfig?.output?.distPath?.root || 'dist';
75
- const { bff } = modernConfig || {};
76
- const prefix = getPrimaryPrefix(bff?.prefix);
77
- const httpMethodDecider = bff?.httpMethodDecider;
78
- const apiRouter = new ApiRouter({
79
- apiDir: apiDirectory,
80
- appDir: appDirectory,
81
- lambdaDir: lambdaDirectory,
82
- prefix,
83
- httpMethodDecider,
84
- isBuild: true
85
- });
86
- const lambdaDir = apiRouter.getLambdaDir();
87
- const existLambda = apiRouter.isExistLambda();
88
- const runtime = bff?.runtimeCreateRequest || RUNTIME_CREATE_REQUEST;
89
- const relativeApiPath = path.relative(appDirectory, apiDirectory);
90
- const relativeLambdaPath = path.relative(appDirectory, lambdaDir);
91
- await pluginGenerator({
92
- prefix,
93
- appDirectory,
94
- relativeDistPath,
95
- relativeApiPath,
96
- relativeLambdaPath,
97
- runtimeFramework: 'hono' === bffRuntimeFramework ? 'hono' : 'effect'
70
+ });
98
71
  });
99
- await clientGenerator({
100
- prefix,
101
- appDir: appDirectory,
102
- apiDir: apiDirectory,
103
- lambdaDir,
104
- existLambda,
105
- port,
106
- requestCreator: bff?.requestCreator,
107
- httpMethodDecider,
108
- relativeDistPath,
109
- relativeApiPath,
110
- bffRuntimeFramework,
111
- effectEntry: bff?.effect?.entry,
112
- effectDataPlatformBatch: bff?.effect?.dataPlatform?.batch
72
+ const generate = ()=>Promise.resolve().then(()=>{
73
+ const { appDirectory, apiDirectory, lambdaDirectory, port, bffRuntimeFramework, packageName } = api.getAppContext();
74
+ const modernConfig = api.getNormalizedConfig();
75
+ const relativeDistPath = modernConfig?.output?.distPath?.root || 'dist';
76
+ const { bff } = modernConfig || {};
77
+ const prefix = getPrimaryPrefix(bff?.prefix);
78
+ const httpMethodDecider = bff?.httpMethodDecider;
79
+ const apiRouter = new ApiRouter({
80
+ apiDir: apiDirectory,
81
+ appDir: appDirectory,
82
+ lambdaDir: lambdaDirectory,
83
+ prefix,
84
+ httpMethodDecider,
85
+ isBuild: true
86
+ });
87
+ const lambdaDir = apiRouter.getLambdaDir();
88
+ const existLambda = apiRouter.isExistLambda();
89
+ const packageJson = fs.readJSONSync(upath.resolve(appDirectory, 'package.json'));
90
+ const requestId = packageJson.name || packageName;
91
+ const operationContractsPromise = existLambda ? apiRouter.getApiHandlers().then((handlers)=>buildOperationContractMap({
92
+ handlers,
93
+ requestId,
94
+ operationVersion: deriveOperationVersion(packageJson.version)
95
+ })) : Promise.resolve({});
96
+ return operationContractsPromise.then((operationContracts)=>{
97
+ const runtime = bff?.runtimeCreateRequest || RUNTIME_CREATE_REQUEST;
98
+ const relativeApiPath = upath.relative(appDirectory, apiDirectory);
99
+ const relativeLambdaPath = upath.relative(appDirectory, lambdaDir);
100
+ const sourceEffectEntry = 'effect' === bffRuntimeFramework ? resolveEffectSourceEntry(appDirectory, apiDirectory, bff?.effect?.entry) : void 0;
101
+ const relativeEffectEntry = void 0 !== sourceEffectEntry && sourceEffectEntry.length > 0 ? upath.relative(appDirectory, sourceEffectEntry).replace(/\.(?:[cm]?ts|tsx|jsx)$/u, '.js') : '';
102
+ return pluginGenerator({
103
+ prefix,
104
+ appDirectory,
105
+ relativeDistPath,
106
+ relativeApiPath,
107
+ relativeLambdaPath,
108
+ runtimeFramework: 'hono' === bffRuntimeFramework ? 'hono' : 'effect',
109
+ relativeEffectEntry,
110
+ operationContracts
111
+ }).then(()=>clientGenerator({
112
+ prefix,
113
+ appDir: appDirectory,
114
+ apiDir: apiDirectory,
115
+ lambdaDir,
116
+ existLambda,
117
+ port,
118
+ requestCreator: bff?.requestCreator,
119
+ httpMethodDecider,
120
+ relativeDistPath,
121
+ relativeApiPath,
122
+ bffRuntimeFramework,
123
+ effectEntry: bff?.effect?.entry,
124
+ effectDataPlatformBatch: bff?.effect?.dataPlatform?.batch
125
+ })).then(()=>runtimeGenerator({
126
+ runtime,
127
+ appDirectory,
128
+ relativeDistPath
129
+ }));
130
+ });
113
131
  });
114
- await runtimeGenerator({
115
- runtime,
116
- appDirectory,
117
- relativeDistPath
132
+ const handleCrossProjectInvocation = (isBuild = false)=>Promise.resolve().then(()=>{
133
+ const { bff } = api.getNormalizedConfig();
134
+ if (bff?.crossProject !== true) return;
135
+ return isBuild ? generate() : compileApi().then(()=>generate());
118
136
  });
119
- };
120
- const handleCrossProjectInvocation = async (isBuild = false)=>{
121
- const { bff } = api.getNormalizedConfig();
122
- if (bff?.crossProject) {
123
- if (!isBuild) await compileApi();
124
- await generate();
125
- }
126
- };
127
137
  return {
128
138
  compileApi,
129
139
  generate,
@@ -1,24 +1,19 @@
1
- import { applyOptionsChain, fs } from "@modern-js/utils";
1
+ import { applyOptionsChain, fs, upath } from "@modern-js/utils";
2
2
  import { transform } from "@swc/core";
3
- import path from "path";
4
3
  const RELEASE_IDENTITY_GLOBAL_NAMES = [
5
4
  'ULTRAMODERN_BUILD_MARKER',
6
5
  'ULTRAMODERN_SOURCE_REVISION',
7
6
  'ULTRAMODERN_RELEASE_VERSION'
8
7
  ];
9
- const collectJavaScriptFiles = async (directory)=>{
10
- const entries = await fs.readdir(directory, {
11
- withFileTypes: true
12
- });
13
- const nested = await Promise.all(entries.map(async (entry)=>{
14
- const absolutePath = path.join(directory, entry.name);
15
- if (entry.isDirectory()) return collectJavaScriptFiles(absolutePath);
16
- return /\.(?:c|m)?js$/u.test(entry.name) ? [
17
- absolutePath
18
- ] : [];
19
- }));
20
- return nested.flat();
21
- };
8
+ const collectJavaScriptFiles = (directory)=>Promise.resolve().then(()=>fs.readdir(directory, {
9
+ withFileTypes: true
10
+ })).then((entries)=>Promise.all(entries.map((entry)=>{
11
+ const absolutePath = upath.join(directory, entry.name);
12
+ if (entry.isDirectory()) return collectJavaScriptFiles(absolutePath);
13
+ return /\.(?:c|m)?js$/u.test(entry.name) ? [
14
+ absolutePath
15
+ ] : [];
16
+ })).then((nested)=>nested.flat()));
22
17
  const serializeServerGlobalVars = (options)=>{
23
18
  const globalVars = applyOptionsChain({}, options, {
24
19
  env: 'server',
@@ -40,37 +35,38 @@ const serializeServerGlobalVars = (options)=>{
40
35
  ];
41
36
  }));
42
37
  };
43
- const transformServerGlobalVars = async (outputDirectories, globalVars)=>{
44
- if (0 === Object.keys(globalVars).length) return;
45
- const existingDirectories = (await Promise.all(outputDirectories.map(async (directory)=>await fs.pathExists(directory) ? directory : void 0))).filter((directory)=>Boolean(directory));
46
- const files = (await Promise.all(existingDirectories.map(collectJavaScriptFiles))).flat();
47
- const releaseIdentityBanner = RELEASE_IDENTITY_GLOBAL_NAMES.every((name)=>Object.hasOwn(globalVars, name)) ? `${RELEASE_IDENTITY_GLOBAL_NAMES.map((name)=>`void ${globalVars[name]};`).join('')}\n` : '';
48
- await Promise.all(files.map(async (filename)=>{
49
- const source = await fs.readFile(filename, 'utf8');
50
- const sourceMapFilename = `${filename}.map`;
51
- const sourceMapExists = await fs.pathExists(sourceMapFilename);
52
- const inputSourceMap = sourceMapExists ? await fs.readFile(sourceMapFilename, 'utf8') : void 0;
53
- const result = await transform(`${releaseIdentityBanner}${source}`, {
54
- filename,
55
- inputSourceMap,
56
- sourceMaps: sourceMapExists,
57
- jsc: {
58
- parser: {
59
- syntax: "ecmascript"
60
- },
61
- preserveAllComments: true,
62
- target: 'es2022',
63
- transform: {
64
- optimizer: {
65
- globals: {
66
- vars: globalVars
67
- }
68
- }
69
- }
70
- }
71
- });
72
- await fs.writeFile(filename, result.code);
73
- if (sourceMapExists && result.map) await fs.writeFile(sourceMapFilename, result.map);
74
- }));
75
- };
38
+ const transformServerGlobalVars = (outputDirectories, globalVars)=>Promise.resolve().then(()=>{
39
+ if (0 === Object.keys(globalVars).length) return;
40
+ return Promise.all(outputDirectories.map((directory)=>fs.pathExists(directory).then((directoryExists)=>directoryExists ? directory : void 0))).then((existingDirectories)=>{
41
+ const filesPromise = Promise.all(existingDirectories.filter((directory)=>void 0 !== directory).map(collectJavaScriptFiles)).then((files)=>files.flat());
42
+ const releaseIdentityBanner = RELEASE_IDENTITY_GLOBAL_NAMES.every((name)=>Object.hasOwn(globalVars, name)) ? `${RELEASE_IDENTITY_GLOBAL_NAMES.map((name)=>`void ${globalVars[name]};`).join('')}\n` : '';
43
+ return filesPromise.then((files)=>Promise.all(files.map((filename)=>fs.readFile(filename, 'utf8').then((source)=>{
44
+ const sourceMapFilename = `${filename}.map`;
45
+ return fs.pathExists(sourceMapFilename).then((sourceMapExists)=>{
46
+ const inputSourceMapPromise = sourceMapExists ? fs.readFile(sourceMapFilename, 'utf8') : Promise.resolve(void 0);
47
+ return inputSourceMapPromise.then((inputSourceMap)=>transform(`${releaseIdentityBanner}${source}`, {
48
+ filename,
49
+ inputSourceMap,
50
+ sourceMaps: sourceMapExists,
51
+ jsc: {
52
+ parser: {
53
+ syntax: "ecmascript"
54
+ },
55
+ preserveAllComments: true,
56
+ target: 'es2022',
57
+ transform: {
58
+ optimizer: {
59
+ globals: {
60
+ vars: globalVars
61
+ }
62
+ }
63
+ }
64
+ }
65
+ }).then((result)=>fs.writeFile(filename, result.code).then(()=>{
66
+ if (sourceMapExists && void 0 !== result.map && result.map.length > 0) return fs.writeFile(sourceMapFilename, result.map);
67
+ })));
68
+ });
69
+ }))));
70
+ }).then(()=>void 0);
71
+ });
76
72
  export { serializeServerGlobalVars, transformServerGlobalVars };
package/dist/esm/cli.mjs CHANGED
@@ -128,4 +128,6 @@ const bffPlugin = ()=>({
128
128
  });
129
129
  }
130
130
  });
131
+ const cli = bffPlugin;
132
+ export default cli;
131
133
  export { bffPlugin };
@@ -4,6 +4,7 @@ import path from "path";
4
4
  import { generateEffectClientCode, resolveEffectEntryFile } from "./utils/effectClientGenerator.mjs";
5
5
  const EFFECT_BFF_WORKER_RUNTIME_QUERY = 'modern-bff-runtime';
6
6
  const EFFECT_BFF_WORKER_RUNTIME_SOURCE_QUERY = 'modern-bff-runtime-source';
7
+ const createErrorModule = (message)=>`throw new Error(${JSON.stringify(message)});`;
7
8
  async function transformEffectRuntimeSource(source, filename) {
8
9
  const swc = await import("@swc/core");
9
10
  const result = await swc.transform(source, {
@@ -25,7 +26,7 @@ async function transformEffectRuntimeSource(source, filename) {
25
26
  function createEffectWorkerRuntimeWrapper(resourcePath) {
26
27
  const sourceRequest = `${resourcePath}?${EFFECT_BFF_WORKER_RUNTIME_SOURCE_QUERY}`;
27
28
  return `import * as effectBffModule from ${JSON.stringify(sourceRequest)};
28
- import { createEffectBffEdgeDispatcher } from '@modern-js/plugin-bff/effect-edge';
29
+ import { createEffectBffEdgeDispatcher } from '@modern-js/plugin-bff/effect-edge/dispatcher';
29
30
 
30
31
  export const __modern_create_effect_bff_dispatcher = options =>
31
32
  createEffectBffEdgeDispatcher({
@@ -66,13 +67,13 @@ async function loader(source) {
66
67
  onDependency: (dependency)=>this.addDependency(dependency)
67
68
  });
68
69
  if (code) return void callback(void 0, code);
69
- callback(void 0, `throw new Error('Failed to generate Effect client for ${resourcePath}')`);
70
+ callback(void 0, createErrorModule(`Failed to generate Effect client for ${resourcePath}`));
70
71
  return;
71
72
  }
72
73
  const warning = `The file ${resourcePath} is not allowed to be imported in src directory, only API definition files are allowed.`;
73
74
  if (!draftOptions.existLambda) {
74
75
  logger.warn(warning);
75
- callback(null, `throw new Error('${warning}')`);
76
+ callback(null, createErrorModule(warning));
76
77
  return;
77
78
  }
78
79
  const options = {
@@ -89,14 +90,14 @@ async function loader(source) {
89
90
  const { lambdaDir } = draftOptions;
90
91
  if (!resourcePath.startsWith(lambdaDir)) {
91
92
  logger.warn(warning);
92
- callback(null, `throw new Error('${warning}')`);
93
+ callback(null, createErrorModule(warning));
93
94
  return;
94
95
  }
95
96
  if (draftOptions.fetcher) options.fetcher = draftOptions.fetcher;
96
97
  if (draftOptions.requestCreator) options.requestCreator = draftOptions.requestCreator;
97
98
  options.requireResolve = require.resolve;
98
99
  const result = await generateClient(options);
99
- callback(void 0, result.isOk ? result.value : `throw new Error('${result.value}')`);
100
+ callback(void 0, result.isOk ? result.value : createErrorModule(result.value));
100
101
  }
101
102
  const src_loader = loader;
102
103
  export default src_loader;
@@ -2,22 +2,27 @@ import { logger } from "@modern-js/utils";
2
2
  import { HttpApi } from "effect/unstable/httpapi";
3
3
  import { resolveAdapterCrossProjectPolicy } from "../../../utils/crossProjectServerPolicy.mjs";
4
4
  import { collectEffectEndpoints, extractHttpApiFromModule, toOperationContractSources } from "../endpoint-contracts.mjs";
5
- async function resolveEffectAdapterCrossProjectPolicy(api, prefix, mod) {
6
- let contractSources = [];
7
- if (mod) try {
8
- const effectApi = await extractHttpApiFromModule(mod, HttpApi.isHttpApi);
9
- if (effectApi) {
10
- const reflect = (apiValue, handlers)=>HttpApi.reflect(apiValue, {
5
+ function resolveEffectAdapterCrossProjectPolicy(api, prefix, mod) {
6
+ const contractSourcesPromise = null === mod ? Promise.resolve([]) : Promise.resolve().then(()=>extractHttpApiFromModule(mod, HttpApi.isHttpApi)).then((effectApi)=>{
7
+ if (null !== effectApi) {
8
+ const reflect = (apiValue, handlers)=>{
9
+ if (!HttpApi.isHttpApi(apiValue)) throw new Error('[BFF][Effect] Endpoint reflection received a non-HttpApi value.');
10
+ HttpApi.reflect(apiValue, {
11
11
  onGroup: handlers.onGroup ?? (()=>{}),
12
12
  onEndpoint: handlers.onEndpoint
13
13
  });
14
- contractSources = toOperationContractSources(collectEffectEndpoints(reflect, effectApi, prefix));
14
+ };
15
+ return toOperationContractSources(collectEffectEndpoints(reflect, effectApi, prefix));
15
16
  }
16
- } catch (error) {
17
+ return [];
18
+ }).catch((error)=>{
17
19
  logger.warn(`[BFF][Effect] Failed reflect HttpApi endpoints cross-project policy: ${String(error)}`);
18
- }
19
- const policy = resolveAdapterCrossProjectPolicy(api, contractSources);
20
- if (policy?.enabled && 0 === contractSources.length) logger.warn('[BFF][Effect] Cross-project policy enabled but no HttpApi endpoints could be reflected; requests fail operation-contract matching unless allowUnknownOperations is enabled.');
21
- return policy;
20
+ return [];
21
+ });
22
+ return contractSourcesPromise.then((contractSources)=>{
23
+ const policy = resolveAdapterCrossProjectPolicy(api, contractSources);
24
+ if (policy?.enabled === true && 0 === contractSources.length) logger.warn('[BFF][Effect] Cross-project policy enabled but no HttpApi endpoints could be reflected; requests fail operation-contract matching unless allowUnknownOperations is enabled.');
25
+ return policy;
26
+ });
22
27
  }
23
28
  export { resolveEffectAdapterCrossProjectPolicy };
@@ -22,23 +22,31 @@ function relativeAppPath(appDirectory, entryPath) {
22
22
  const relativePath = path.relative(appDirectory, entryPath);
23
23
  return '..' === relativePath || relativePath.startsWith(`..${path.sep}`) ? void 0 : relativePath;
24
24
  }
25
+ function isDependencyPath(relativePath) {
26
+ return relativePath?.split(/[\\/]/u).includes('node_modules') ?? false;
27
+ }
25
28
  function resolveEffectAdapterEntryFile(api) {
26
29
  const { appDirectory, apiDirectory, distDirectory } = api.getServerContext();
27
30
  const appRoot = path.resolve(appDirectory || process.cwd());
28
- const productionRoot = isProd() && distDirectory ? distDirectory : void 0;
31
+ const productionRoot = isProd() && distDirectory ? path.resolve(distDirectory) : void 0;
29
32
  const bffConfig = api.getServerConfig()?.bff;
30
33
  const configuredEntry = bffConfig?.effect?.entry;
31
34
  if (configuredEntry) {
32
35
  const sourceEntry = path.isAbsolute(configuredEntry) ? configuredEntry : path.resolve(appRoot, configuredEntry);
33
36
  if (productionRoot) {
34
37
  const relativeEntry = relativeAppPath(appRoot, sourceEntry);
38
+ if (void 0 === relativeEntry || isDependencyPath(relativeEntry)) return resolveJsOrTsEntry(sourceEntry);
35
39
  return relativeEntry ? resolveJsOrTsEntry(builtEntryPath(path.resolve(productionRoot, relativeEntry))) : void 0;
36
40
  }
37
41
  return resolveJsOrTsEntry(sourceEntry);
38
42
  }
39
- const relativeApiDirectory = apiDirectory ? path.isAbsolute(apiDirectory) ? relativeAppPath(appRoot, apiDirectory) : apiDirectory : API_DIR;
40
- if (!relativeApiDirectory) return;
41
- const apiRoot = path.resolve(productionRoot || appRoot, relativeApiDirectory);
42
- return resolveJsOrTsEntry(path.resolve(apiRoot, 'index'));
43
+ const resolvedApiDirectory = path.resolve(appRoot, apiDirectory || API_DIR);
44
+ const productionApiDirectory = productionRoot ? void 0 !== relativeAppPath(productionRoot, resolvedApiDirectory) ? resolvedApiDirectory : (()=>{
45
+ const relativeApiDirectory = relativeAppPath(appRoot, resolvedApiDirectory);
46
+ if (isDependencyPath(relativeApiDirectory)) return resolvedApiDirectory;
47
+ return relativeApiDirectory ? path.resolve(productionRoot, relativeApiDirectory) : void 0;
48
+ })() : resolvedApiDirectory;
49
+ if (!productionApiDirectory) return;
50
+ return resolveJsOrTsEntry(path.resolve(productionApiDirectory, 'index'));
43
51
  }
44
52
  export { resolveEffectAdapterEntryFile };
@@ -1,22 +1,24 @@
1
1
  import { logger } from "@modern-js/utils";
2
2
  import { createSafeFailureResponse } from "../../safe-failure.mjs";
3
- async function createEffectAdapterRuntimeErrorResponse(api, error, c) {
3
+ function createEffectAdapterRuntimeErrorResponse(api, error, c) {
4
4
  try {
5
5
  const serverConfig = api.getServerConfig();
6
6
  const onErrorHandler = serverConfig?.onError;
7
- if (onErrorHandler) {
7
+ if (void 0 !== onErrorHandler) {
8
8
  const onErrorContext = ensureJsonContext(c);
9
- const result = await onErrorHandler(error instanceof Error ? error : new Error(String(error)), onErrorContext);
10
- if (result instanceof Response) return result;
11
- } else logger.error(error);
9
+ return Promise.resolve(onErrorHandler(error instanceof Error ? error : new Error(String(error)), onErrorContext)).then((result)=>result instanceof Response ? result : createSafeFailureResponse(error)).catch((configError)=>{
10
+ logger.error(`Error in serverConfig.onError handler: ${String(configError)}`);
11
+ return createSafeFailureResponse(error);
12
+ });
13
+ }
14
+ logger.error(error instanceof Error ? error : new Error(String(error)));
12
15
  } catch (configError) {
13
16
  logger.error(`Error in serverConfig.onError handler: ${configError}`);
14
17
  }
15
- return createSafeFailureResponse(error);
18
+ return Promise.resolve(createSafeFailureResponse(error));
16
19
  }
17
20
  function ensureJsonContext(c) {
18
- const maybeJsonContext = c;
19
- if ('function' == typeof maybeJsonContext.json) return c;
21
+ if ('json' in c && 'function' == typeof c.json) return c;
20
22
  const headers = {
21
23
  'content-type': 'application/json; charset=utf-8'
22
24
  };
@@ -27,7 +29,7 @@ function ensureJsonContext(c) {
27
29
  headers: extraHeaders
28
30
  } : statusOrInit;
29
31
  const responseHeaders = new Headers(headers);
30
- if (responseInit.headers) new Headers(responseInit.headers).forEach((value, key)=>{
32
+ if (void 0 !== responseInit.headers) new Headers(responseInit.headers).forEach((value, key)=>{
31
33
  responseHeaders.set(key, value);
32
34
  });
33
35
  return new Response(JSON.stringify(data), {
@@ -1,6 +1,6 @@
1
1
  import { fs, isProd, logger } from "@modern-js/utils";
2
2
  import path from "path";
3
- import { loadEffectSourceModule } from "../../utils/effectSourceLoader.mjs";
3
+ import { loadEffectBuiltModule, loadEffectSourceModule } from "../../utils/effectSourceLoader.mjs";
4
4
  import { before } from "./adapter/constants.mjs";
5
5
  import { resolveEffectAdapterCrossProjectPolicy } from "./adapter/cross-project-policy.mjs";
6
6
  import { resolveEffectAdapterEntryFile } from "./adapter/entry.mjs";
@@ -29,10 +29,10 @@ class EffectAdapter {
29
29
  let mod;
30
30
  try {
31
31
  const { appDirectory } = this.api.getServerContext();
32
- mod = await loadEffectSourceModule({
32
+ mod = await (isProd() ? loadEffectBuiltModule(entryFile) : loadEffectSourceModule({
33
33
  resourcePath: entryFile,
34
34
  appDir: appDirectory || path.dirname(entryFile)
35
- });
35
+ }));
36
36
  } catch (error) {
37
37
  logger.error(`[BFF][Effect] Failed to load Effect entry: ${entryFile}\n${String(error)}`);
38
38
  this.handler = null;
@@ -2,27 +2,36 @@ import { BACKEND_FEDERATION_EFFECT_EXPOSE } from "@modern-js/utils/universal";
2
2
  import { formatBackendFederationIdentityIssues, validateExpectedBackendFederationIdentity } from "./identity.mjs";
3
3
  import { createBackendFederationRuntime } from "./runtime.mjs";
4
4
  import { isRecord, normalizeExpose } from "./utils.mjs";
5
+ import * as __rspack_external_effect_Effect_41060786 from "effect/Effect";
6
+ import * as __rspack_external_effect_Logger_1691a01b from "effect/Logger";
5
7
  const LEGACY_LOAD_WARNING = '[BFF][Effect] loadBackendFederatedEffectApi was called without an expected delivery-unit identity (expected.unitId + expected.buildMarker). Identity-less public backend loads are deprecated (ADR-0019/MV-G23) and will be rejected in a future major.';
6
- async function loadBackendFederatedEffectApi(options) {
8
+ const legacyWarningLogger = __rspack_external_effect_Logger_1691a01b.withLeveledConsole(__rspack_external_effect_Logger_1691a01b.formatLogFmt);
9
+ function warnLegacyLoad() {
10
+ __rspack_external_effect_Effect_41060786.runSync(__rspack_external_effect_Effect_41060786.provideService(__rspack_external_effect_Effect_41060786.logWarning(LEGACY_LOAD_WARNING), __rspack_external_effect_Logger_1691a01b.CurrentLoggers, new Set([
11
+ legacyWarningLogger
12
+ ])));
13
+ }
14
+ function loadBackendFederatedEffectApi(options) {
7
15
  const remoteName = options.remote?.name ?? options.remoteName;
8
- if (!remoteName) throw new Error('[BFF][Effect] Missing backend federation remote name.');
9
- if (void 0 === options.expected) console.warn(LEGACY_LOAD_WARNING);
16
+ if (void 0 === remoteName || 0 === remoteName.length) return Promise.reject(new Error('[BFF][Effect] Missing backend federation remote name.'));
17
+ if (void 0 === options.expected) warnLegacyLoad();
10
18
  const runtime = options.runtime ?? createBackendFederationRuntime(options);
11
19
  const expose = options.expose ?? options.remote?.expose ?? BACKEND_FEDERATION_EFFECT_EXPOSE;
12
20
  const remoteRequest = `${remoteName}/${normalizeExpose(expose)}`;
13
- const loaded = await runtime.loadRemote(remoteRequest);
14
- if (!isRecord(loaded)) throw new Error(`[BFF][Effect] Backend federation expose ${remoteRequest} must load an object module.`);
15
- const backendContract = loaded.backendFederationContract;
16
- if (!isRecord(backendContract) || true !== backendContract.strictEffectApproach) throw new Error(`[BFF][Effect] Backend federation expose ${remoteRequest} must expose strict Effect metadata (strictEffectApproach: true).`);
17
- if ('effect' !== backendContract.runtimeFramework) throw new Error(`[BFF][Effect] Backend federation expose ${remoteRequest} must expose strict Effect metadata (runtimeFramework: "effect").`);
18
- if ('string' == typeof backendContract.name && backendContract.name !== remoteName) throw new Error(`[BFF][Effect] Backend federation expose ${remoteRequest} metadata name mismatch: expected ${remoteName}, received ${backendContract.name}.`);
19
- if (!('runtime' in loaded)) throw new Error(`[BFF][Effect] Backend federation expose ${remoteRequest} must expose api and runtime.`);
20
- if (void 0 !== options.expected) {
21
- const issues = validateExpectedBackendFederationIdentity(loaded, options.expected, {
22
- allowMissingIdentityMetadata: true === options.allowMissingIdentityMetadata
23
- });
24
- if (issues.length > 0) throw new Error(`[BFF][Effect] Backend federation expose ${remoteRequest} delivery-unit identity mismatch: ${formatBackendFederationIdentityIssues(issues)}.`);
25
- }
26
- return loaded;
21
+ return runtime.loadRemote(remoteRequest).then((loaded)=>{
22
+ if (!isRecord(loaded)) throw new Error(`[BFF][Effect] Backend federation expose ${remoteRequest} must load an object module.`);
23
+ const backendContract = loaded.backendFederationContract;
24
+ if (!isRecord(backendContract) || true !== backendContract.strictEffectApproach) throw new Error(`[BFF][Effect] Backend federation expose ${remoteRequest} must expose strict Effect metadata (strictEffectApproach: true).`);
25
+ if ('effect' !== backendContract.runtimeFramework) throw new Error(`[BFF][Effect] Backend federation expose ${remoteRequest} must expose strict Effect metadata (runtimeFramework: "effect").`);
26
+ if ('string' == typeof backendContract.name && backendContract.name !== remoteName) throw new Error(`[BFF][Effect] Backend federation expose ${remoteRequest} metadata name mismatch: expected ${remoteName}, received ${backendContract.name}.`);
27
+ if (!('runtime' in loaded)) throw new Error(`[BFF][Effect] Backend federation expose ${remoteRequest} must expose api and runtime.`);
28
+ if (void 0 !== options.expected) {
29
+ const issues = validateExpectedBackendFederationIdentity(loaded, options.expected, {
30
+ allowMissingIdentityMetadata: true === options.allowMissingIdentityMetadata
31
+ });
32
+ if (issues.length > 0) throw new Error(`[BFF][Effect] Backend federation expose ${remoteRequest} delivery-unit identity mismatch: ${formatBackendFederationIdentityIssues(issues)}.`);
33
+ }
34
+ return loaded;
35
+ });
27
36
  }
28
37
  export { loadBackendFederatedEffectApi };
@@ -1,13 +1,23 @@
1
1
  function createBackendFederationLoadEntryPlugin(options) {
2
2
  return {
3
3
  name: 'modernjs-backend-federation-load-entry',
4
- async loadEntry ({ remoteInfo }) {
5
- return options.resolveEntry({
4
+ loadEntry ({ remoteInfo }) {
5
+ return Promise.resolve(options.resolveEntry({
6
6
  name: remoteInfo.name,
7
7
  entry: remoteInfo.entry,
8
8
  type: remoteInfo.type,
9
9
  entryGlobalName: remoteInfo.entryGlobalName,
10
10
  shareScope: remoteInfo.shareScope
11
+ })).then((entry)=>{
12
+ if (void 0 === entry) return;
13
+ return {
14
+ get (id) {
15
+ return ()=>Promise.resolve(entry.get(id)).then((factory)=>factory());
16
+ },
17
+ init (...args) {
18
+ return entry.init?.(...args);
19
+ }
20
+ };
11
21
  });
12
22
  }
13
23
  };