0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Update Vite ws property to hot (#10138)

This commit is contained in:
Bjorn Lu 2024-02-16 21:15:46 +08:00 committed by GitHub
parent 5ff288f61b
commit 41a6baa080
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 19 additions and 19 deletions

View file

@ -172,7 +172,7 @@ export async function createContentTypesGenerator({
}
const collectionInfo = collectionEntryMap[collectionKey];
if (collectionInfo.type === 'content') {
viteServer.ws.send({
viteServer.hot.send({
type: 'error',
err: new AstroError({
...AstroErrorData.MixedContentDataCollectionError,
@ -212,7 +212,7 @@ export async function createContentTypesGenerator({
}
const collectionInfo = collectionEntryMap[collectionKey];
if (collectionInfo.type === 'data') {
viteServer.ws.send({
viteServer.hot.send({
type: 'error',
err: new AstroError({
...AstroErrorData.MixedContentDataCollectionError,
@ -359,7 +359,7 @@ async function writeContentFiles({
typeTemplateContent: string;
contentEntryTypes: Pick<ContentEntryType, 'contentModuleTypes'>[];
contentConfig?: ContentConfig;
viteServer: Pick<ViteDevServer, 'ws'>;
viteServer: Pick<ViteDevServer, 'hot'>;
}) {
let contentTypesStr = '';
let dataTypesStr = '';
@ -374,7 +374,7 @@ async function writeContentFiles({
collection.type !== 'unknown' &&
collection.type !== collectionConfig.type
) {
viteServer.ws.send({
viteServer.hot.send({
type: 'error',
err: new AstroError({
...AstroErrorData.ContentCollectionTypeMismatchError,
@ -387,7 +387,7 @@ async function writeContentFiles({
collection.type === 'data'
? "Try adding `type: 'data'` to your collection config."
: undefined,
location: { file: '' /** required for error overlay `ws` messages */ },
location: { file: '' /** required for error overlay `hot` messages */ },
}) as any,
});
return;

View file

@ -80,7 +80,7 @@ export async function restartContainer(container: Container): Promise<Container
);
}
// Inform connected clients of the config error
container.viteServer.ws.send({
container.viteServer.hot.send({
type: 'error',
err: {
message: error.message,

View file

@ -31,8 +31,8 @@ export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader {
}
});
const _wsSend = viteServer.ws.send;
viteServer.ws.send = function (...args: any) {
const _wsSend = viteServer.hot.send;
viteServer.hot.send = function (...args: any) {
// If the tsconfig changed, Vite will trigger a reload as it invalidates the module.
// However in Astro, the whole server is restarted when the tsconfig changes. If we
// do a restart and reload at the same time, the browser will refetch and the server
@ -75,13 +75,13 @@ export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader {
return viteServer.ssrFixStacktrace(err);
},
clientReload() {
viteServer.ws.send({
viteServer.hot.send({
type: 'full-reload',
path: '*',
});
},
webSocketSend(msg) {
return viteServer.ws.send(msg);
return viteServer.hot.send(msg);
},
isHttps() {
return !!viteServer.config.server.https;

View file

@ -101,14 +101,14 @@ export async function syncInternal(
)
);
// Patch `ws.send` to bubble up error events
// `ws.on('error')` does not fire for some reason
const wsSend = tempViteServer.ws.send;
tempViteServer.ws.send = (payload: HMRPayload) => {
// Patch `hot.send` to bubble up error events
// `hot.on('error')` does not fire for some reason
const hotSend = tempViteServer.hot.send;
tempViteServer.hot.send = (payload: HMRPayload) => {
if (payload.type === 'error') {
throw payload.err;
}
return wsSend(payload);
return hotSend(payload);
};
try {

View file

@ -54,7 +54,7 @@ export async function handleHotUpdate(
ctx.server.moduleGraph.invalidateModule(mod);
}
}
ctx.server.ws.send({ type: 'full-reload', path: '*' });
ctx.server.hot.send({ type: 'full-reload', path: '*' });
}
}
}

View file

@ -25,21 +25,21 @@ export default function astroDevToolbar({ settings, logger }: AstroPluginOptions
}
},
configureServer(server) {
server.ws.on('astro:devtoolbar:error:load', (args) => {
server.hot.on('astro:devtoolbar:error:load', (args) => {
logger.error(
'toolbar',
`Failed to load dev toolbar app from ${args.entrypoint}: ${args.error}`
);
});
server.ws.on('astro:devtoolbar:error:init', (args) => {
server.hot.on('astro:devtoolbar:error:init', (args) => {
logger.error(
'toolbar',
`Failed to initialize dev toolbar app ${args.app.name} (${args.app.id}):\n${args.error}`
);
});
server.ws.on('astro:devtoolbar:app:toggled', (args) => {
server.hot.on('astro:devtoolbar:app:toggled', (args) => {
// Debounce telemetry to avoid recording events when the user is rapidly toggling apps for debugging
clearTimeout(telemetryTimeout);
telemetryTimeout = setTimeout(() => {