From 2e12da70d9e8d71caa997ac028a1492114e18a0e Mon Sep 17 00:00:00 2001 From: Ayo Date: Tue, 10 Oct 2023 18:40:12 +0200 Subject: [PATCH] refactor: fs imports --- plugins/1-copy-components.ts | 7 ++++--- plugins/2-register-components.ts | 8 ++++---- routes/[...index].ts | 3 --- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/plugins/1-copy-components.ts b/plugins/1-copy-components.ts index 1679e21..197701c 100644 --- a/plugins/1-copy-components.ts +++ b/plugins/1-copy-components.ts @@ -1,11 +1,12 @@ -import * as fs from "fs"; +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 (!fs.existsSync("./public/.output")) fs.mkdirSync("./public/.output"); - fs.writeFileSync(`./public/.output/${cleanKey}`, content.toString()); + if (!existsSync("./public/.output")) mkdirSync("./public/.output"); + writeFileSync(`./public/.output/${cleanKey}`, content.toString()); }); }); diff --git a/plugins/2-register-components.ts b/plugins/2-register-components.ts index 67eccf7..8af6127 100644 --- a/plugins/2-register-components.ts +++ b/plugins/2-register-components.ts @@ -1,4 +1,4 @@ -import * as fs from "fs"; +import { mkdirSync, writeFileSync, existsSync } from "node:fs"; export default defineNitroPlugin(async () => { console.log("Building registry of custom elements..."); @@ -19,10 +19,10 @@ export default defineNitroPlugin(async () => { const customElementsDefine = `Object.keys(registry).forEach((key) => {if(window?.hasOwnProperty("customElements"))customElements.define(key, registry[key]);})`; - if (!fs.existsSync("./public/.output")) { - fs.mkdirSync("./public/.output"); + if (!existsSync("./public/.output")) { + mkdirSync("./public/.output"); } - fs.writeFileSync( + writeFileSync( "./public/.output/registry.js", [...imports, registryObject, customElementsDefine].join(";") ); diff --git a/routes/[...index].ts b/routes/[...index].ts index 237dee5..eb41dfc 100644 --- a/routes/[...index].ts +++ b/routes/[...index].ts @@ -60,14 +60,11 @@ function doSetUp(html: string) { const setupMap = {}; serverScripts.forEach((script: string) => { - console.log(script); - const constructor = ` new Function(\`${script}\`); `; const evalStore = eval(constructor); Object.assign(setupMap, new evalStore()); - console.log(">>> from server", setupMap); }); return html.replace("{{name}}", setupMap["name"]);