Compare commits

...

3 commits

Author SHA1 Message Date
Nate Moore
f011ff8695 always externalize astro/container 2023-02-02 22:25:21 -06:00
Nate Moore
8d00309193 do not require opts 2023-02-02 22:25:21 -06:00
Nate Moore
33017d7ea6 wip: add container API 2023-02-02 22:25:21 -06:00
3 changed files with 76 additions and 1 deletions

View file

@ -28,6 +28,10 @@
"types": "./dist/@types/astro.d.ts",
"default": "./astro.js"
},
"./container": {
"types": "./dist/container/index.d.ts",
"default": "./dist/container/index.js"
},
"./env": "./env.d.ts",
"./types": "./types.d.ts",
"./client": "./client.d.ts",

View file

@ -0,0 +1,71 @@
import { renderComponentToIterable } from '../runtime/server/index.js';
import { HTMLParts } from '../runtime/server/render/common.js';
import { nodeLogDestination } from '../core/logger/node.js';
import { createBasicEnvironment, createRenderContext } from '../core/render/index.js';
import { createResult } from '../core/render/result.js';
import type { CreateBasicEnvironmentArgs, Environment } from '../core/render/environment.js';
export class AstroContainer {
env: Environment;
constructor(opts: Pick<CreateBasicEnvironmentArgs, 'renderers'|'site'>) {
this.env = createBasicEnvironment({
...opts,
mode: 'production',
logging: { dest: nodeLogDestination, level: 'error' },
});
}
async renderToString(
Component: any,
{
request = new Request('http://localhost:3000'),
props = {},
params = {},
slots = {},
}: {
props?: Record<string | number | symbol, any>,
slots?: Record<string, any>
params?: Record<string, any>,
request?: Request,
} = {}
) {
const { env } = this;
const ctx = createRenderContext({ request })
const result = createResult({
adapterName: env.adapterName,
links: ctx.links,
styles: ctx.styles,
logging: env.logging,
markdown: env.markdown,
mode: env.mode,
origin: ctx.origin,
params,
props,
pathname: ctx.pathname,
propagation: ctx.propagation,
resolve: env.resolve,
renderers: env.renderers,
request: ctx.request,
site: env.site,
scripts: ctx.scripts,
ssr: env.ssr,
status: ctx.status ?? 200,
});
const output = await renderComponentToIterable(
result,
Component.name ?? 'Component',
Component,
props,
slots
);
if (typeof output !== 'string' && Symbol.asyncIterator in output) {
let parts = new HTMLParts();
for await (const chunk of output) {
parts.append(chunk, result);
}
return parts.toString();
}
return String(output);
}
}

View file

@ -153,7 +153,7 @@ export async function createVite(
// shiki is imported by Code.astro, which is no-externalized (processed by Vite).
// However, shiki's deps are in CJS and trips up Vite's dev SSR transform, externalize
// shiki to load it with node instead.
external: [...(mode === 'dev' ? ['shiki'] : []), ...astroPkgsConfig.ssr.external],
external: [...(mode === 'dev' ? ['shiki'] : []), 'astro/container', ...astroPkgsConfig.ssr.external],
},
};