-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
101 lines (86 loc) · 2.36 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//import { h, Component as BaseComponent } from 'preact';
//import { renderToString } from 'preact-render-to-string';
//import StaticHtml from './static-html.js';
import { render } from '@lit-labs/ssr/lib/render-with-global-dom-shim.js';
function isCustomElementTag(name) {
return typeof name === 'string' && /-/.test(name);
}
function getCustomElementConstructor(name) {
if(typeof customElements !== 'undefined' && isCustomElementTag(name)) {
return customElements.get(name) || null;
}
if(typeof name === 'function') {
return name;
}
return null;
}
async function isLitElement(Component) {
const { LitElement } = await import('lit');
const Ctr = getCustomElementConstructor(Component);
if(Ctr && LitElement.isPrototypeOf(Ctr)) {
return true;
}
return false;
}
async function check(Component, _props, _children) {
if(await isLitElement(Component)) {
return true;
}
// TODO other checks (Haunted?)
return false;
}
function compileTemplate(tag, props) {
const template = [`<${tag} `];
const values = [];
for(const [key, value] of Object.entries(props)) {
let lastEntry = template[template.length - 1];
lastEntry += `${key}=`;
template[template.length - 1] = lastEntry;
values.push(value);
template.push('');
}
template.pop();
template.push(`></${tag}>`);
Object.defineProperty(template, 'raw', {
enumerable: false,
writable: false,
value: Array.from(template)
});
return template;
}
const templateCache = new Map();
async function getTemplate(tag, props) {
const key = tag + '-' + Object.keys(props).join('-');
if(templateCache.has(key)) {
return templateCache.get(key);
}
const template = compileTemplate(tag, props);
templateCache.set(key, template);
return template;
}
async function renderToStaticMarkup(Component, props, children) {
// TODO caching templates
const { html } = await import('lit');
const template = compileTemplate(Component, props);
const result = render(html(template, ...values));
let out = '';
for(let chunk of result) {
out += chunk;
}
return {
html: out
};
}
function getComponentInfo(Component, _props, _children, options = {}) {
return {
url: `/_astro/src/components/Test.js`,
export: 'default'
};
}
const hydrationMethod = 'self';
export default {
check,
renderToStaticMarkup,
getComponentInfo,
hydrationMethod,
};