rasengan
Version:
The modern React Framework
92 lines (91 loc) • 3.24 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import fs from "fs";
export class ManifestManager {
_manifestPath;
_manifest;
_entry = "src/index";
constructor(manifestPath) {
this._manifestPath = manifestPath;
this._manifest = this.loadManifest();
}
/**
* Load the manifest file.
*/
loadManifest() {
try {
const content = fs.readFileSync(this._manifestPath, "utf-8");
return JSON.parse(content);
}
catch (error) {
console.error(`Error loading manifest file: ${error.message}`);
return {};
}
}
/**
* Resolve all assets for a given page.
* @param pageName - The name of the page to resolve.
* @returns Resolved assets with scripts and styles.
*/
resolveAssets(pageName) {
const visited = new Set();
const assets = {
scripts: [],
styles: [],
};
const collectAssets = (entry) => {
if (!entry || visited.has(entry.name))
return;
visited.add(entry.name);
// Add scripts
if (entry.file && !assets.scripts.includes(entry.file)) {
assets.scripts.push(entry.file);
}
// Add CSS
if (entry.css) {
entry.css.forEach((cssFile) => {
if (!assets.styles.includes(cssFile)) {
assets.styles.push(cssFile);
}
});
}
// // Collect static imports
// if (entry.imports) {
// entry.imports.forEach((importName) => {
// const isPageChunk = importName.includes("page-");
// if (!isPageChunk) {
// collectAssets(this._manifest[importName]);
// } else {
// const pagePath = importName.split("-")[1];
// if (pagePath === pageName) {
// collectAssets(this._manifest[importName]);
// }
// }
// });
// }
if (entry.imports) {
for (const importName of entry.imports) {
collectAssets(this._manifest[importName]);
}
}
// Collect dynamic imports
if (entry.dynamicImports) {
for (const dynamicImport of entry.dynamicImports) {
collectAssets(this._manifest[dynamicImport]);
}
}
};
collectAssets(this._manifest[`${this._entry}.js`] || this._manifest[`${this._entry}.ts`]);
return assets;
}
/**
* Generate meta tags for a given page.
* @param pageName - The name of the page to generate meta tags for.
* @returns HTML string containing script and style tags.
*/
generateMetaTags(pageName) {
const { scripts, styles } = this.resolveAssets(pageName);
const scriptTags = scripts.map((file) => (_jsx("script", { type: 'module', src: `/${file}` })));
const styleTags = styles.map((file) => (_jsx("link", { rel: 'stylesheet', href: `/${file}` })));
return [...scriptTags, ...styleTags];
}
}