diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 63bdb6f7a92b..c350ed0fa353 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -25,7 +25,7 @@ import type { AstroSettings, RoutesList } from '../types/astro.js'; import { vitePluginAdapterConfig } from '../vite-plugin-adapter-config/index.js'; import { vitePluginApp } from '../vite-plugin-app/index.js'; import astroVitePlugin from '../vite-plugin-astro/index.js'; -import { vitePluginAstroServer } from '../vite-plugin-astro-server/index.js'; +import { vitePluginAstroServer, vitePluginAstroServerClient } from '../vite-plugin-astro-server/index.js'; import configAliasVitePlugin from '../vite-plugin-config-alias/index.js'; import { astroDevCssPlugin } from '../vite-plugin-css/index.js'; import vitePluginFileURL from '../vite-plugin-fileurl/index.js'; @@ -161,6 +161,7 @@ export async function createVite( // the build to run very slow as the filewatcher is triggered often. command === 'dev' && vitePluginApp(), command === 'dev' && vitePluginAstroServer({ settings, logger }), + command === 'dev' && vitePluginAstroServerClient(), astroDevCssPlugin({ routesList, command }), importMetaEnv({ envLoader }), astroEnv({ settings, sync, envLoader }), diff --git a/packages/astro/src/vite-plugin-astro-server/index.ts b/packages/astro/src/vite-plugin-astro-server/index.ts index ed6bd9296435..54fe2e02b8bc 100644 --- a/packages/astro/src/vite-plugin-astro-server/index.ts +++ b/packages/astro/src/vite-plugin-astro-server/index.ts @@ -1,2 +1,2 @@ export { createController, runWithErrorHandling } from './controller.js'; -export { default as vitePluginAstroServer } from './plugin.js'; +export { default as vitePluginAstroServer, createVitePluginAstroServerClient as vitePluginAstroServerClient } from './plugin.js'; diff --git a/packages/astro/src/vite-plugin-astro-server/plugin.ts b/packages/astro/src/vite-plugin-astro-server/plugin.ts index d70f986e0d93..59bdfcd99096 100644 --- a/packages/astro/src/vite-plugin-astro-server/plugin.ts +++ b/packages/astro/src/vite-plugin-astro-server/plugin.ts @@ -167,6 +167,23 @@ export default function createVitePluginAstroServer({ }; } +export function createVitePluginAstroServerClient(): vite.Plugin { + return { + name: 'astro:server-client', + applyToEnvironment(environment) { + return environment.name === 'client'; + }, + transform(code, id, opts = {}) { + if (opts.ssr) return; + if (!id.includes('vite/dist/client/client.mjs')) return; + + // Replace the Vite overlay with ours + return patchOverlay(code); + } + } +} + + /** * It creates a `SSRManifest` from the `AstroSettings`. *