From 67f52c24b0bc477d13dcb6bd13750e655574ea96 Mon Sep 17 00:00:00 2001 From: Adam Chalemian Date: Mon, 4 May 2026 10:07:55 -0400 Subject: [PATCH] fix(sync): skip adapter configureServer hooks during type generation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The temp Vite server created by `astro build` for type generation only resolves virtual modules — it never serves HTTP requests. Adapter plugins (notably `@cloudflare/vite-plugin` via `@astrojs/cloudflare`) were still firing `configureServer` and starting heavy runtimes (miniflare/workerd), and Vite 7's per-environment config meant `optimizeDeps.noDiscovery` only applied to the `client` environment so adapters were re-injecting large `optimizeDeps.include` lists into server environments. Two changes in `createTempViteServer`: 1. Set `optimizeDeps: { noDiscovery: true, include: [] }` on every environment (`client`, `ssr`, `astro`, `prerender`). 2. Add an inline `astro:sync:strip-server-hooks` plugin that removes `configureServer` from non-Astro plugins in `configResolved`. Astro's own plugins are preserved. On a representative project this drops the “Types Generated” phase from ~3.6 s to ~125 ms. Closes #16332 --- .changeset/fast-sync-temp-server.md | 5 +++++ packages/astro/src/core/sync/index.ts | 32 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 .changeset/fast-sync-temp-server.md diff --git a/.changeset/fast-sync-temp-server.md b/.changeset/fast-sync-temp-server.md new file mode 100644 index 000000000000..bd170c55b6c7 --- /dev/null +++ b/.changeset/fast-sync-temp-server.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Skips adapter `configureServer` hooks and per-environment dependency pre-bundling during the temporary Vite server used by `astro build` for type generation. The temp server only resolves virtual modules — it never serves HTTP requests — so starting adapter runtimes (e.g. miniflare/workerd via `@astrojs/cloudflare`) and pre-bundling adapter `optimizeDeps.include` lists were both unnecessary. On a representative project this reduces the “Types Generated” phase from ~3.6 s to ~125 ms. diff --git a/packages/astro/src/core/sync/index.ts b/packages/astro/src/core/sync/index.ts index bf04261585f1..bcec400b72d3 100644 --- a/packages/astro/src/core/sync/index.ts +++ b/packages/astro/src/core/sync/index.ts @@ -234,6 +234,12 @@ async function createTempViteServer( { dev: true }, ); + // Disable dependency optimization for all environments. The temp server only needs + // to resolve virtual modules for type generation, not pre-bundle dependencies. + // Setting this per-environment ensures adapter plugins (e.g. Cloudflare) see the + // flag and skip injecting their own optimizeDeps.include lists. + const noOptimize = { optimizeDeps: { noDiscovery: true, include: [] } }; + const tempViteServer = await createServer( await createVite( { @@ -241,6 +247,32 @@ async function createTempViteServer( optimizeDeps: { noDiscovery: true }, ssr: { external: [] }, logLevel: 'silent', + environments: { + client: noOptimize, + ssr: noOptimize, + astro: noOptimize, + prerender: noOptimize, + }, + plugins: [ + // Strip `configureServer` hooks from non-Astro plugins. The temp + // server is only used to resolve modules for type generation — it + // never serves HTTP requests. Removing these hooks prevents adapter + // plugins from starting heavy runtimes (e.g. miniflare/workerd for + // Cloudflare). Astro's own plugins (prefixed with `astro:`) are + // preserved because some rely on configureServer to set up state + // needed by their other hooks. + { + name: 'astro:sync:strip-server-hooks', + enforce: 'pre', + configResolved(config) { + for (const plugin of config.plugins) { + if (plugin.configureServer && !plugin.name.startsWith('astro:')) { + delete plugin.configureServer; + } + } + }, + }, + ], }, { routesList,