fix: temporarily pin to nitropack@2.8 for evalStore not a constructor err (#36)

* fix: check if evalStore is constructor

* deps: pin nitro to 2.8

* chore: remove console.log
This commit is contained in:
Ayo Ayco 2024-03-11 12:51:32 +01:00 committed by GitHub
parent ebb91c709d
commit 61e10e701a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 271 additions and 505 deletions

View file

@ -23,6 +23,6 @@
"web-component-base": "^2.0.6" "web-component-base": "^2.0.6"
}, },
"devDependencies": { "devDependencies": {
"nitropack": "^2.9.2" "nitropack": "2.8"
} }
} }

View file

@ -50,7 +50,7 @@ export function useMcFlyRoute({ config, storage }) {
} }
function getPurePath(path) { function getPurePath(path) {
return path.split('?')[0] return path.split("?")[0];
} }
/** /**
@ -61,7 +61,8 @@ function getPurePath(path) {
*/ */
async function getHtml(path, storage) { async function getHtml(path, storage) {
const purePath = getPurePath(path); const purePath = getPurePath(path);
const rawPath = purePath[purePath.length - 1] === "/" ? purePath.slice(0, -1) : purePath; const rawPath =
purePath[purePath.length - 1] === "/" ? purePath.slice(0, -1) : purePath;
const filename = rawPath === "" ? "/index.html" : `${rawPath}.html`; const filename = rawPath === "" ? "/index.html" : `${rawPath}.html`;
const fallback = getPath(rawPath + "/index.html"); const fallback = getPath(rawPath + "/index.html");
const filePath = getPath(filename); const filePath = getPath(filename);
@ -140,22 +141,25 @@ async function buildRegistry(usedCustomElements, type, storage) {
const evalStore = eval( const evalStore = eval(
`class WebComponent {}; class HTMLElement {}; (${content.toString()})` `class WebComponent {}; class HTMLElement {}; (${content.toString()})`
); );
const className = new evalStore().constructor.name;
if (!classesImported.includes(className)) { if (isConstructor(evalStore)) {
if ( const className = new evalStore().constructor.name;
!isBaseClassImported &&
content.toString().includes("extends WebComponent") if (!classesImported.includes(className)) {
) { if (
const baseClassImport = `import { WebComponent, html, attachEffect } from "https://unpkg.com/web-component-base@2.0.6/index.js";`; !isBaseClassImported &&
registryScript += baseClassImport; content.toString().includes("extends WebComponent")
isBaseClassImported = true; ) {
const baseClassImport = `import { WebComponent, html, attachEffect } from "https://unpkg.com/web-component-base@2.0.6/index.js";`;
registryScript += baseClassImport;
isBaseClassImported = true;
}
registryScript += content;
registryScript += `customElements.define("${name}", ${className});`;
classesImported.push(className);
} }
registryScript += content;
registryScript += `customElements.define("${name}", ${className});`;
classesImported.push(className);
} }
} }
@ -164,6 +168,21 @@ async function buildRegistry(usedCustomElements, type, storage) {
return registryScript; return registryScript;
} }
/**
* Check if function is a constructor
* @param {function} f
* @returns boolean
*/
function isConstructor(f) {
try {
new f();
} catch (err) {
// verify err is the expected error and then
return false;
}
return true;
}
/** /**
* Evaluates server:setup script and replaces all variables used in the HTML * Evaluates server:setup script and replaces all variables used in the HTML
* @param {string} html * @param {string} html
@ -207,14 +226,14 @@ function doSetUp(html) {
let [key, value] = match; let [key, value] = match;
value = value.replace(/\s/g, ""); value = value.replace(/\s/g, "");
// nested objects // nested objects
const keys = value.split('.'); const keys = value.split(".");
let finalValue = ''; let finalValue = "";
let setupCopy = setupMap; let setupCopy = setupMap;
keys.forEach(i => { keys.forEach((i) => {
finalValue = setupCopy[i] finalValue = setupCopy[i];
setupCopy = finalValue; setupCopy = finalValue;
}) });
html = html.replace(key, finalValue); html = html.replace(key, finalValue);
} }
@ -350,24 +369,26 @@ async function useFragments(html, storage) {
*/ */
function replaceSlots(fragmentNode, node) { function replaceSlots(fragmentNode, node) {
let slotted = []; let slotted = [];
const containsAll = (arr, target) => target.every(v => arr.includes(v)); const containsAll = (arr, target) => target.every((v) => arr.includes(v));
walkSync(fragmentNode, (n) => { walkSync(fragmentNode, (n) => {
if (n.type === ELEMENT_NODE && n.name === "slot") { if (n.type === ELEMENT_NODE && n.name === "slot") {
// find node child with same name attribute // find node child with same name attribute
const currentSlotName = n.attributes?.['name'] ?? null; const currentSlotName = n.attributes?.["name"] ?? null;
let nodeChildren = []; let nodeChildren = [];
if (currentSlotName === null) { if (currentSlotName === null) {
nodeChildren = node.children.filter(child => !child.attributes?.['slot']); nodeChildren = node.children.filter(
(child) => !child.attributes?.["slot"]
);
} else { } else {
nodeChildren = node.children.filter(child => { nodeChildren = node.children.filter((child) => {
const childSlotName = child.attributes?.['slot']; const childSlotName = child.attributes?.["slot"];
return childSlotName === currentSlotName; return childSlotName === currentSlotName;
}) });
} }
if (nodeChildren.length > 0 && !containsAll(slotted, nodeChildren)) { if (nodeChildren.length > 0 && !containsAll(slotted, nodeChildren)) {
slotted = [...slotted, ...nodeChildren] slotted = [...slotted, ...nodeChildren];
const index = n.parent.children.indexOf(n); const index = n.parent.children.indexOf(n);
n.parent.children.splice(index, 1, ...nodeChildren); n.parent.children.splice(index, 1, ...nodeChildren);
} }

View file

@ -22,5 +22,8 @@
"esprima": "^4.0.1", "esprima": "^4.0.1",
"h3": "^1.8.2", "h3": "^1.8.2",
"ultrahtml": "^1.5.2" "ultrahtml": "^1.5.2"
},
"devDependencies": {
"unstorage": "^1.10.1"
} }
} }

File diff suppressed because it is too large Load diff

View file

@ -14,7 +14,7 @@
"@mcflyjs/cli": "workspace:*", "@mcflyjs/cli": "workspace:*",
"@mcflyjs/config": "workspace:*", "@mcflyjs/config": "workspace:*",
"@mcflyjs/core": "workspace:*", "@mcflyjs/core": "workspace:*",
"nitropack": "latest" "nitropack": "2.8"
}, },
"version": "0.0.1", "version": "0.0.1",
"main": "index.js", "main": "index.js",

View file

@ -13,6 +13,6 @@
"@mcflyjs/cli": "latest", "@mcflyjs/cli": "latest",
"@mcflyjs/config": "latest", "@mcflyjs/config": "latest",
"@mcflyjs/core": "latest", "@mcflyjs/core": "latest",
"nitropack": "latest" "nitropack": "2.8"
} }
} }