Add type declarations (#59)

This commit is contained in:
Drew Powers 2021-04-02 21:01:57 -06:00 committed by GitHub
parent 008ffc2951
commit d9733e8d42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 30 deletions

25
src/@types/renderer.ts Normal file
View file

@ -0,0 +1,25 @@
export interface DynamicRenderContext {
componentUrl: string;
componentExport: string;
frameworkUrls: string;
}
export interface ComponentRenderer {
renderStatic: StaticRendererGenerator;
render(context: { root: string; Component: string; props: string; [key: string]: string }): string;
imports?: Record<string, string[]>;
}
export interface ComponentContext {
'data-astro-id': string;
root: string;
}
export type StaticRenderer = (props: Record<string, any>, ...children: any[]) => Promise<string>;
export type StaticRendererGenerator<T = any> = (Component: T) => StaticRenderer;
export type DynamicRenderer = (props: Record<string, any>, ...children: any[]) => Promise<string>;
export type DynamicRendererContext<T = any> = (Component: T, renderContext: DynamicRenderContext) => DynamicRenderer;
export type DynamicRendererGenerator = (
wrapperStart: string | ((context: ComponentContext) => string),
wrapperEnd: string | ((context: ComponentContext) => string)
) => DynamicRendererContext;

View file

@ -1,11 +1,12 @@
import { Renderer, createRenderer } from './renderer';
import { h, render } from 'preact';
import { renderToString } from 'preact-render-to-string';
import type { ComponentRenderer } from '../../@types/renderer';
import { createRenderer } from './renderer';
// This prevents tree-shaking of render.
Function.prototype(render);
const Preact: Renderer = {
const Preact: ComponentRenderer = {
renderStatic(Component) {
return async (props, ...children) => renderToString(h(Component, props, ...children));
},

View file

@ -1,8 +1,9 @@
import { Renderer, createRenderer } from './renderer';
import type { ComponentRenderer } from '../../@types/renderer';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { createRenderer } from './renderer';
const ReactRenderer: Renderer = {
const ReactRenderer: ComponentRenderer = {
renderStatic(Component) {
return async (props, ...children) => ReactDOMServer.renderToString(React.createElement(Component, props, children));
},

View file

@ -1,18 +1,8 @@
interface DynamicRenderContext {
componentUrl: string;
componentExport: string;
frameworkUrls: string;
}
export interface Renderer {
renderStatic(Component: any): (props: Record<string, any>, ...children: any[]) => Promise<string>;
render(context: { root: string; Component: string; props: string; [key: string]: string }): string;
imports?: Record<string, string[]>;
}
import type { ComponentRenderer, DynamicRenderContext, DynamicRendererGenerator, StaticRendererGenerator } from '../../@types/renderer';
/** Initialize Astro Component renderer for Static and Dynamic components */
export function createRenderer(renderer: Renderer) {
const _static: Renderer['renderStatic'] = (Component: any) => renderer.renderStatic(Component);
export function createRenderer(renderer: ComponentRenderer) {
const _static: StaticRendererGenerator = (Component) => renderer.renderStatic(Component);
const _imports = (context: DynamicRenderContext) => {
const values = Object.values(renderer.imports ?? {})
.reduce((acc, v) => {
@ -31,12 +21,9 @@ export function createRenderer(renderer: Renderer) {
const astroId = `${Math.floor(Math.random() * 1e16)}`;
return { ['data-astro-id']: astroId, root: `document.querySelector('[data-astro-id="${astroId}"]')`, Component: 'Component' };
};
const createDynamicRender = (
wrapperStart: string | ((context: ReturnType<typeof createContext>) => string),
wrapperEnd: string | ((context: ReturnType<typeof createContext>) => string)
) => (Component: any, renderContext: DynamicRenderContext) => {
const createDynamicRender: DynamicRendererGenerator = (wrapperStart, wrapperEnd) => (Component, renderContext) => {
const innerContext = createContext();
return async (props: Record<string, any>, ...children: any[]) => {
return async (props, ...children) => {
let value: string;
try {
value = await _static(Component)(props, ...children);

View file

@ -1,6 +1,7 @@
import { Renderer, createRenderer } from './renderer';
import type { ComponentRenderer } from '../../@types/renderer';
import { createRenderer } from './renderer';
const SvelteRenderer: Renderer = {
const SvelteRenderer: ComponentRenderer = {
renderStatic(Component) {
return async (props, ...children) => {
const { html } = Component.render(props);

View file

@ -1,8 +1,9 @@
import type { ComponentRenderer } from '../../@types/renderer';
import { renderToString } from '@vue/server-renderer';
import { createSSRApp, h as createElement } from 'vue';
import { Renderer, createRenderer } from './renderer';
import { createRenderer } from './renderer';
const Vue: Renderer = {
const Vue: ComponentRenderer = {
renderStatic(Component) {
return async (props, ...children) => {
const app = createSSRApp({

View file

@ -5,13 +5,13 @@
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"target": "ES2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "ES2020", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */