refactor: registry insertion

This commit is contained in:
Ayo 2023-10-10 23:15:16 +02:00
parent b87f7ee5dc
commit b286fe81be
3 changed files with 19 additions and 4 deletions

View file

@ -14,7 +14,7 @@ export default eventHandler(async (event) => {
if (!html) html = await useStorage().getItem(getPath("/404.html")); if (!html) html = await useStorage().getItem(getPath("/404.html"));
// transforms // transforms
const transforms = [insertRegistry, doSetUp, deleteServerScripts]; const transforms = [doSetUp, deleteServerScripts, insertRegistry];
if (html) { if (html) {
for (const transform of transforms) { for (const transform of transforms) {
html = transform(html.toString()); html = transform(html.toString());
@ -37,7 +37,16 @@ function insertRegistry(html: string): string {
const registryScript = const registryScript =
'<script type="module" src="./.output/registry.js"></script>'; '<script type="module" src="./.output/registry.js"></script>';
return html.toString().replace("</head>", registryScript + "</head>"); const ast = parse(html);
// insert registry script to head
walkSync(ast, (node) => {
if (node.type === ELEMENT_NODE && node.name === "head") {
node.children.push(parse(registryScript));
}
});
return renderSync(ast);
} }
function doSetUp(html: string) { function doSetUp(html: string) {
@ -78,6 +87,7 @@ function doSetUp(html: string) {
console.log(match[0], match[1]); console.log(match[0], match[1]);
html = html.replace(match[0], setupMap[match[1].trim()]); html = html.replace(match[0], setupMap[match[1].trim()]);
} }
console.log("---------");
return html; return html;
} }

View file

@ -1,4 +1,8 @@
<script server:setup> <script server:setup>
let greeting = "hello"; const homeLinkLabel = "Home";
let greeting = "hello!";
</script> </script>
{{greeting}}!! wow we can't find that
<a href="/">{{homeLinkLabel}}</a>
{{greeting }} wow we can't find that

View file

@ -16,6 +16,7 @@
</head> </head>
<body> <body>
<a href="/about">{{ count }}</a> <a href="/about">{{ count }}</a>
{{greeting}} hey
<div> <div>
<hello-world name="{{name }}"></hello-world> <hello-world name="{{name }}"></hello-world>
</div> </div>