dynamic bundling
This commit is contained in:
parent
e48f581b3c
commit
eb12efae50
@ -9,8 +9,7 @@
|
|||||||
{
|
{
|
||||||
"work": "deno run -Ar ./scripts/dev_server.ts",
|
"work": "deno run -Ar ./scripts/dev_server.ts",
|
||||||
"scan": "deno run -Ar ./scripts/refresh_types.ts",
|
"scan": "deno run -Ar ./scripts/refresh_types.ts",
|
||||||
"html": "deno run -Ar ./scripts/scaffold.ts --html",
|
"html": "deno run -Ar ./scripts/scaffold.ts --html"
|
||||||
"make": "deno bundle --no-lock --platform=browser --inline-imports=true --output=bundle.js --minify src/bundle_entry.ts"
|
|
||||||
},
|
},
|
||||||
"imports":
|
"imports":
|
||||||
{
|
{
|
||||||
|
24
dist/core.js
vendored
24
dist/core.js
vendored
File diff suppressed because one or more lines are too long
15
index.html
15
index.html
@ -1,15 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<!-- css reset --><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 src="./bundle.js"></script>
|
|
||||||
<script>
|
|
||||||
console.log(Gale);
|
|
||||||
console.log(van);
|
|
||||||
console.log(vanX);
|
|
||||||
</script>
|
|
||||||
</head>
|
|
||||||
<body></body>
|
|
||||||
</html>
|
|
@ -4,7 +4,11 @@ globalThis.van = Van;
|
|||||||
import * as VanX from "npm:vanjs-ext@^0.6.3";
|
import * as VanX from "npm:vanjs-ext@^0.6.3";
|
||||||
globalThis.vanX = VanX;
|
globalThis.vanX = VanX;
|
||||||
|
|
||||||
import Gale from "./gale.js";
|
import Gale from "../src/gale.js";
|
||||||
globalThis.Gale = Gale;
|
globalThis.Gale = Gale;
|
||||||
|
|
||||||
import "./hmr.js";
|
import BindHMR from "../src/hmr.js";
|
||||||
|
BindHMR();
|
||||||
|
|
||||||
|
import Boot from "../src/boot.js";
|
||||||
|
Boot();
|
@ -1,8 +1,12 @@
|
|||||||
const command = new Deno.Command("deno bundle", {});
|
const command = new Deno.Command("deno", {
|
||||||
const decode = new TextDecoder("utf-8");
|
args:[
|
||||||
|
"bundle",
|
||||||
const output = await command.output();
|
"--no-lock",
|
||||||
|
"--platform=browser",
|
||||||
|
"--inline-imports=true",
|
||||||
|
"--output=dist/core.js",
|
||||||
console.assert()
|
"--minify",
|
||||||
|
"scripts/bundle_entry.ts"
|
||||||
|
]
|
||||||
|
});
|
||||||
|
command.outputSync();
|
@ -4,7 +4,7 @@ import {HTML} from "./assemble_files.ts";
|
|||||||
// Parse the port from the command-line arguments, defaulting to 8000
|
// Parse the port from the command-line arguments, defaulting to 8000
|
||||||
const port = parseInt(Deno.args[0] || "8000", 10);
|
const port = parseInt(Deno.args[0] || "8000", 10);
|
||||||
const sockets: WebSocket[] = [];
|
const sockets: WebSocket[] = [];
|
||||||
const devinc = await fetch(import.meta.resolve("../src/dev.js")).then(r=>r.text());
|
//const devinc = await fetch(import.meta.resolve("../src/dev.js")).then(r=>r.text());
|
||||||
|
|
||||||
let html: string;
|
let html: string;
|
||||||
try {
|
try {
|
||||||
@ -12,7 +12,7 @@ try {
|
|||||||
} catch (_) {
|
} catch (_) {
|
||||||
html = HTML;
|
html = HTML;
|
||||||
}
|
}
|
||||||
html = html.replace("</head>", `<script>${devinc}</script></head>`);
|
//html = html.replace("</head>", `<script>${devinc}</script></head>`);
|
||||||
|
|
||||||
function extension(path: string): string {
|
function extension(path: string): string {
|
||||||
// Remove trailing slash if it exists
|
// Remove trailing slash if it exists
|
||||||
|
28
src/boot.js
28
src/boot.js
@ -1,15 +1,13 @@
|
|||||||
(
|
export default (root="/")=>fetch(root+"deno.json")
|
||||||
(root="/")=>fetch(root+"deno.json")
|
.then(text=>text.json())
|
||||||
.then(text=>text.json())
|
.then(json=>{
|
||||||
.then(json=>{
|
const n=(t,e)=>{let n=document.createElement("script");n.type=t,n.textContent=e,document.head.appendChild(n)};
|
||||||
const n=(t,e)=>{let n=document.createElement("script");n.type=t,n.textContent=e,document.head.appendChild(n)};
|
const imports = json.imports;
|
||||||
const imports = json.imports;
|
for(let n in imports)
|
||||||
for(let n in imports)
|
{
|
||||||
{
|
const path=imports[n];
|
||||||
const path=imports[n];
|
path.startsWith("./")&&(imports[n]=root+path.substring(2))
|
||||||
path.startsWith("./")&&(imports[n]=root+path.substring(2))
|
}
|
||||||
}
|
n("importmap",JSON.stringify({imports})),
|
||||||
n("importmap",JSON.stringify({imports})),
|
n("module",'import "entry"; ')
|
||||||
n("module",'import "entry"; ')
|
})
|
||||||
})
|
|
||||||
)();
|
|
33
src/dev.js
33
src/dev.js
@ -1,33 +0,0 @@
|
|||||||
// 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;
|
|
||||||
}
|
|
195
src/hmr.js
195
src/hmr.js
@ -1,90 +1,121 @@
|
|||||||
export default {
|
|
||||||
Time: 0,
|
let Time = 0;
|
||||||
/** @type {Record<string, string>} */
|
/** @type {Record<string, string>} */
|
||||||
Temp:{},
|
let Temp = {};
|
||||||
Tick()
|
function Tick()
|
||||||
|
{
|
||||||
|
for(const k in Temp)
|
||||||
{
|
{
|
||||||
for(const k in HMR.Temp)
|
sessionStorage.setItem(k, Temp[k]);
|
||||||
|
}
|
||||||
|
Temp = {};
|
||||||
|
Time = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @type {(key:string, value:string)=>void} */
|
||||||
|
function Save(key, value)
|
||||||
|
{
|
||||||
|
Temp[key] = value;
|
||||||
|
if(!Time)
|
||||||
|
{
|
||||||
|
Time = setTimeout(Tick, 500);
|
||||||
|
}
|
||||||
|
console.log("SAVE", key, value);
|
||||||
|
};
|
||||||
|
|
||||||
|
/** @type {(key:string)=>string|null} */
|
||||||
|
function Load(key)
|
||||||
|
{
|
||||||
|
const value = sessionStorage.getItem(key);
|
||||||
|
console.log("LOAD", key, value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @type {string|undefined} */
|
||||||
|
let _ID = undefined;
|
||||||
|
let _index = 0;
|
||||||
|
|
||||||
|
/** @type {(id:string|undefined = undefined)=>void} */
|
||||||
|
function StartID(id)
|
||||||
|
{
|
||||||
|
_index = 0;
|
||||||
|
_ID = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
function NextID()
|
||||||
|
{
|
||||||
|
return _ID ? _ID + "_" + (_index++) + "_" : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
export default()=>{
|
||||||
|
//bind Van
|
||||||
|
const origninalState = globalThis.van.state;
|
||||||
|
globalThis.van.state =(value, key="")=>
|
||||||
|
{
|
||||||
|
const type = typeof value;
|
||||||
|
let reader =d=>d;
|
||||||
|
let writer =d=>d?.toString() || null;
|
||||||
|
|
||||||
|
switch(type)
|
||||||
{
|
{
|
||||||
sessionStorage.setItem(k, HMR.Temp[k]);
|
case "boolean" :
|
||||||
|
reader =(data)=> data === "true"; break;
|
||||||
|
case "number" :
|
||||||
|
reader = parseFloat; break;
|
||||||
|
case "object" :
|
||||||
|
reader = JSON.parse;
|
||||||
|
writer = JSON.stringify;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
HMR.Temp = {};
|
|
||||||
HMR.Time = 0;
|
|
||||||
},
|
|
||||||
/** @type {(key:string, value:string)=>void} */
|
|
||||||
Save(key, value)
|
|
||||||
{
|
|
||||||
this.Temp[key] = value;
|
|
||||||
if(!this.Time)
|
|
||||||
{
|
|
||||||
this.Time = setTimeout(this.Tick, 500);
|
|
||||||
}
|
|
||||||
console.log("SAVE", key, value);
|
|
||||||
},
|
|
||||||
/** @type {(key:string)=>string|null} */
|
|
||||||
Load(key)
|
|
||||||
{
|
|
||||||
const value = sessionStorage.getItem(key);
|
|
||||||
console.log("LOAD", key, value);
|
|
||||||
return value;
|
|
||||||
},
|
|
||||||
|
|
||||||
/** @type {string|undefined} */
|
const fullKey = "HMR_" + NextID() + key;
|
||||||
_ID: undefined,
|
const stringValue = Load(fullKey);
|
||||||
_index: 0,
|
const signal = origninalState((stringValue ? reader(stringValue) : value));
|
||||||
/** @type {(id:string|undefined = undefined)=>void} */
|
van.derive(()=>Save(fullKey, writer(signal.val)));
|
||||||
StartID(id)
|
|
||||||
|
return signal;
|
||||||
|
};
|
||||||
|
|
||||||
|
//bind VanX
|
||||||
|
const originalReactive = globalThis.vanX.reactive;
|
||||||
|
globalThis.vanX.reactive =(obj, id)=>
|
||||||
{
|
{
|
||||||
this._index = 0;
|
StartID(id);
|
||||||
this._ID = id;
|
const state = originalReactive(obj);
|
||||||
},
|
StartID();
|
||||||
NextID()
|
return state;
|
||||||
{
|
|
||||||
return this._ID ? this._ID + "_" + (this._index++) + "_" : "";
|
|
||||||
},
|
|
||||||
|
|
||||||
BindVan()
|
|
||||||
{
|
|
||||||
//bind Van
|
|
||||||
const origninalState = globalThis.van.state;
|
|
||||||
globalThis.van.state =(value, key="")=>
|
|
||||||
{
|
|
||||||
const type = typeof value;
|
|
||||||
let reader =d=>d;
|
|
||||||
let writer =d=>d?.toString() || null;
|
|
||||||
|
|
||||||
switch(type)
|
|
||||||
{
|
|
||||||
case "boolean" :
|
|
||||||
reader =(data)=> data === "true"; break;
|
|
||||||
case "number" :
|
|
||||||
reader = parseFloat; break;
|
|
||||||
case "object" :
|
|
||||||
reader = JSON.parse;
|
|
||||||
writer = JSON.stringify;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
const fullKey = "HMR_" + HMR.NextID() + key;
|
|
||||||
const stringValue = HMR.Load(fullKey);
|
|
||||||
const signal = origninalState((stringValue ? reader(stringValue) : value));
|
|
||||||
van.derive(()=>HMR.Save(fullKey, writer(signal.val)));
|
|
||||||
|
|
||||||
return signal;
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
BindVanX()
|
|
||||||
{
|
|
||||||
//bind VanX
|
|
||||||
const originalReactive = globalThis.vanX.reactive;
|
|
||||||
globalThis.vanX.reactive =(obj, id)=>
|
|
||||||
{
|
|
||||||
HMR.StartID(id);
|
|
||||||
const state = originalReactive(obj);
|
|
||||||
HMR.StartID();
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user