polen
Version:
A framework for delightful GraphQL developer portals
32 lines • 1.36 kB
JavaScript
import { assetUrl } from '#api/utils/asset-url/index';
import { Group, Str } from '@wollybeard/kit';
export const injectManifestIntoHtml = (html, manifest, basePath) => {
const assets = getRelevantAsssetsFromManifest(manifest, basePath);
return injectAssetsIntoHtml(html, assets);
};
const getRelevantAsssetsFromManifest = (manifest, basePath) => {
const htmlAssets = [];
for (const manifestChunk of Object.values(manifest)) {
if (manifestChunk.isEntry) {
htmlAssets.push({ type: `js`, path: assetUrl(manifestChunk.file, basePath) });
}
for (const cssItem of manifestChunk.css ?? []) {
htmlAssets.push({ type: `css`, path: assetUrl(cssItem, basePath) });
}
}
return Group.by(htmlAssets, `type`);
};
const injectAssetsIntoHtml = (html, assets) => {
const htmlAssets = Group.map(assets, {
css: (assets) => assets
.map((asset) => `<link rel="stylesheet" href="${asset.path}" />`)
.join(Str.Char.newline),
js: (assets) => assets
.map((asset) => `<script type="module" src="${asset.path}"></script>`)
.join(Str.Char.newline),
});
return html
.replace(`</head>`, `${htmlAssets.css ?? ``}</head>`)
.replace(`</body>`, `${htmlAssets.js ?? ``}</body>`);
};
//# sourceMappingURL=manifest.js.map