feat: don't register components if no custom-element in page

This commit is contained in:
Ayo 2023-10-10 23:27:58 +02:00
parent b286fe81be
commit f420c9fe2b
2 changed files with 32 additions and 13 deletions

View file

@ -39,7 +39,16 @@ function insertRegistry(html: string): string {
const ast = parse(html); const ast = parse(html);
let hasCustomElements = false;
walkSync(ast, (node) => {
if (node.type === ELEMENT_NODE && node.name.includes("-")) {
hasCustomElements = true;
}
});
// insert registry script to head // insert registry script to head
if (hasCustomElements)
walkSync(ast, (node) => { walkSync(ast, (node) => {
if (node.type === ELEMENT_NODE && node.name === "head") { if (node.type === ELEMENT_NODE && node.name === "head") {
node.children.push(parse(registryScript)); node.children.push(parse(registryScript));
@ -82,10 +91,10 @@ function doSetUp(html: string) {
const regex = /{{(.*?)}}/g; const regex = /{{(.*?)}}/g;
var match; var match;
let matches = []; while ((match = regex.exec(html))) {
while ((match = regex.exec(html)) != null) { let [key, value] = match;
console.log(match[0], match[1]); value = value.replace(/\s/g, "");
html = html.replace(match[0], setupMap[match[1].trim()]); html = html.replace(key, setupMap[value]);
} }
console.log("---------"); console.log("---------");

View file

@ -1,8 +1,18 @@
<script server:setup> <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>not found</title>
<script server:setup>
const homeLinkLabel = "Home"; const homeLinkLabel = "Home";
let greeting = "hello!"; let greeting = "hello!";
</script> </script>
</head>
<body>
<a href="./">{{homeLinkLabel}}</a>
<a href="/">{{homeLinkLabel}}</a> {{greeting }} wow we can't find that
</body>
{{greeting }} wow we can't find that </html>