From b375a33397e1908461679efdaeb94c24dab8905a Mon Sep 17 00:00:00 2001 From: octospacc Date: Wed, 27 Sep 2023 00:49:37 +0200 Subject: [PATCH] Babel-compile library, add core-js --- .gitignore | 3 ++ Spacc.Web.js | 118 ++++++++++++++++------------------------------ SpaccDotWeb.html | 32 +++++-------- babel.config.json | 16 +++++++ package-lock.json | 12 +++++ 5 files changed, 84 insertions(+), 97 deletions(-) create mode 100644 .gitignore create mode 100644 babel.config.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f3d7c83 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.tmp +Build/* +node_modules/* diff --git a/Spacc.Web.js b/Spacc.Web.js index f8a53c4..d8a4dd7 100644 --- a/Spacc.Web.js +++ b/Spacc.Web.js @@ -3,15 +3,17 @@ const SpaccDotWeb = ((args) => { ////////////////////////////////////////////// let windowObject, documentObject; let Lib = {}; -let isDomVirtual = false; +//let isDomVirtual = false; +let isBuildingApp = false; const platformIsNode = (typeof module === 'object' && typeof module.exports === 'object'); const platformIsBrowser = (typeof window !== 'undefined' && typeof window.document !== 'undefined'); if (platformIsNode) { Lib.fs = require('fs'); + Lib.childProcess = require('child_process'); Lib.jsdom = require('jsdom'); - isDomVirtual = true; + //isDomVirtual = true; windowObject = new Lib.jsdom.JSDOM().window; }; @@ -27,93 +29,67 @@ const SpaccDotWeb = ((args) => { ////////////////////////////////////////////// let SpaccDotWeb = {}; -SpaccDotWeb.AppBuildStandalone = (fileIndex) => { - fileIndex ||= 'index.html'; +if (platformIsNode) { + SpaccDotWeb.AppBuildStandalone = (fileIndex) => { + isBuildingApp = true; + fileIndex ||= 'index.html'; - isDomVirtual = true; + //isDomVirtual = true; - let htmlIndex = Lib.fs.readFileSync(fileIndex, 'utf8'); - //let domIndex = new Lib.jsdom.JSDOM(htmlIndex); + Lib.fs.mkdirSync(`${__dirname}/Build/${fileIndex}.tmp`, { recursive: true }); + let htmlIndex = Lib.fs.readFileSync(fileIndex, 'utf8'); - //let htmlFinal = DomMakeBase(AppMetaGet(domIndex)); - //let domFinal = new Lib.jsdom.JSDOM(htmlFinal); + windowObject = new Lib.jsdom.JSDOM(htmlIndex).window; + documentObject = windowObject.document; - //for (const script of domIndex.window.document.querySelectorAll('script[module]')) { - // domFinal.window.document.head.innerHTML += script.outerHTML; - //}; + DomSetup();//SpaccDotWeb.AppInit(); - //return domFinal.window.document.documentElement.outerHTML; - - /*windowDomIndex*/windowObject = new Lib.jsdom.JSDOM(htmlIndex).window; - documentObject = windowObject.document; - - //windowObject.document.documentElement = windowDomIndex.document.documentElement; - - //console.log(documentObject.documentElement.outerHTML); - - DomSetup();//SpaccDotWeb.AppInit(); - - return `${documentObject.documentElement.outerHTML}`; + isBuildingApp = false; + Lib.fs.writeFileSync(`${__dirname}/Build/${fileIndex}`, `${documentObject.documentElement.outerHTML}`); + }; }; -SpaccDotWeb.AppInit = () => { +SpaccDotWeb.AppInit = function AppInit(){ try { - DomSetup(AppMetaGet()); + DomSetup(); } catch(err) { console.log(err) } - //if (!isDomVirtual) { - // DomSetup(); - //}; }; -//SpaccDotWeb.DownloadFile = () => { -// -//}; - //SpaccDotWeb.Make = () => {}; SpaccDotWeb.Create = (tag, attrs) => { let elem = documentObject.createElement(tag); - // ... + for (const key in attrs) { + elem[key] = attrs[key]; + }; return elem; }; -SpaccDotWeb.Select = (query, dom) => { - //dom &&= dom.window.document; - //dom ||= document; - //return dom.querySelector(query); +SpaccDotWeb.Select = (query) => { let elem = documentObject.querySelector(query); elem && (elem.Insert = elem.appendChild); return elem; }; -AppMetaGet = (/*dom*/) => JSON.parse(SpaccDotWeb.Select('#Meta'/*, dom*/).innerHTML); +const AppMetaGet = () => JSON.parse(SpaccDotWeb.Select('#Meta').innerHTML); -DomMakeBase = (/*meta*/) => { +const DomMakeBase = () => { const meta = AppMetaGet(); + const htmlFrags = { Title: (meta.Name ? `${meta.Name}` : ''), Description: (meta.Description ? `` : ''), Uri: (meta.Uri ? `` : '') }; - //return ` - // - // - // - // - // ${htmlFrags.Title} - // ${htmlFrags.Description} - // ${htmlFrags.Uri} - // - // - // - //
- // - //`; let scripts = ''; - if (isDomVirtual) { - scripts += `${Lib.fs.readFileSync(__filename, 'utf8')}`; + if (isBuildingApp/*isDomVirtual*/) { + const SpaccMinified = Lib.childProcess.execSync(`cat "${__filename}" | npx babel -f "${__filename.split('/').slice(-1)[0]}"`); + scripts += ``; + scripts += `${SpaccMinified}`; + //scripts += ``; + //scripts += `${Lib.fs.readFileSync(__filename, 'utf8')}`; for (const elem of documentObject.querySelectorAll('script[module]')) { scripts += elem.outerHTML; }; @@ -121,47 +97,35 @@ DomMakeBase = (/*meta*/) => { return { head: ` - - - ${htmlFrags.Title} - ${htmlFrags.Description} - ${htmlFrags.Uri} + + + ${htmlFrags.Title} + ${htmlFrags.Description} + ${htmlFrags.Uri} + `, body: `
${scripts}`, }; }; -DomSetup = (/*meta*/) => { +const DomSetup = () => { const doctypeNew = documentObject.implementation.createHTMLDocument().doctype; windowObject.document.doctype ? documentObject.replaceChild(doctypeNew, documentObject.doctype) : documentObject.insertBefore(doctypeNew, documentObject.childNodes[0]); - //documentObject.documentElement.setHTML(DomMakeBase(data)); - //documentObject.documentElement.setHTML(``); - //documentObject.write(''); - //documentObject.write(DomMakeBase(data)); - //for (el of documentObject.documentElement.childNodes) { - // el.remove(); - //}; - //for (el of documentObject.documentElement.childNodes) { - // el.remove(); - //}; - //documentObject.documentElement.setHTML(''); - //documentObject.documentElement.innerHTML = ''; - //documentObject.write(''); - const domBase = DomMakeBase(/*meta*/); + const domBase = DomMakeBase(); documentObject.write(domBase.head + domBase.body); documentObject.head.innerHTML = domBase.head; documentObject.body.innerHTML = domBase.body; }; -platformIsBrowser && (window.SpaccDotWeb = SpaccDotWeb); return SpaccDotWeb; })(); ///////////////////////////////////////////////////////////////////////// +platformIsBrowser && (window.SpaccDotWeb = SpaccDotWeb); platformIsNode && (console.log(eval(process.argv.slice(-1)[0]))); return SpaccDotWeb; diff --git a/SpaccDotWeb.html b/SpaccDotWeb.html index 6c773c5..5a124ac 100644 --- a/SpaccDotWeb.html +++ b/SpaccDotWeb.html @@ -7,29 +7,21 @@ } diff --git a/babel.config.json b/babel.config.json new file mode 100644 index 0000000..a48a9b7 --- /dev/null +++ b/babel.config.json @@ -0,0 +1,16 @@ +{ + "presets": [ + [ + "@babel/preset-env", + { + "targets": { + "chrome": "4", + "edge": "12", + "firefox": "2", + "ie": "6", + "safari": "3.1" + } + } + ] + ] +} diff --git a/package-lock.json b/package-lock.json index 8599ed9..47e99ec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@babel/core": "^7.23.0", "@babel/preset-env": "^7.22.20", "@xmldom/xmldom": "^0.8.10", + "core-js": "^3.32.2", "jsdom": "^22.1.0" } }, @@ -2038,6 +2039,17 @@ "integrity": "sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==", "dev": true }, + "node_modules/core-js": { + "version": "3.32.2", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-3.32.2.tgz", + "integrity": "sha512-pxXSw1mYZPDGvTQqEc5vgIb83jGQKFGYWY76z4a7weZXUolw3G+OvpZqSRcfYOoOVUQJYEPsWeQK8pKEnUtWxQ==", + "dev": true, + "hasInstallScript": true, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/core-js" + } + }, "node_modules/core-js-compat": { "version": "3.32.2", "resolved": "https://registry.npmjs.org/core-js-compat/-/core-js-compat-3.32.2.tgz",