Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dev server should call rsbuild.afterListen when listen #6008

Merged
merged 6 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/old-years-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/app-tools': patch
'@modern-js/server': patch
---

fix: dev server should call rsbuild.afterListen when listen
fix: dev server 应该在 listen 时,调用 rsbuild.afterListen
22 changes: 19 additions & 3 deletions packages/server/server/src/createDevServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function createDevServer(
options: ModernDevServerOptions,
applyPlugins: ApplyPlugins,
) {
const { config, pwd, serverConfigFile, serverConfigPath } = options;
const { config, pwd, serverConfigFile, serverConfigPath, builder } = options;
const dev = getDevOptions(options);

const distDir = path.resolve(pwd, config.output.path || 'dist');
Expand Down Expand Up @@ -47,11 +47,27 @@ export async function createDevServer(
nodeServer = await createNodeServer(server.handle.bind(server));
}

server.addPlugins([devPlugin(options)]);
const builderDevServer = await builder?.createDevServer({
runCompile: options.runCompile,
});

server.addPlugins([
devPlugin({
...options,
builderDevServer,
}),
]);

await applyPlugins(server, prodServerOptions, nodeServer);

await server.init();

return nodeServer;
const afterListen = async () => {
await builderDevServer?.afterListen();
};

return {
server: nodeServer,
afterListen,
};
}
19 changes: 11 additions & 8 deletions packages/server/server/src/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ServerPlugin, ServerBaseOptions } from '@modern-js/server-core';
import { connectMid2HonoMid } from '@modern-js/server-core/node';
import { API_DIR, SHARED_DIR } from '@modern-js/utils';
import { RequestHandler } from '@modern-js/types';
import { UniBuilderInstance } from '@modern-js/uni-builder';
import { ModernDevServerOptions } from './types';
import {
startWatcher,
Expand All @@ -11,24 +12,26 @@ import {
getMockMiddleware,
} from './helpers';

export const devPlugin = <O extends ServerBaseOptions>(
options: ModernDevServerOptions<O>,
): ServerPlugin => ({
type BuilderDevServer = Awaited<
ReturnType<UniBuilderInstance['createDevServer']>
>;

export type DevPluginOptions = ModernDevServerOptions<ServerBaseOptions> & {
builderDevServer?: BuilderDevServer;
};

export const devPlugin = (options: DevPluginOptions): ServerPlugin => ({
name: '@modern-js/plugin-dev',

setup(api) {
const { config, pwd, builder } = options;
const { config, pwd, builder, builderDevServer } = options;

const closeCb: Array<(...args: []) => any> = [];

const dev = getDevOptions(options);

return {
async prepare() {
const builderDevServer = await builder?.createDevServer({
runCompile: options.runCompile,
});

// https://github.com/web-infra-dev/rsbuild/blob/32fbb85e22158d5c4655505ce75e3452ce22dbb1/packages/shared/src/types/server.ts#L112
const {
middlewares: builderMiddlewares,
Expand Down
10 changes: 7 additions & 3 deletions packages/solutions/app-tools/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const dev = async (
const host = normalizedConfig.dev?.host || DEFAULT_DEV_HOST;

if (apiOnly) {
const server = await createDevServer(
const { server } = await createDevServer(
{
...serverOptions,
runCompile: false,
Expand All @@ -123,7 +123,7 @@ export const dev = async (
},
);
} else {
const server = await createDevServer(
const { server, afterListen } = await createDevServer(
{
...serverOptions,
builder: appContext.builder,
Expand All @@ -136,10 +136,14 @@ export const dev = async (
port,
host,
},
(err?: Error) => {
async (err?: Error) => {
if (err) {
logger.error('Occur error %s, when start dev server', err);
}

logger.debug('listen dev server done');

await afterListen();
},
);

Expand Down
3 changes: 2 additions & 1 deletion packages/solutions/app-tools/src/utils/createServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const createServer = async (
if (server) {
server.close();
}
server = await createDevServer(options, applyPlugins);
// eslint-disable-next-line prefer-destructuring
server = (await createDevServer(options, applyPlugins)).server;

return server;
};
Loading