Skip to content

Commit c73217b

Browse files
authored
chore: support serve command use custom serve launcher (#6817)
1 parent 2249d77 commit c73217b

File tree

4 files changed

+55
-54
lines changed

4 files changed

+55
-54
lines changed

.changeset/good-cars-knock.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@modern-js/app-tools': patch
3+
'@modern-js/prod-server': patch
4+
---
5+
6+
chore: support serve command use custom serve launcher
7+
chore: 支持 serve 命令使用自定义启动器

packages/server/prod-server/src/index.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import {
66
loadServerRuntimeConfig,
77
} from '@modern-js/server-core/node';
88
import sourceMapSupport from 'source-map-support';
9-
import {
10-
type ApplyPlugins,
11-
applyPlugins as defaultApplyPlugins,
12-
} from './apply';
9+
import { applyPlugins } from './apply';
1310
import type { BaseEnv, ProdServerOptions } from './types';
1411

1512
sourceMapSupport.install();
@@ -25,10 +22,7 @@ export type { ServerPlugin } from '@modern-js/server-core';
2522

2623
export type { ProdServerOptions, BaseEnv } from './types';
2724

28-
export const createProdServer = async (
29-
options: ProdServerOptions,
30-
applyPlugins?: ApplyPlugins,
31-
) => {
25+
export const createProdServer = async (options: ProdServerOptions) => {
3226
await loadServerEnv(options);
3327

3428
const serverBaseOptions = options;
@@ -57,7 +51,7 @@ export const createProdServer = async (
5751
// load env file.
5852
const nodeServer = await createNodeServer(server.handle.bind(server));
5953

60-
await (applyPlugins || defaultApplyPlugins)(server, options, nodeServer);
54+
await applyPlugins(server, options, nodeServer);
6155

6256
await server.init();
6357

packages/solutions/app-tools/src/commands/dev.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { registerCompiler } from '../utils/register';
1717
import { generateRoutes } from '../utils/routes';
1818
import type { DevOptions } from '../utils/types';
1919

20-
export interface ExtraServerOptions {
20+
interface ExtraServerOptions {
2121
applyPlugins?: ApplyPlugins;
2222
}
2323

packages/solutions/app-tools/src/commands/serve.ts

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
import type { AppNormalizedConfig, AppTools } from '../types';
1212
import { loadServerPlugins } from '../utils/loadPlugins';
1313
import { printInstructions } from '../utils/printInstructions';
14-
import type { ExtraServerOptions } from './dev';
14+
15+
type ExtraServerOptions = {
16+
launcher?: typeof createProdServer;
17+
};
1518

1619
export const serve = async (
1720
api: CLIPluginAPI<AppTools<'shared'>>,
@@ -54,50 +57,47 @@ export const serve = async (
5457

5558
const pluginInstances = await loadServerPlugins(api, appDirectory, metaName);
5659

57-
const app = await createProdServer(
58-
{
59-
metaName,
60-
pwd: distDirectory,
61-
config: {
62-
...userConfig,
63-
dev: userConfig.dev as any,
64-
// server-core can't get RegExp & Function output.enableInlineScripts by JSON.stringy;
65-
output: {
66-
path: userConfig.output.distPath?.root,
67-
...(userConfig.output || {}),
68-
} as any,
69-
},
70-
routes: serverRoutes,
71-
plugins: pluginInstances,
72-
serverConfigFile,
73-
serverConfigPath,
74-
appContext: {
75-
appDirectory,
76-
internalDirectory,
77-
sharedDirectory: getTargetDir(
78-
appContext.sharedDirectory,
79-
appContext.appDirectory,
80-
appContext.distDirectory,
81-
),
82-
apiDirectory: isCrossProjectServer
83-
? appContext.apiDirectory
84-
: getTargetDir(
85-
appContext.apiDirectory,
86-
appContext.appDirectory,
87-
appContext.distDirectory,
88-
),
89-
lambdaDirectory: isCrossProjectServer
90-
? appContext.lambdaDirectory
91-
: getTargetDir(
92-
appContext.lambdaDirectory,
93-
appContext.appDirectory,
94-
appContext.distDirectory,
95-
),
96-
},
97-
runMode,
60+
const app = await (serverOptions?.launcher || createProdServer)({
61+
metaName,
62+
pwd: distDirectory,
63+
config: {
64+
...userConfig,
65+
dev: userConfig.dev as any,
66+
// server-core can't get RegExp & Function output.enableInlineScripts by JSON.stringy;
67+
output: {
68+
path: userConfig.output.distPath?.root,
69+
...(userConfig.output || {}),
70+
} as any,
9871
},
99-
serverOptions?.applyPlugins,
100-
);
72+
routes: serverRoutes,
73+
plugins: pluginInstances,
74+
serverConfigFile,
75+
serverConfigPath,
76+
appContext: {
77+
appDirectory,
78+
internalDirectory,
79+
sharedDirectory: getTargetDir(
80+
appContext.sharedDirectory,
81+
appContext.appDirectory,
82+
appContext.distDirectory,
83+
),
84+
apiDirectory: isCrossProjectServer
85+
? appContext.apiDirectory
86+
: getTargetDir(
87+
appContext.apiDirectory,
88+
appContext.appDirectory,
89+
appContext.distDirectory,
90+
),
91+
lambdaDirectory: isCrossProjectServer
92+
? appContext.lambdaDirectory
93+
: getTargetDir(
94+
appContext.lambdaDirectory,
95+
appContext.appDirectory,
96+
appContext.distDirectory,
97+
),
98+
},
99+
runMode,
100+
});
101101

102102
app.listen(port, async () => {
103103
await printInstructions(

0 commit comments

Comments
 (0)