Get scripts and styles in devapp

This commit is contained in:
Matthew Phillips 2023-02-13 08:36:16 -05:00
parent 26458cf4f4
commit c0febd56f9
4 changed files with 34 additions and 33 deletions

View file

@ -1,10 +1,20 @@
import type { ManifestData, RouteData } from '../../@types/astro';
import type { ComponentInstance, ManifestData, RouteData } from '../../@types/astro';
import type { SSRManifest as Manifest } from './types';
import { createContainer, type CreateContainerParams } from '../dev/index.js';
import { createViteLoader } from '../module-loader/index.js';
import { createRouteManifest } from '../routing/index.js';
import { createDevelopmentEnvironment, preload, type DevelopmentEnvironment } from '../render/dev/index.js';
import {
createDevelopmentEnvironment,
getScriptsAndStyles,
preload,
type DevelopmentEnvironment
} from '../render/dev/index.js';
import {
createRenderContext,
renderPage as coreRenderPage
} from '../render/index.js';
import { App, MatchOptions } from './index.js';
import { RenderContext, Environment } from '../render';
export type DevAppParams = Partial<CreateContainerParams> & {
root: URL;
@ -16,6 +26,7 @@ export class DevApp extends App {
#container: Awaited<ReturnType<typeof createContainer>> | null = null;
#env: DevelopmentEnvironment | null = null;
#root: URL;
#modToRoute = new Map<ComponentInstance, RouteData>();
constructor(params: DevAppParams) {
const { root, userConfig } = params;
const manifest: Manifest = {
@ -98,7 +109,24 @@ export class DevApp extends App {
// Save this module in the pageMap, so that super.render() finds it.
this.#manifest.pageMap.set(route.component, mod);
this.#modToRoute.set(mod, route);
}
return super.render(request, route);
}
renderPage = async (mod: ComponentInstance, ctx: RenderContext, env: Environment) => {
const route = this.#modToRoute.get(mod)!;
const { scripts, links, styles, propagationMap } = await getScriptsAndStyles({
env: this.#env!,
filePath: new URL(route.component, this.#root),
});
ctx.scripts = scripts;
ctx.links = links;
ctx.styles = styles;
ctx.propagation = propagationMap;
return coreRenderPage(mod, ctx, env);
};
}

View file

@ -47,6 +47,7 @@ export class App {
};
#base: string;
#baseWithoutTrailingSlash: string;
renderPage: typeof renderPage;
constructor(manifest: Manifest, streaming = true) {
this.#manifest = manifest;
@ -80,6 +81,7 @@ export class App {
this.#base = this.#manifest.base || '/';
this.#baseWithoutTrailingSlash = removeTrailingForwardSlash(this.#base);
this.renderPage = renderPage;
}
updateRoutes(routes: RouteInfo[]) {
this.#manifestData = {
@ -212,7 +214,7 @@ export class App {
status,
});
const response = await renderPage(mod, ctx, this.#env);
const response = await this.renderPage(mod, ctx, this.#env);
return response;
} catch (err: any) {
error(this.#logging, 'ssr', err.stack || err.message || String(err));

View file

@ -74,7 +74,7 @@ interface GetScriptsAndStylesParams {
filePath: URL;
}
async function getScriptsAndStyles({ env, filePath }: GetScriptsAndStylesParams) {
export async function getScriptsAndStyles({ env, filePath }: GetScriptsAndStylesParams) {
// Add hoisted script tags
const scripts = await getScriptsForURL(filePath, env.loader);

View file

@ -52,40 +52,11 @@ describe('base configuration', () => {
try {
const request = new Request(`http://localhost:8080/docs`);
debugger;
const response = await app.render(request);
expect(response.status).to.equal(200);
console.log(await response.text());
} finally {
await app.close();
}
/*
await runInContainer(
{
fs,
root,
userConfig: {
base: '/docs',
trailingSlash: 'never',
},
},
async (container) => {
const { req, res, done, text } = createRequestAndResponse({
method: 'GET',
url: '/docs',
});
container.handle(req, res);
await done;
expect(res.statusCode).to.equal(200);
}
);
*/
});
});