Skip to content

Commit 29678b4

Browse files
authored
fix: bff test case (#7653)
1 parent d653da7 commit 29678b4

File tree

8 files changed

+51
-33
lines changed

8 files changed

+51
-33
lines changed

packages/cli/builder/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export type { CreateBuilderOptions, BuilderInstance };
66
export type {
77
BuilderConfig,
88
BundlerType,
9+
ToolsDevServerConfig,
910
MetaOptions,
1011
Stats,
1112
MultiStats,

packages/cli/plugin-bff/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"@modern-js/create-request": "workspace:*",
8686
"@modern-js/server-core": "workspace:*",
8787
"@modern-js/server-utils": "workspace:*",
88+
"@modern-js/builder": "workspace:*",
8889
"@modern-js/utils": "workspace:*",
8990
"@swc/helpers": "^0.5.17",
9091
"type-is": "^1.6.18"

packages/cli/plugin-bff/src/cli.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { IncomingMessage } from 'http';
22
import path from 'path';
33
import type { AppTools, CliPlugin } from '@modern-js/app-tools';
44
import { ApiRouter } from '@modern-js/bff-core';
5+
import type { ToolsDevServerConfig } from '@modern-js/builder';
56
import { compile } from '@modern-js/server-utils';
67
import type { ServerRoute } from '@modern-js/types';
78
import {
@@ -144,6 +145,33 @@ export const bffPlugin = (): CliPlugin<AppTools> => ({
144145
return bffRuntimeFramework === 'hono';
145146
};
146147

148+
const createCompressConfig = (
149+
devServer: ToolsDevServerConfig | undefined,
150+
prefix: string,
151+
) => {
152+
if (
153+
!devServer ||
154+
typeof devServer !== 'object' ||
155+
Array.isArray(devServer)
156+
) {
157+
return undefined;
158+
}
159+
160+
const { compress } = devServer;
161+
162+
if (compress === undefined || compress === true) {
163+
return {
164+
filter: (req: IncomingMessage) => !req.url?.includes(prefix),
165+
};
166+
}
167+
168+
if (compress === false) {
169+
return false;
170+
}
171+
172+
return compress;
173+
};
174+
147175
api.config(async () => {
148176
const honoRuntimePath = isHono()
149177
? { [RUNTIME_HONO]: RUNTIME_HONO }
@@ -152,23 +180,13 @@ export const bffPlugin = (): CliPlugin<AppTools> => ({
152180
const devServer = api.getConfig()?.tools?.devServer;
153181
const prefix = api.getConfig()?.bff?.prefix || DEFAULT_API_PREFIX;
154182

155-
if (
156-
typeof devServer === 'object' &&
157-
devServer !== null &&
158-
!Array.isArray(devServer)
159-
) {
160-
const compress = devServer.compress;
161-
if (typeof compress === 'undefined' || compress === true) {
162-
devServer.compress = {
163-
filter: (req: IncomingMessage) => {
164-
return !req.url?.includes(prefix);
165-
},
166-
};
167-
}
168-
}
183+
const compress = createCompressConfig(devServer, prefix);
169184

170185
return {
171186
tools: {
187+
devServer: {
188+
compress,
189+
},
172190
bundlerChain: (chain, { CHAIN_ID, isServer }) => {
173191
const { port, appDirectory, apiDirectory, lambdaDirectory } =
174192
api.getAppContext();

packages/cli/plugin-bff/src/utils/crossProjectApiPlugin.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const crossProjectApiPlugin = (): CliPlugin<AppTools> => ({
1313
name: '@modern-js/plugin-independent-bff',
1414
post: ['@modern-js/plugin-bff'],
1515
setup: api => {
16-
api.config(async () => {
16+
api.modifyResolvedConfig(resolvedConfig => {
1717
const { appDirectory: originAppDirectory } = api.getAppContext();
1818

1919
const sdkPath = path.join(originAppDirectory, NODE_MODULES, PACKAGE_NAME);
@@ -22,19 +22,20 @@ export const crossProjectApiPlugin = (): CliPlugin<AppTools> => ({
2222
const apiDirectory = path.join(sdkDistPath, API_DIR);
2323
const lambdaDirectory = path.resolve(sdkDistPath, LAMBDA_DIR);
2424

25-
const appContext = api.getAppContext();
26-
2725
api.updateAppContext({
28-
...appContext,
2926
apiDirectory,
3027
lambdaDirectory,
3128
});
32-
return {
33-
bff: {
34-
prefix: PREFIX,
35-
isCrossProjectServer: true,
36-
},
37-
};
29+
const config = api.getConfig();
30+
if (config?.bff?.prefix) {
31+
console.warn(
32+
`[WARNING] Detected bff.prefix configuration: "${config.bff.prefix}".
33+
When using cross-project BFF, you should not configure bff.prefix as it may cause API path conflicts or access issues. Please remove the bff.prefix configuration.`,
34+
);
35+
}
36+
resolvedConfig.bff.prefix = PREFIX;
37+
(resolvedConfig.bff as any).isCrossProjectServer = true;
38+
return resolvedConfig;
3839
});
3940
},
4041
});

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/bff-corss-project/bff-api-app/modern.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default applyBaseConfig({
77
prefix: '/api-app',
88
crossProject: true,
99
},
10-
plugins: [expressPlugin(), bffPlugin()],
10+
plugins: [expressPlugin() as any, bffPlugin()],
1111
server: {
1212
port: 3399,
1313
},

tests/integration/bff-corss-project/bff-client-app/modern.config.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ import { crossProjectApiPlugin } from 'bff-api-app/plugin';
44
import { applyBaseConfig } from '../../../utils/applyBaseConfig';
55

66
export default applyBaseConfig({
7-
bff: {
8-
prefix: '/web-app',
9-
},
107
server: {
118
ssr: false,
129
port: 3401,
1310
},
14-
plugins: [bffPlugin(), expressPlugin(), crossProjectApiPlugin()],
11+
plugins: [bffPlugin(), expressPlugin() as any, crossProjectApiPlugin()],
1512
});

tests/integration/bff-corss-project/bff-indep-client-app/modern.config.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ import { expressPlugin } from '@modern-js/plugin-express';
33
import { applyBaseConfig } from '../../../utils/applyBaseConfig';
44

55
export default applyBaseConfig({
6-
bff: {
7-
prefix: '/indep-web-app',
8-
},
96
server: {
107
ssr: false,
118
},
12-
plugins: [bffPlugin(), expressPlugin()],
9+
plugins: [bffPlugin(), expressPlugin() as any],
1310
});

0 commit comments

Comments
 (0)