1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2025-07-01 12:22:15 +02:00

🎉 docs/p05-scopes

This commit is contained in:
2023-11-30 17:05:19 +01:00
parent 01f102c149
commit fb14d51cb4
13 changed files with 240 additions and 16 deletions

View File

@ -10,16 +10,19 @@ const pkg= s.cat("package.json").xargs(JSON.parse);
for(const info of pages){
const { id }= info;
echo(`Generating ${id}.html…`);
const ssr= createHTMl("");
const { el }= await register(ssr.dom);
const serverDOM= createHTMl("");
serverDOM.registerGlobally(
"HTMLScriptElement"
);
const { el }= await register(serverDOM.dom);
const { page }= await import(`../docs_src/${id}.html.js`); //→ TODO: important to mention in docs!!!
document.body.append(
serverDOM.document.body.append(
el(page, { pkg, info }),
);
echo.use("-R", `Writing ${id}.html…`);
dispatchEvent("oneachrender", document);
s.echo(ssr.serialize()).to(path_target.root+id+".html");
s.echo(serverDOM.serialize()).to(path_target.root+id+".html");
}
s.echo(styles.content).to(path_target.css+styles.name);
dispatchEvent("onssrend");

View File

@ -4,6 +4,7 @@ let keys= [];
let dom= null;
import { relative } from 'node:path';
import { writeFileSync } from 'node:fs';
/** @param {string} html */
export function createHTMl(html, options= {}){
if(!html) html= html_default;
if(dom) cleanHTML();
@ -12,23 +13,30 @@ export function createHTMl(html, options= {}){
dom= new JSDOM(html, options);
const window= dom.window;
if(!keys.length)
keys= Object.getOwnPropertyNames(window).filter((k) => !k.startsWith('_') && !(k in globalThis));
keys.forEach(key=> globalThis[key]= window[key]);
globalThis.document= window.document
globalThis.window= window
window.console= globalThis.console
return {
dom,
writeToFS({ html, css }){
if(css) this.style.writeToFile(css, relative(html, css).slice(1));
if(html) writeFileSync(html, dom.serialize());
},
serialize(){ return dom.serialize(); }
/** @param {string[]} [names] */
registerGlobally(...names){
keys.push(...names);
if(!keys.length)
keys= Object.getOwnPropertyNames(window).filter((k) => !k.startsWith('_') && !(k in globalThis));
keys.forEach(key=> globalThis[key]= window[key]);
globalThis.document= window.document
globalThis.window= window
window.console= globalThis.console
return this;
},
serialize(){ return dom.serialize(); },
window, document: window.document
};
}
export function cleanHTML(){
if(!dom) return;
keys.forEach(key=> delete globalThis[key]);
keys.length= 0;
dom= false;
}