Skip to content

Commit

Permalink
Fix internal imports
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Sep 23, 2021
1 parent be05785 commit 3c20e7b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 60 deletions.
7 changes: 4 additions & 3 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
".": "./astro.js",
"./client/*": "./dist/client/*",
"./components": "./components/index.js",
"./debug": "./components/Debug.astro",
"./components/*": "./components/*",
"./package.json": "./package.json",
"./debug": "./components/Debug.astro",
"./internal": "./dist/internal/index.js",
"./internal/*": "./dist/internal/*",
"./runtime/*": "./dist/runtime/*.js",
"./internal": "./dist/internal/index.js"
"./package.json": "./package.json"
},
"imports": {
"#astro/*": "./dist/*.js"
Expand Down
67 changes: 52 additions & 15 deletions packages/astro/src/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { AstroComponentMetadata } from '../@types/astro';
import { valueToEstree } from 'estree-util-value-to-estree';
import * as astring from 'astring';
import shorthash from 'shorthash';
import { renderToString, renderAstroComponent } from '../runtime/astro.js';

const { generate, GENERATOR } = astring;

Expand Down Expand Up @@ -77,11 +76,11 @@ export interface AstroComponentFactory {
isAstroComponentFactory?: boolean;
}

export const createComponent = (cb: AstroComponentFactory) => {
export function createComponent(cb: AstroComponentFactory) {
// Add a flag to this callback to mark it as an Astro component
(cb as any).isAstroComponentFactory = true;
return cb;
};
}

function extractHydrationDirectives(inputProps: Record<string | number, any>): { hydrationDirective: [string, any] | null; props: Record<string | number, any> } {
let props: Record<string | number, any> = {};
Expand Down Expand Up @@ -135,14 +134,14 @@ setup("${astroId}", {${metadata.hydrateArgs ? `value: ${JSON.stringify(metadata.
return hydrationScript;
}

export const renderSlot = async (result: any, slotted: string, fallback?: any) => {
export async function renderSlot(result: any, slotted: string, fallback?: any) {
if (slotted) {
return _render(slotted);
}
return fallback;
};
}

export const renderComponent = async (result: any, displayName: string, Component: unknown, _props: Record<string | number, any>, slots?: any) => {
export async function renderComponent(result: any, displayName: string, Component: unknown, _props: Record<string | number, any>, slots?: any) {
Component = await Component;
// children = await renderGenerator(children);
const { renderers } = result._metadata;
Expand Down Expand Up @@ -196,35 +195,73 @@ export const renderComponent = async (result: any, displayName: string, Componen
result.scripts.add(await generateHydrateScript({ renderer, astroId, props }, metadata as Required<AstroComponentMetadata>));

return `<astro-root uid="${astroId}">${html}</astro-root>`;
};
}

export const addAttribute = (value: any, key: string) => {
export function addAttribute(value: any, key: string) {
if (value == null || value === false) {
return '';
}
return ` ${key}="${value}"`;
};
}

export const spreadAttributes = (values: Record<any, any>) => {
export function spreadAttributes(values: Record<any, any>) {
let output = '';
for (const [key, value] of Object.entries(values)) {
output += addAttribute(value, key);
}
return output;
};
}

export const defineStyleVars = (astroId: string, vars: Record<any, any>) => {
export function defineStyleVars(astroId: string, vars: Record<any, any>) {
let output = '\n';
for (const [key, value] of Object.entries(vars)) {
output += ` --${key}: ${value};\n`;
}
return `.${astroId} {${output}}`;
};
}

export const defineScriptVars = (vars: Record<any, any>) => {
export function defineScriptVars(vars: Record<any, any>) {
let output = '';
for (const [key, value] of Object.entries(vars)) {
output += `let ${key} = ${JSON.stringify(value)};\n`;
}
return output;
};
}

export async function renderToString(result: any, componentFactory: AstroComponentFactory, props: any, children: any) {
const Component = await componentFactory(result, props, children);
let template = await renderAstroComponent(Component);
return template;
}

export async function renderPage(result: any, Component: AstroComponentFactory, props: any, children: any) {
const template = await renderToString(result, Component, props, children);
const styles = Array.from(result.styles).map((style) => `<style>${style}</style>`);
const scripts = Array.from(result.scripts);
return template.replace('</head>', styles.join('\n') + scripts.join('\n') + '</head>');
}

export async function renderAstroComponent(component: InstanceType<typeof AstroComponent>) {
let template = '';

for await (const value of component) {
if (value || value === 0) {
template += value;
}
}

return template;
}

function renderElement(name: string, { props: _props, children = '' }: { props: Record<any, any>; children?: string }) {
const { hoist: _, 'data-astro-id': astroId, 'define:vars': defineVars, ...props } = _props;
if (defineVars) {
if (name === 'style') {
children = defineStyleVars(astroId, defineVars) + '\n' + children;
}
if (name === 'script') {
children = defineScriptVars(defineVars) + '\n' + children;
}
}
return `<${name}${spreadAttributes(props)}>${children}</${name}>`;
}
41 changes: 0 additions & 41 deletions packages/astro/src/runtime/astro.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/astro/src/runtime/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { fileURLToPath } from 'url';
import fs from 'fs';
import path from 'path';
import slash from 'slash';
import { renderPage } from './astro.js';
import glob from 'tiny-glob';
import { renderPage } from '../internal/index.js';
import { generatePaginateFunction } from './paginate.js';
import { getParams, validateGetStaticPathsModule, validateGetStaticPathsResult } from './routing.js';
import { parseNpmName, canonicalURL as getCanonicalURL, codeFrame } from './util.js';
Expand Down

0 comments on commit 3c20e7b

Please sign in to comment.