import { find, forEach, map } from "lodash"; // import { FetchedData } from "../fetchPublicData"; import getLocales from "../helpers/getLocales"; import createPaths from "../helpers/createPaths"; import createFilenames from "../helpers/createFilenames"; import templatePaths from "../helpers/templatePaths"; import chunksByPage from "../helpers/chunksByPage"; import createCommonConfig from "../helpers/createCommonConfig"; import getPageAndGlobalTranslations from "../helpers/getPageAndGlobalTranslations"; import getUncategorizedCategoryProperties from "../helpers/getUncategorizedCategoryProperties"; const HtmlWebpackPlugin = require("html-webpack-plugin"); type ArchiveData = { locale: string; categories: { name: string; link: string; }[]; availableLocales: string[]; }; const archives = (responses: FetchedData): typeof HtmlWebpackPlugin[] => { const categories = responses.categoriesResponse.data.items; const archiveData: ArchiveData[] = []; forEach(getLocales(), locale => { const categoryData: ArchiveData["categories"] = []; forEach(categories, category => { const categoryTranslation = find(category.translations, { locale }); if (!categoryTranslation) return; categoryData.push({ name: categoryTranslation.name, link: `${createPaths.getPublicUrl()}/${createPaths.category(locale, category.slug)}` }); }); const uncategorizedProps = getUncategorizedCategoryProperties(locale); categoryData.push({ name: uncategorizedProps.name, link: `${createPaths.getPublicUrl()}/${createPaths.category(locale, uncategorizedProps.slug)}` }); archiveData.push({ locale, categories: categoryData, availableLocales: getLocales() }); }); return map( archiveData, ({ locale, categories, availableLocales }) => new HtmlWebpackPlugin({ filename: createFilenames.archive(locale), template: templatePaths.archive, chunks: chunksByPage.archive, templateParameters: { ...createCommonConfig(locale, availableLocales, createPaths.archive), categories, i18n: getPageAndGlobalTranslations(locale, "archive") } }) ); }; export default archives;