diff --git a/mcfly.config.ts b/mcfly.config.ts new file mode 100644 index 0000000..a711861 --- /dev/null +++ b/mcfly.config.ts @@ -0,0 +1,6 @@ +import components from "./packages/custom-elements"; +import defineConfig from "./packages/define-config"; + +export default defineConfig({ + integrations: [components()], +}); diff --git a/package-lock.json b/package-lock.json index 31f571d..04eb873 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "nitro-web-components", + "name": "@ayoayco/nitro-web-components", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/plugins/2-register-components.ts b/packages/custom-elements.ts similarity index 57% rename from plugins/2-register-components.ts rename to packages/custom-elements.ts index 8af6127..fea76b7 100644 --- a/plugins/2-register-components.ts +++ b/packages/custom-elements.ts @@ -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..."); const rawKeys = await useStorage().getKeys("/assets/components"); const keys = rawKeys.map((key) => key.replace("assets:components:", "")); @@ -26,4 +44,4 @@ export default defineNitroPlugin(async () => { "./public/.output/registry.js", [...imports, registryObject, customElementsDefine].join(";") ); -}); +}; diff --git a/packages/define-config.ts b/packages/define-config.ts new file mode 100644 index 0000000..62549e3 --- /dev/null +++ b/packages/define-config.ts @@ -0,0 +1,6 @@ +export type McFlyConfig = { + integrations?: Array<() => void>; +}; +export default function defineConfig(config: McFlyConfig) { + return () => config; +} diff --git a/plugins/1-copy-components.ts b/plugins/1-copy-components.ts deleted file mode 100644 index 197701c..0000000 --- a/plugins/1-copy-components.ts +++ /dev/null @@ -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()); - }); -}); diff --git a/plugins/_mcfly-plugin.ts b/plugins/_mcfly-plugin.ts new file mode 100644 index 0000000..46e1c5b --- /dev/null +++ b/plugins/_mcfly-plugin.ts @@ -0,0 +1,9 @@ +import config from "../mcfly.config"; + +export default defineNitroPlugin(() => { + const { integrations } = config(); + if (integrations?.length > 0) + integrations.forEach((integration) => { + integration(); + }); +}); diff --git a/routes/[...index].ts b/routes/[...index].ts index 36b00f2..c993bc0 100644 --- a/routes/[...index].ts +++ b/routes/[...index].ts @@ -2,16 +2,8 @@ import { ELEMENT_NODE, parse, renderSync, walkSync } from "ultrahtml"; import { parseScript } from "esprima"; export default eventHandler(async (event) => { - const rawPath = - event.path[event.path.length - 1] === "/" - ? 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")); + const { path } = event; + let html = await getHtml(path); // transforms 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 }); }); +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) { return `assets/pages${filename}`; } diff --git a/tsconfig.json b/tsconfig.json index c3eae23..43008af 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,4 +1,3 @@ -// eslint disable-next-line { "extends": "./.nitro/types/tsconfig.json" }