Skip to content

Commit 5be8756

Browse files
committed
feat: solve memory leaks
1 parent 40c89f0 commit 5be8756

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

packages/server/core/src/serverBase.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ export class ServerBase<E extends Env = any> {
180180
get onError() {
181181
return this.app.onError.bind(this.app);
182182
}
183+
184+
close() {
185+
this.serverContext = null;
186+
this.plugins = [];
187+
(this.app as Hono<E> | null) = null;
188+
}
183189
}
184190

185191
export function createServerBase<E extends Env>(options: ServerBaseOptions) {

packages/server/server/src/createDevServer.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import { logger } from '@modern-js/utils';
88
import { devPlugin, manager } from './dev';
99
import { getDevAssetPrefix, getDevOptions } from './helpers';
1010
import { ResourceType } from './helpers/utils';
11-
import serverHmrPlugin from './plugins/serverHmr';
11+
import serverHmrPlugin from './plugins/serverReload';
1212
import type { ApplyPlugins, ModernDevServerOptions } from './types';
1313

14+
export let serverReload: (() => Promise<void>) | null = null;
15+
1416
async function createServerOptions(
1517
options: ModernDevServerOptions,
1618
serverConfigPath: string,
@@ -44,8 +46,8 @@ export async function createDevServer(
4446
distDir,
4547
);
4648

47-
const server = createServerBase(prodServerOptions);
48-
let currentServer = server;
49+
let currentServer = createServerBase(prodServerOptions);
50+
4951
let isReloading = false;
5052

5153
const devHttpsOption = typeof dev === 'object' && dev.https;
@@ -75,8 +77,10 @@ export async function createDevServer(
7577
if (isReloading) {
7678
return;
7779
}
80+
isReloading = true;
81+
7882
try {
79-
isReloading = true;
83+
await currentServer.close();
8084

8185
const updatedProdServerOptions = await createServerOptions(
8286
options,
@@ -88,25 +92,30 @@ export async function createDevServer(
8892
await manager.close(ResourceType.Watcher);
8993

9094
newServer.addPlugins([
91-
serverHmrPlugin(reload),
92-
devPlugin({
93-
...options,
94-
}),
95+
serverHmrPlugin(),
96+
devPlugin(
97+
{
98+
...options,
99+
builderDevServer: undefined,
100+
},
101+
true,
102+
),
95103
]);
96-
await applyPlugins(newServer, updatedProdServerOptions, nodeServer);
104+
await applyPlugins(newServer, updatedProdServerOptions);
97105
await newServer.init();
98106

99107
currentServer = newServer;
108+
100109
logger.info(`Custom Web Server HMR succeeded`);
101110
} catch (e) {
102111
logger.error('[Custom Web Server HMR failed]:', e);
103112
} finally {
104113
isReloading = false;
105114
}
106115
};
107-
108-
server.addPlugins([
109-
serverHmrPlugin(reload),
116+
serverReload = reload;
117+
currentServer.addPlugins([
118+
serverHmrPlugin(),
110119
devPlugin({
111120
...options,
112121
builderDevServer,
@@ -119,9 +128,9 @@ export async function createDevServer(
119128
prodServerOptions.config.output.assetPrefix = assetPrefix;
120129
}
121130

122-
await applyPlugins(server, prodServerOptions, nodeServer);
131+
await applyPlugins(currentServer, prodServerOptions, nodeServer);
123132

124-
await server.init();
133+
await currentServer.init();
125134

126135
const afterListen = async () => {
127136
await builderDevServer?.afterListen();

packages/server/server/src/dev.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ export type DevPluginOptions = ModernDevServerOptions<ServerBaseOptions> & {
2121

2222
export const manager = new ResourceManager();
2323

24-
export const devPlugin = (options: DevPluginOptions): ServerPlugin => ({
24+
export const devPlugin = (
25+
options: DevPluginOptions,
26+
isReload = false,
27+
): ServerPlugin => ({
2528
name: '@modern-js/plugin-dev',
2629

2730
setup(api) {
@@ -56,11 +59,12 @@ export const devPlugin = (options: DevPluginOptions): ServerPlugin => ({
5659
// TODO: remove any
5760
const hooks = (api as any).getHooks();
5861

59-
builder?.onDevCompileDone(({ stats }) => {
60-
if (stats.toJson({ all: false }).name !== 'server') {
61-
onRepack(distDirectory!, hooks);
62-
}
63-
});
62+
!isReload &&
63+
builder?.onDevCompileDone(({ stats }) => {
64+
if (stats.toJson({ all: false }).name !== 'server') {
65+
onRepack(distDirectory!, hooks);
66+
}
67+
});
6468

6569
const { watchOptions } = config.server;
6670
const watcher = startWatcher({

packages/server/server/src/plugins/serverHmr.ts renamed to packages/server/server/src/plugins/serverReload.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import path from 'path';
22
import type { ServerPlugin } from '@modern-js/server-core';
33

4-
export default (reload: () => Promise<void>): ServerPlugin => ({
5-
name: '@modern-js/server-hmr-plugin',
4+
import { serverReload } from '../createDevServer';
5+
6+
export default (): ServerPlugin => ({
7+
name: '@modern-js/server-reload-plugin',
68
setup: api => {
79
api.onReset(async ({ event }) => {
810
if (event.type === 'file-change') {
@@ -14,7 +16,7 @@ export default (reload: () => Promise<void>): ServerPlugin => ({
1416
filename.startsWith(serverPath) && !filename.startsWith(indexPath),
1517
);
1618
if (isServerFileChanged) {
17-
await reload();
19+
await serverReload?.();
1820
}
1921
}
2022
});

0 commit comments

Comments
 (0)