From 05dd10a38e804711888dec86b8773060a4f16943 Mon Sep 17 00:00:00 2001 From: Zack Spear Date: Mon, 5 Jun 2023 15:09:33 -0700 Subject: [PATCH] feat: rebuild manifest --- package.json | 5 ++++- postbuild.ts | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 postbuild.ts diff --git a/package.json b/package.json index d725a5735..e6c5f69a1 100644 --- a/package.json +++ b/package.json @@ -3,11 +3,14 @@ "private": true, "scripts": { "build": "nuxt build", + "postbuild": "npm run rebuildManifest", "dev": "nuxt dev", "generate": "nuxt generate", + "postgenerate": "npm run rebuildManifest", "preview": "nuxt preview", "postinstall": "nuxt prepare", - "serve": "serve dist/nuxt-custom-elements/connect-components" + "serve": "serve dist/nuxt-custom-elements/connect-components", + "rebuildManifest": "node postBuild.ts" }, "devDependencies": { "@nuxtjs/tailwindcss": "^6.7.0", diff --git a/postbuild.ts b/postbuild.ts new file mode 100644 index 000000000..453cfbeb9 --- /dev/null +++ b/postbuild.ts @@ -0,0 +1,61 @@ +// type Manifest = { +// ['connect-components.client.css']: ManifestCSS; +// ['connect-components.client.mjs']: ManifestJS; +// } + +// type ManifestCSS = { +// file: string; +// src: string; +// } + +// type ManifestJS = { +// css: string[]; +// file: string; +// isEntry: boolean; +// src: string; +// } + +const fs = require('fs'); + +const manifest = '.nuxt/nuxt-custom-elements/dist/connect-components/manifest.json'; +const stringToRemove = '.nuxt/nuxt-custom-elements/entries/'; + +const originalCssKey = '.nuxt/nuxt-custom-elements/entries/connect-components.client.css'; +const originalJsKey = '.nuxt/nuxt-custom-elements/entries/connect-components.client.mjs'; + +console.log('*****************************'); +console.log('* RE-BUILDING MANIFEST.JSON *'); +console.log('*****************************'); + +fs.readFile(manifest, 'utf8', (error, data) => { + if (error){ + console.log(error); + return; + } + const parsedData = JSON.parse(data); + + if ('connect-components.client.mjs' in parsedData) return console.log('Manifest already re-written'); + if (!(originalJsKey in parsedData)) return console.error(`Expected JS key not in ${manifest}`); + + // create new manifest with updated top-level keys + const newManifest = { + 'connect-components.client.css': { + ...parsedData[originalCssKey], + }, + 'connect-components.client.mjs': { + ...parsedData[originalJsKey], + } + }; + // then rewrite the manifest + fs.writeFile(manifest, JSON.stringify(newManifest, null, 2), (err) => { + if (err) { + console.log('Failed to write updated data to file', err); + return; + } + console.log('Updated file successfully'); + }); + // echo out the filename that needs to be created on the server + const map = new Map(Object.entries(newManifest)); + const js = map.get('connect-components.client.mjs'); + console.log(`JS FILE FOR WEBGUI: ${js.file}`); +}); \ No newline at end of file