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"
},
"devDependencies": {
"nitropack": "^2.9.2"
"nitropack": "2.8"
}
}

View file

@ -50,7 +50,7 @@ export function useMcFlyRoute({ config, storage }) {
}
function getPurePath(path) {
return path.split('?')[0]
return path.split("?")[0];
}
/**
@ -61,7 +61,8 @@ function getPurePath(path) {
*/
async function getHtml(path, storage) {
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 fallback = getPath(rawPath + "/index.html");
const filePath = getPath(filename);
@ -140,6 +141,8 @@ async function buildRegistry(usedCustomElements, type, storage) {
const evalStore = eval(
`class WebComponent {}; class HTMLElement {}; (${content.toString()})`
);
if (isConstructor(evalStore)) {
const className = new evalStore().constructor.name;
if (!classesImported.includes(className)) {
@ -158,12 +161,28 @@ async function buildRegistry(usedCustomElements, type, storage) {
classesImported.push(className);
}
}
}
registryScript += "</script>";
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
* @param {string} html
@ -207,14 +226,14 @@ function doSetUp(html) {
let [key, value] = match;
value = value.replace(/\s/g, "");
// nested objects
const keys = value.split('.');
let finalValue = '';
const keys = value.split(".");
let finalValue = "";
let setupCopy = setupMap;
keys.forEach(i => {
finalValue = setupCopy[i]
keys.forEach((i) => {
finalValue = setupCopy[i];
setupCopy = finalValue;
})
});
html = html.replace(key, finalValue);
}
@ -350,24 +369,26 @@ async function useFragments(html, storage) {
*/
function replaceSlots(fragmentNode, node) {
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) => {
if (n.type === ELEMENT_NODE && n.name === "slot") {
// find node child with same name attribute
const currentSlotName = n.attributes?.['name'] ?? null;
const currentSlotName = n.attributes?.["name"] ?? null;
let nodeChildren = [];
if (currentSlotName === null) {
nodeChildren = node.children.filter(child => !child.attributes?.['slot']);
nodeChildren = node.children.filter(
(child) => !child.attributes?.["slot"]
);
} else {
nodeChildren = node.children.filter(child => {
const childSlotName = child.attributes?.['slot'];
nodeChildren = node.children.filter((child) => {
const childSlotName = child.attributes?.["slot"];
return childSlotName === currentSlotName;
})
});
}
if (nodeChildren.length > 0 && !containsAll(slotted, nodeChildren)) {
slotted = [...slotted, ...nodeChildren]
slotted = [...slotted, ...nodeChildren];
const index = n.parent.children.indexOf(n);
n.parent.children.splice(index, 1, ...nodeChildren);
}

View file

@ -22,5 +22,8 @@
"esprima": "^4.0.1",
"h3": "^1.8.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/config": "workspace:*",
"@mcflyjs/core": "workspace:*",
"nitropack": "latest"
"nitropack": "2.8"
},
"version": "0.0.1",
"main": "index.js",

View file

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