deparate dev code include

This commit is contained in:
Seth Trowbridge 2025-02-23 21:30:44 -05:00
parent 68fe43b981
commit d9a750f7f0
2 changed files with 35 additions and 13 deletions

View File

@ -3,32 +3,22 @@ import { contentType } from "jsr:@std/media-types";
// Parse the port from the command-line arguments, defaulting to 8000
const port = parseInt(Deno.args[0] || "8000", 10);
const sockets: WebSocket[] = [];
const connect = `<script>
const ws = new WebSocket('ws://localhost:${port}/ws');
ws.addEventListener('message', (event) => {
if (event.data === 'reload') {
window.location.reload();
}
});
ws.addEventListener('error', console.error);
ws.addEventListener('close', console.warn);
</script>`;
const bundle = await fetch(import.meta.resolve("./bundle.js")).then(r=>r.text());
const devinc = await fetch(import.meta.resolve("./src/dev.js")).then(r=>r.text());
let html: string;
try {
html = Deno.readTextFileSync("./index.html").replace("<head>", "<head>"+connect);
html = Deno.readTextFileSync("./index.html").replace("</head>", `<script>${devinc}</script></head>`);
} catch (_) {
html = `
<!DOCTYPE html>
<html lang="en">
<head>
${connect}
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style> * {margin: 0;padding: 0;box-sizing: border-box;}html, body {height: 100%;width: 100%;font-family: Arial, sans-serif;line-height: 1.6;}body {-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;}img, video {max-width: 100%;height: auto;}a {text-decoration: none;color: inherit;}ul, ol {list-style: none;}button, input, textarea {font-family: inherit;font-size: inherit;line-height: inherit;border: none;background: none;padding: 0;margin: 0;outline: none;}table {border-collapse: collapse;width: 100%;}</style>
<script>${bundle}</script>
<script>${devinc}</script>
</head>
<body></body>
</html>`;

32
src/dev.js Normal file
View File

@ -0,0 +1,32 @@
// added in devmode to index.html
new WebSocket('ws://'+window.location.host+'/ws').addEventListener('message',e=>e.data==='reload'&&window.location.reload());
vanX.Store =(obj, key)=>
{
let checkInit = JSON.stringify(obj);
let checkStore = localStorage.getItem(key+"check");
localStorage.setItem(key+"check", checkInit);
let recallJSON;
if(checkInit == checkStore)
{
let recallText = localStorage.getItem(key);
try
{
recallJSON = JSON.parse(recallText) || obj;
}
catch(e)
{
recallJSON = obj;
}
}
else
{
recallJSON = obj;
}
const store = vanX.reactive( recallJSON );
van.derive(() => localStorage.setItem(key, JSON.stringify(vanX.compact(store))));
return store;
}