initial mcfly config and packages
This commit is contained in:
parent
e01ae51be3
commit
cef1ef3057
8 changed files with 57 additions and 31 deletions
6
mcfly.config.ts
Normal file
6
mcfly.config.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
import components from "./packages/custom-elements";
|
||||||
|
import defineConfig from "./packages/define-config";
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
integrations: [components()],
|
||||||
|
});
|
2
package-lock.json
generated
2
package-lock.json
generated
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"name": "nitro-web-components",
|
"name": "@ayoayco/nitro-web-components",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
|
|
|
@ -1,6 +1,24 @@
|
||||||
import { mkdirSync, writeFileSync, existsSync } from "node:fs";
|
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
||||||
|
|
||||||
export default defineNitroPlugin(async () => {
|
export default function components() {
|
||||||
|
return () => {
|
||||||
|
copyComponents();
|
||||||
|
registerComponents();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const copyComponents = async () => {
|
||||||
|
console.log("Copying components to public folder...");
|
||||||
|
const rawKeys = await useStorage().getKeys("assets:components");
|
||||||
|
rawKeys.forEach(async (key) => {
|
||||||
|
const cleanKey = key.replace("assets:components:", "");
|
||||||
|
const content = await useStorage().getItem(key);
|
||||||
|
if (!existsSync("./public/.output")) mkdirSync("./public/.output");
|
||||||
|
writeFileSync(`./public/.output/${cleanKey}`, content.toString());
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const registerComponents = async () => {
|
||||||
console.log("Building registry of custom elements...");
|
console.log("Building registry of custom elements...");
|
||||||
const rawKeys = await useStorage().getKeys("/assets/components");
|
const rawKeys = await useStorage().getKeys("/assets/components");
|
||||||
const keys = rawKeys.map((key) => key.replace("assets:components:", ""));
|
const keys = rawKeys.map((key) => key.replace("assets:components:", ""));
|
||||||
|
@ -26,4 +44,4 @@ export default defineNitroPlugin(async () => {
|
||||||
"./public/.output/registry.js",
|
"./public/.output/registry.js",
|
||||||
[...imports, registryObject, customElementsDefine].join(";")
|
[...imports, registryObject, customElementsDefine].join(";")
|
||||||
);
|
);
|
||||||
});
|
};
|
6
packages/define-config.ts
Normal file
6
packages/define-config.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
export type McFlyConfig = {
|
||||||
|
integrations?: Array<() => void>;
|
||||||
|
};
|
||||||
|
export default function defineConfig(config: McFlyConfig) {
|
||||||
|
return () => config;
|
||||||
|
}
|
|
@ -1,12 +0,0 @@
|
||||||
import { mkdirSync, writeFileSync, existsSync } from "node:fs";
|
|
||||||
|
|
||||||
export default defineNitroPlugin(async () => {
|
|
||||||
console.log("Copying components to public folder...");
|
|
||||||
const rawKeys = await useStorage().getKeys("assets:components");
|
|
||||||
rawKeys.forEach(async (key) => {
|
|
||||||
const cleanKey = key.replace("assets:components:", "");
|
|
||||||
const content = await useStorage().getItem(key);
|
|
||||||
if (!existsSync("./public/.output")) mkdirSync("./public/.output");
|
|
||||||
writeFileSync(`./public/.output/${cleanKey}`, content.toString());
|
|
||||||
});
|
|
||||||
});
|
|
9
plugins/_mcfly-plugin.ts
Normal file
9
plugins/_mcfly-plugin.ts
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
import config from "../mcfly.config";
|
||||||
|
|
||||||
|
export default defineNitroPlugin(() => {
|
||||||
|
const { integrations } = config();
|
||||||
|
if (integrations?.length > 0)
|
||||||
|
integrations.forEach((integration) => {
|
||||||
|
integration();
|
||||||
|
});
|
||||||
|
});
|
|
@ -2,16 +2,8 @@ import { ELEMENT_NODE, parse, renderSync, walkSync } from "ultrahtml";
|
||||||
import { parseScript } from "esprima";
|
import { parseScript } from "esprima";
|
||||||
|
|
||||||
export default eventHandler(async (event) => {
|
export default eventHandler(async (event) => {
|
||||||
const rawPath =
|
const { path } = event;
|
||||||
event.path[event.path.length - 1] === "/"
|
let html = await getHtml(path);
|
||||||
? event.path.slice(0, -1)
|
|
||||||
: event.path;
|
|
||||||
const filename = rawPath === "" ? "/index.html" : `${rawPath}.html`;
|
|
||||||
const fallback = getPath(rawPath + "/index.html");
|
|
||||||
const path = getPath(filename);
|
|
||||||
let html = await useStorage().getItem(path);
|
|
||||||
if (!html) html = await useStorage().getItem(fallback);
|
|
||||||
if (!html) html = await useStorage().getItem(getPath("/404.html"));
|
|
||||||
|
|
||||||
// transforms
|
// transforms
|
||||||
const transforms = [doSetUp, deleteServerScripts, insertRegistry];
|
const transforms = [doSetUp, deleteServerScripts, insertRegistry];
|
||||||
|
@ -21,13 +13,21 @@ export default eventHandler(async (event) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO remove server:script tags
|
|
||||||
*/
|
|
||||||
|
|
||||||
return html ?? new Response("Not found", { status: 404 });
|
return html ?? new Response("Not found", { status: 404 });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const getHtml = async (path: string) => {
|
||||||
|
const rawPath = path[path.length - 1] === "/" ? path.slice(0, -1) : path;
|
||||||
|
const filename = rawPath === "" ? "/index.html" : `${rawPath}.html`;
|
||||||
|
const fallback = getPath(rawPath + "/index.html");
|
||||||
|
const filePath = getPath(filename);
|
||||||
|
let html = await useStorage().getItem(filePath);
|
||||||
|
if (!html) html = await useStorage().getItem(fallback);
|
||||||
|
if (!html) html = await useStorage().getItem(getPath("/404.html"));
|
||||||
|
|
||||||
|
return html;
|
||||||
|
};
|
||||||
|
|
||||||
function getPath(filename: string) {
|
function getPath(filename: string) {
|
||||||
return `assets/pages${filename}`;
|
return `assets/pages${filename}`;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
// eslint disable-next-line
|
|
||||||
{
|
{
|
||||||
"extends": "./.nitro/types/tsconfig.json"
|
"extends": "./.nitro/types/tsconfig.json"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue