-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwcc.js
166 lines (130 loc) · 4.58 KB
/
wcc.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// this must come first
import './dom-shim.js';
import * as acorn from 'acorn';
import * as walk from 'acorn-walk';
import { parse, parseFragment, serialize } from 'parse5';
import fs from 'node:fs/promises';
let definitions;
function getParse(html) {
return html.indexOf('<html>') >= 0 || html.indexOf('<body>') >= 0 || html.indexOf('<head>') >= 0
? parse
: parseFragment;
}
function isCustomElementDefinitionNode(node) {
const { expression } = node;
return expression.type === 'CallExpression' && expression.callee && expression.callee.object
&& expression.callee.property && expression.callee.object.name === 'customElements'
&& expression.callee.property.name === 'define';
}
async function renderComponentRoots(tree, includeShadowRoots = true) {
for (const node of tree.childNodes) {
if (node.tagName && node.tagName.indexOf('-') > 0) {
const { tagName } = node;
const { moduleURL } = definitions[tagName];
const elementInstance = await initializeCustomElement(moduleURL, tagName, node.attrs);
const shadowRootHtml = elementInstance.getInnerHTML({ includeShadowRoots });
const shadowRootTree = parseFragment(shadowRootHtml);
node.childNodes = node.childNodes.length === 0 ? shadowRootTree.childNodes : [...shadowRootTree.childNodes, ...node.childNodes];
}
if (node.childNodes && node.childNodes.length > 0) {
await renderComponentRoots(node, includeShadowRoots);
}
// does this only apply to `<template>` tags?
if (node.content && node.content.childNodes && node.content.childNodes.length > 0) {
await renderComponentRoots(node.content, includeShadowRoots);
}
}
return tree;
}
async function registerDependencies(moduleURL) {
const moduleContents = await fs.readFile(moduleURL, 'utf-8');
walk.simple(acorn.parse(moduleContents, {
ecmaVersion: 'latest',
sourceType: 'module'
}), {
async ImportDeclaration(node) {
const dependencyModuleURL = new URL(node.source.value, moduleURL);
await registerDependencies(dependencyModuleURL);
},
async ExpressionStatement(node) {
if (isCustomElementDefinitionNode(node)) {
const { arguments: args } = node.expression;
const tagName = args[0].value;
definitions[tagName] = {
instanceName: args[1].name,
moduleURL
};
}
}
});
}
async function getTagName(moduleURL) {
const moduleContents = await fs.readFile(moduleURL, 'utf-8');
let tagName;
walk.simple(acorn.parse(moduleContents, {
ecmaVersion: 'latest',
sourceType: 'module'
}), {
async ExpressionStatement(node) {
if (isCustomElementDefinitionNode(node)) {
tagName = node.expression.arguments[0].value;
}
}
});
return tagName;
}
async function initializeCustomElement(elementURL, tagName, attrs = []) {
await registerDependencies(elementURL);
const element = tagName
? customElements.get(tagName)
: (await import(elementURL)).default;
const dataLoader = (await import(elementURL)).getData;
const data = dataLoader ? await dataLoader() : {};
const elementInstance = new element(data); // eslint-disable-line new-cap
attrs.forEach((attr) => {
elementInstance.setAttribute(attr.name, attr.value);
if (attr.name === 'hydrate') {
definitions[tagName].hydrate = attr.value;
}
});
await elementInstance.connectedCallback();
return elementInstance;
}
async function renderToString(elementURL, options = {}) {
definitions = [];
const { lightMode = false } = options;
const includeShadowRoots = !lightMode;
const elementTagName = await getTagName(elementURL);
const elementInstance = await initializeCustomElement(elementURL);
const elementHtml = elementInstance.getInnerHTML({ includeShadowRoots });
const elementTree = getParse(elementHtml)(elementHtml);
const finalTree = await renderComponentRoots(elementTree, includeShadowRoots);
const html = !lightMode && elementTagName ? `
<${elementTagName}>
${serialize(finalTree)}
</${elementTagName}>
`
: serialize(finalTree);
return {
html,
metadata: definitions
};
}
async function renderFromHTML(html, elements = [], options = {}) {
definitions = [];
const { lightMode = false } = options;
const includeShadowRoots = !lightMode;
for (const url of elements) {
await initializeCustomElement(url);
}
const elementTree = getParse(html)(html);
const finalTree = await renderComponentRoots(elementTree, includeShadowRoots);
return {
html: serialize(finalTree),
metadata: definitions
};
}
export {
renderToString,
renderFromHTML
};