next-sitemap
Version:
Sitemap generator for next.js
51 lines (50 loc) • 1.83 kB
JavaScript
import { Logger } from '../logger.js';
import { loadJSON } from '../utils/file.js';
import fg from 'fast-glob';
export class ManifestParser {
config;
runtimePaths;
constructor(config, runtimePaths) {
this.config = config;
this.runtimePaths = runtimePaths;
}
/**
* Return paths of html files if config.output = "export"
* @param exportFolder
* @returns
*/
async getStaticExportPages(config, exportFolder) {
// Skip this step if config.output is not export mode
if (config?.output !== 'export') {
return [];
}
// Get html file paths using glob
const htmlFiles = await fg(`${exportFolder}/**/*.html`);
// Cleanup files
return htmlFiles?.map((file) => file
.replace(exportFolder, '')
.replace('index', '')
.replace('.html', '')
.trim());
}
async loadManifest() {
// Load build manifest
const buildManifest = await loadJSON(this.runtimePaths.BUILD_MANIFEST);
// Throw error if no build manifest exist
if (this.config?.output !== 'export' && !buildManifest) {
throw Logger.noBuildManifest();
}
// Load pre-render manifest
const preRenderManifest = await loadJSON(this.runtimePaths.PRERENDER_MANIFEST);
// Load routes manifest
const routesManifest = await loadJSON(this.runtimePaths.ROUTES_MANIFEST);
// Get static export path when output is set as "export"
const staticExportPages = await this.getStaticExportPages(this.config, this.runtimePaths.STATIC_EXPORT_ROOT);
return {
build: buildManifest ?? {},
preRender: preRenderManifest,
routes: routesManifest,
staticExportPages,
};
}
}