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

View file

@ -1,8 +1,18 @@
<script server:setup>
const homeLinkLabel = "Home";
let greeting = "hello!";
</script>
<!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>
<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>