diff --git a/.changeset/rare-balloons-add.md b/.changeset/rare-balloons-add.md new file mode 100644 index 000000000000..e636ffe73b16 --- /dev/null +++ b/.changeset/rare-balloons-add.md @@ -0,0 +1,5 @@ +--- +"astro": minor +--- + +Vite Runtime API diff --git a/packages/astro/src/content/vite-plugin-content-assets.ts b/packages/astro/src/content/vite-plugin-content-assets.ts index b985eb9a73fb..2270f68b27c2 100644 --- a/packages/astro/src/content/vite-plugin-content-assets.ts +++ b/packages/astro/src/content/vite-plugin-content-assets.ts @@ -1,6 +1,6 @@ import { extname } from 'node:path'; import { pathToFileURL } from 'node:url'; -import type { Plugin, Rollup } from 'vite'; +import { createViteRuntime, type Plugin, type Rollup } from 'vite'; import type { AstroSettings } from '../@types/astro.js'; import { moduleIsTopLevelPage, walkParentInfos } from '../core/build/graph.js'; import { getPageDataByViteID, type BuildInternals } from '../core/build/internal.js'; @@ -28,6 +28,7 @@ export function astroContentAssetPropagationPlugin({ settings: AstroSettings; }): Plugin { let devModuleLoader: ModuleLoader; + let teardown: (() => void | Promise) | undefined; return { name: 'astro:content-asset-propagation', enforce: 'pre', @@ -48,9 +49,12 @@ export function astroContentAssetPropagationPlugin({ return this.resolve(base, importer, { skipSelf: true, ...opts }); } }, - configureServer(server) { + async configureServer(server) { if (mode === 'dev') { - devModuleLoader = createViteLoader(server); + const runtime = await createViteRuntime(server); + // @ts-ignore + teardown = 'teardown' in runtime ? runtime.teardown : undefined; + devModuleLoader = createViteLoader(server, runtime); } }, async transform(_, id, options) { @@ -115,6 +119,9 @@ export function astroContentAssetPropagationPlugin({ return { code, map: { mappings: '' } }; } }, + async buildEnd() { + await teardown?.(); + }, }; } diff --git a/packages/astro/src/core/module-loader/vite.ts b/packages/astro/src/core/module-loader/vite.ts index 1b2a4423cfee..a23c5cb646c5 100644 --- a/packages/astro/src/core/module-loader/vite.ts +++ b/packages/astro/src/core/module-loader/vite.ts @@ -1,9 +1,13 @@ import { EventEmitter } from 'node:events'; import path from 'node:path'; +import type { ViteRuntime } from 'vite/runtime'; import type * as vite from 'vite'; import type { ModuleLoader, ModuleLoaderEventEmitter } from './loader.js'; -export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader { +export function createViteLoader( + viteServer: vite.ViteDevServer, + viteRuntime: ViteRuntime +): ModuleLoader { const events = new EventEmitter() as ModuleLoaderEventEmitter; let isTsconfigUpdated = false; @@ -50,7 +54,7 @@ export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader { return { import(src) { - return viteServer.ssrLoadModule(src); + return viteRuntime.executeUrl(src); }, async resolveId(spec, parent) { const ret = await viteServer.pluginContainer.resolveId(spec, parent); diff --git a/packages/astro/src/vite-plugin-astro-server/plugin.ts b/packages/astro/src/vite-plugin-astro-server/plugin.ts index bea6d75ef0e4..9bc4a9a81cc3 100644 --- a/packages/astro/src/vite-plugin-astro-server/plugin.ts +++ b/packages/astro/src/vite-plugin-astro-server/plugin.ts @@ -17,6 +17,7 @@ import { IncomingMessage } from 'node:http'; import { setRouteError } from './server-state.js'; import { recordServerError } from './error.js'; import { toRoutingStrategy } from '../i18n/utils.js'; +import { createViteRuntime } from 'vite'; export interface AstroPluginOptions { settings: AstroSettings; @@ -29,12 +30,22 @@ export default function createVitePluginAstroServer({ logger, fs: fsMod, }: AstroPluginOptions): vite.Plugin { + let teardown: (() => void | Promise) | undefined; return { name: 'astro:server', - configureServer(viteServer) { - const loader = createViteLoader(viteServer); + async configureServer(viteServer) { + const viteRuntime = await createViteRuntime(viteServer); + // @ts-ignore + teardown = 'teardown' in viteRuntime ? viteRuntime.teardown : undefined; + + const loader = createViteLoader(viteServer, viteRuntime); const manifest = createDevelopmentManifest(settings); - const pipeline = DevPipeline.create({ loader, logger, manifest, settings }); + const pipeline = DevPipeline.create({ + loader, + logger, + manifest, + settings, + }); let manifestData: ManifestData = createRouteManifest({ settings, fsMod }, logger); const controller = createController({ loader }); const localStorage = new AsyncLocalStorage(); @@ -103,6 +114,9 @@ export default function createVitePluginAstroServer({ // Replace the Vite overlay with ours return patchOverlay(code); }, + async buildEnd() { + await teardown?.(); + }, }; }