Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: vite runtime api #10091

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rare-balloons-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": minor
---

Vite Runtime API
13 changes: 10 additions & 3 deletions packages/astro/src/content/vite-plugin-content-assets.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -28,6 +28,7 @@ export function astroContentAssetPropagationPlugin({
settings: AstroSettings;
}): Plugin {
let devModuleLoader: ModuleLoader;
let teardown: (() => void | Promise<void>) | undefined;
return {
name: 'astro:content-asset-propagation',
enforce: 'pre',
Expand All @@ -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) {
Expand Down Expand Up @@ -115,6 +119,9 @@ export function astroContentAssetPropagationPlugin({
return { code, map: { mappings: '' } };
}
},
async buildEnd() {
await teardown?.();
},
};
}

Expand Down
8 changes: 6 additions & 2 deletions packages/astro/src/core/module-loader/vite.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 17 additions & 3 deletions packages/astro/src/vite-plugin-astro-server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,12 +30,22 @@ export default function createVitePluginAstroServer({
logger,
fs: fsMod,
}: AstroPluginOptions): vite.Plugin {
let teardown: (() => void | Promise<void>) | 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();
Expand Down Expand Up @@ -103,6 +114,9 @@ export default function createVitePluginAstroServer({
// Replace the Vite overlay with ours
return patchOverlay(code);
},
async buildEnd() {
await teardown?.();
},
};
}

Expand Down
Loading