refactor: cleanup render package

This commit is contained in:
Nate Moore 2021-10-16 17:31:47 -05:00
parent 2a6f2601d6
commit 4eafad8017
5 changed files with 15 additions and 24 deletions

View file

@ -2,9 +2,7 @@ import type { Renderer } from './astro';
export interface AstroConfigMinimal {
/** production website, needed for some RSS & Sitemap functions */
origin: string;
/** the web request (needed for dynamic routes) */
pathname?: string;
site: string;
/** Renderers for SSR */
renderers?: Renderer[];
}

View file

@ -1,9 +1,11 @@
import type { AstroConfigMinimal } from '../@types/config-minimal';
export let config: AstroConfigMinimal | null = null;
export let config: AstroConfigMinimal = {
site: 'https://example.com',
renderers: []
};
export function setConfig(options: AstroConfigMinimal): void {
validateConfig(options);
export function configure(options: AstroConfigMinimal): void {
if (!config) {
config = {} as any;
}
@ -12,11 +14,3 @@ export function setConfig(options: AstroConfigMinimal): void {
}
}
export function validateConfig(config: unknown) {
if (!config) {
throw new Error(`[Astro]: You must call \`setConfig(value)\` before rendering!`)
}
if (typeof config !== 'object') {
throw new Error(`[Astro]: \`config\` must be of type "object". Found typeof "${typeof config}"!`)
}
}

View file

@ -1,5 +1,5 @@
export type { AstroConfigMinimal } from './@types/config-minimal';
export type { RenderResult } from './render';
export { setConfig } from './config/index.js';
export { configure } from './config/index.js';
export { render } from './render/index.js';

View file

@ -1,7 +1,7 @@
import type { AstroComponentFactory } from '../internal'
import { createResult, renderToString, AstroElement } from './utils.js';
import { config, validateConfig } from '../config/index.js';
import { config } from '../config/index.js';
export interface RenderResult {
html: { code: string };
@ -9,9 +9,8 @@ export interface RenderResult {
js: AstroElement[];
}
export async function render(Component: AstroComponentFactory, props: Record<string, any>): Promise<RenderResult> {
validateConfig(config);
const result = createResult(config!)
export async function render(Component: AstroComponentFactory, props: Record<string, any>, req: { url: string }): Promise<RenderResult> {
const result = createResult(config!, req);
const template = await renderToString(result, Component, props);
const styles = Array.from(result.styles).map(({ props, children }: any) => new AstroElement('style', props, children));
const scripts = Array.from(result.scripts).map(({ props, children }: any) => new AstroElement('script', props, children));

View file

@ -4,7 +4,7 @@ import type { Renderer } from '../@types/astro';
import type { AstroConfigMinimal } from '../@types/config-minimal'
import type { AstroComponentFactory, AstroComponent } from '../internal';
import { defineStyleVars, defineScriptVars, spreadAttributes } from '../internal';
import { defineStyleVars, defineScriptVars, spreadAttributes } from '../internal/index.js';
export interface CreateResultOptions {
/** production website, needed for some RSS & Sitemap functions */
@ -14,15 +14,15 @@ export interface CreateResultOptions {
renderers?: Renderer[];
}
export function createResult({ origin, pathname = '/', renderers = [] }: AstroConfigMinimal): SSRResult {
export function createResult({ site: origin, renderers = [] }: AstroConfigMinimal, req: { url: string }): SSRResult {
const result: SSRResult = {
styles: new Set(),
scripts: new Set(),
/** This function returns the `Astro` faux-global */
createAstro(astroGlobal: TopLevelAstro, props: Record<string, any>, slots: Record<string, any> | null) {
const site = new URL(origin);
const url = new URL('.' + pathname, site);
const canonicalURL = getCanonicalURL('.' + pathname, origin);
const url = new URL(req.url);
const canonicalURL = getCanonicalURL(url.pathname, origin);
return {
__proto__: astroGlobal,
@ -94,6 +94,6 @@ export async function renderAstroComponent(component: InstanceType<typeof AstroC
export async function renderToString(result: SSRResult, componentFactory: AstroComponentFactory, props: Record<string|number, any>, slots: Record<string, AstroComponentFactory>|null = null) {
const Component = await componentFactory(result, props, slots);
let template = await renderAstroComponent(Component);
const template = await renderAstroComponent(Component);
return template;
}