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,13 +39,22 @@ function insertRegistry(html: string): string {
const ast = parse(html); const ast = parse(html);
// insert registry script to head let hasCustomElements = false;
walkSync(ast, (node) => { walkSync(ast, (node) => {
if (node.type === ELEMENT_NODE && node.name === "head") { if (node.type === ELEMENT_NODE && node.name.includes("-")) {
node.children.push(parse(registryScript)); hasCustomElements = true;
} }
}); });
// insert registry script to head
if (hasCustomElements)
walkSync(ast, (node) => {
if (node.type === ELEMENT_NODE && node.name === "head") {
node.children.push(parse(registryScript));
}
});
return renderSync(ast); return renderSync(ast);
} }
@ -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>
const homeLinkLabel = "Home"; <html lang="en">
let greeting = "hello!"; <head>
</script> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>not found</title>
<a href="/">{{homeLinkLabel}}</a> <script server:setup>
const homeLinkLabel = "Home";
let greeting = "hello!";
</script>
</head>
<body>
<a href="./">{{homeLinkLabel}}</a>
{{greeting }} wow we can't find that {{greeting }} wow we can't find that
</body>
</html>