Handle server restart from Vite plugins (#5849)
This commit is contained in:
parent
e818cc0466
commit
8c100a6fe6
3 changed files with 72 additions and 28 deletions
5
.changeset/poor-ladybugs-thank.md
Normal file
5
.changeset/poor-ladybugs-thank.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Handle server restart from Vite plugins
|
|
@ -142,11 +142,9 @@ export async function createContainerWithAutomaticRestart({
|
|||
},
|
||||
};
|
||||
|
||||
function handleServerRestart(logMsg: string) {
|
||||
async function handleServerRestart(logMsg: string) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-shadow
|
||||
const container = restart.container;
|
||||
return async function (changedFile: string) {
|
||||
if (shouldRestartContainer(container, changedFile)) {
|
||||
const { container: newContainer, error } = await restartContainer({
|
||||
beforeRestart,
|
||||
container,
|
||||
|
@ -172,15 +170,25 @@ export async function createContainerWithAutomaticRestart({
|
|||
resolveRestart = resolve;
|
||||
});
|
||||
}
|
||||
|
||||
function handleChangeRestart(logMsg: string) {
|
||||
return async function (changedFile: string) {
|
||||
if (shouldRestartContainer(restart.container, changedFile)) {
|
||||
handleServerRestart(logMsg);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Set up watches
|
||||
function addWatches() {
|
||||
const watcher = restart.container.viteServer.watcher;
|
||||
watcher.on('change', handleServerRestart('Configuration updated. Restarting...'));
|
||||
watcher.on('unlink', handleServerRestart('Configuration removed. Restarting...'));
|
||||
watcher.on('add', handleServerRestart('Configuration added. Restarting...'));
|
||||
watcher.on('change', handleChangeRestart('Configuration updated. Restarting...'));
|
||||
watcher.on('unlink', handleChangeRestart('Configuration removed. Restarting...'));
|
||||
watcher.on('add', handleChangeRestart('Configuration added. Restarting...'));
|
||||
|
||||
// Restart the Astro dev server instead of Vite's when the API is called by plugins.
|
||||
// Ignore the `forceOptimize` parameter for now.
|
||||
restart.container.viteServer.restart = () => handleServerRestart('Restarting...');
|
||||
}
|
||||
addWatches();
|
||||
return restart;
|
||||
|
|
|
@ -180,4 +180,35 @@ describe('dev container restarts', () => {
|
|||
await restart.container.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('Is able to restart on viteServer.restart API call', async () => {
|
||||
const fs = createFs(
|
||||
{
|
||||
'/src/pages/index.astro': ``,
|
||||
},
|
||||
root
|
||||
);
|
||||
|
||||
const { astroConfig } = await openConfig({
|
||||
cwd: root,
|
||||
flags: {},
|
||||
cmd: 'dev',
|
||||
logging: defaultLogging,
|
||||
});
|
||||
const settings = createSettings(astroConfig, fileURLToPath(root));
|
||||
|
||||
let restart = await createContainerWithAutomaticRestart({
|
||||
params: { fs, root, settings },
|
||||
});
|
||||
await startContainer(restart.container);
|
||||
expect(isStarted(restart.container)).to.equal(true);
|
||||
|
||||
try {
|
||||
let restartComplete = restart.restarted();
|
||||
await restart.container.viteServer.restart();
|
||||
await restartComplete;
|
||||
} finally {
|
||||
await restart.container.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue