-
Notifications
You must be signed in to change notification settings - Fork 397
feat: server config hot reload #7264
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@modern-js/app-tools': patch | ||
'@modern-js/server': patch | ||
--- | ||
|
||
feat: server config hot reload | ||
feat: 支持自定义 web server 热更新 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import path from 'path'; | ||
import type { ServerPluginLegacy } from '@modern-js/server-core'; | ||
import { restart } from '../utils/createServer'; | ||
|
||
export default (): ServerPluginLegacy => ({ | ||
name: '@modern-js/server-hmr-plugin', | ||
setup: api => { | ||
return { | ||
async reset({ event }) { | ||
if (event.type === 'file-change') { | ||
const { appDirectory } = api.useAppContext(); | ||
const serverPath = path.join(appDirectory, 'server'); | ||
const indexPath = path.join(serverPath, 'index'); | ||
const isServerFileChanged = event.payload.some( | ||
({ filename }) => | ||
filename.startsWith(serverPath) && | ||
!filename.startsWith(indexPath), | ||
); | ||
if (isServerFileChanged) { | ||
await restart(); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,11 @@ export default (): CliPluginFuture<AppTools<'shared'>> => ({ | |
], | ||
}, | ||
})); | ||
api._internalServerPlugins(({ plugins }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better create a new server plugin. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, this plugin should not be used in production |
||
plugins.push({ | ||
name: '@modern-js/app-tools/server/plugin', | ||
}); | ||
return { plugins }; | ||
}); | ||
}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,158 @@ | ||
import type { Server } from 'node:http'; | ||
import type net from 'net'; | ||
import type { Server as HttpServer } from 'node:http'; | ||
import type { Http2SecureServer } from 'node:http2'; | ||
import { applyPlugins } from '@modern-js/prod-server'; | ||
import { | ||
type ApplyPlugins, | ||
type ModernDevServerOptions, | ||
createDevServer, | ||
} from '@modern-js/server'; | ||
|
||
let server: Server | Http2SecureServer | null = null; | ||
type AnyServer = HttpServer | Http2SecureServer; | ||
type Socket = net.Socket; | ||
let server: AnyServer | null = null; | ||
let initialServerOptions: ModernDevServerOptions | null = null; | ||
const activeSockets = new Set<Socket>(); | ||
let restartCallback: (() => Promise<void>) | null = null; | ||
|
||
export const getServer = () => server; | ||
|
||
export const setServer = (newServer: Server | Http2SecureServer) => { | ||
export const setServer = (newServer: AnyServer) => { | ||
server = newServer; | ||
}; | ||
|
||
export const closeServer = async () => { | ||
if (server) { | ||
server.close(); | ||
server = null; | ||
} | ||
export const closeServer = (timeout = 5000): Promise<void> => { | ||
return new Promise((resolve, reject) => { | ||
if (!server) { | ||
resolve(); | ||
return; | ||
} | ||
|
||
const cleanupSockets = () => { | ||
for (const socket of activeSockets) { | ||
try { | ||
socket.destroy(); | ||
} catch (e) { | ||
console.error('Error destroying socket:', e); | ||
} | ||
} | ||
activeSockets.clear(); | ||
}; | ||
|
||
let isClosed = false; | ||
cleanupSockets(); | ||
|
||
const timer = setTimeout(() => { | ||
if (isClosed) return; | ||
isClosed = true; | ||
|
||
cleanupSockets(); | ||
server?.removeAllListeners(); | ||
reject(new Error(`Server close timed out after ${timeout}ms`)); | ||
}, timeout); | ||
|
||
server.close(err => { | ||
if (isClosed) return; | ||
isClosed = true; | ||
|
||
clearTimeout(timer); | ||
cleanupSockets(); | ||
server?.removeAllListeners(); | ||
server = null; | ||
|
||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
export const createServer = async ( | ||
options: ModernDevServerOptions, | ||
): Promise<Server | Http2SecureServer> => { | ||
applyPluginsFn?: ApplyPlugins, | ||
) => { | ||
if (server) { | ||
server.close(); | ||
try { | ||
await closeServer(); | ||
} catch (error) { | ||
console.error('Error closing existing server:', error); | ||
} | ||
} | ||
|
||
const { server: newServer, afterListen } = await createDevServer( | ||
options, | ||
applyPluginsFn || applyPlugins, | ||
); | ||
|
||
server = newServer; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same logic as |
||
|
||
server.on('connection', (socket: Socket) => { | ||
activeSockets.add(socket); | ||
|
||
socket.on('close', () => { | ||
activeSockets.delete(socket); | ||
}); | ||
}); | ||
|
||
setServerOptions(options); | ||
setServer(newServer); | ||
return { server, afterListen }; | ||
}; | ||
|
||
export const setServerOptions = (options: ModernDevServerOptions): void => { | ||
initialServerOptions = options; | ||
}; | ||
|
||
export const restart = async (): Promise<AnyServer> => { | ||
if (!initialServerOptions) { | ||
throw new Error('Cannot restart server: Initial options not available'); | ||
} | ||
|
||
try { | ||
await closeServer(); | ||
} catch (error) { | ||
console.error('Error closing server during restart:', error); | ||
} | ||
server = (await createDevServer(options, applyPlugins)).server; | ||
|
||
return server; | ||
const { server: newServer, afterListen } = await createDevServer( | ||
{ | ||
...initialServerOptions, | ||
}, | ||
applyPlugins, | ||
); | ||
|
||
server = newServer; | ||
restartCallback = afterListen; | ||
|
||
server.on('connection', (socket: Socket) => { | ||
activeSockets.add(socket); | ||
|
||
socket.on('close', () => { | ||
activeSockets.delete(socket); | ||
}); | ||
}); | ||
|
||
return new Promise((resolve, reject) => { | ||
server!.listen( | ||
initialServerOptions?.dev.port, | ||
initialServerOptions?.dev.host, | ||
async (err?: Error) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
|
||
try { | ||
if (restartCallback) { | ||
await restartCallback(); | ||
} | ||
resolve(server!); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}, | ||
); | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setServer
andsetServerOptions
is never used