diff --git a/src/node/build/build.ts b/src/node/build/build.ts index 8c1f56e4..771d8bd4 100644 --- a/src/node/build/build.ts +++ b/src/node/build/build.ts @@ -56,7 +56,10 @@ export async function build( const pageMetaMap = Object.create(null) as Record try { - const out = await bundle(siteConfig, buildOptions, pageMetaMap) + const out = await task( + 'building client + server bundles', + bundle.bind(null, siteConfig, buildOptions, pageMetaMap) + ) if (process.env.BUNDLE_ONLY) { return @@ -74,7 +77,13 @@ export async function build( } } - await generateSitemap(siteConfig, pageMetaMap) + if (siteConfig.sitemap?.hostname) { + await task( + 'generating sitemap', + generateSitemap.bind(null, siteConfig, pageMetaMap) + ) + } + await siteConfig.buildEnd?.(siteConfig) clearCache() diff --git a/src/node/build/bundle.ts b/src/node/build/bundle.ts index 0990a81c..409c5c28 100644 --- a/src/node/build/bundle.ts +++ b/src/node/build/bundle.ts @@ -14,7 +14,6 @@ import { APP_PATH } from '../alias' import type { SiteConfig } from '../config' import { createVitePressPlugin, type PageMeta } from '../plugin' import { escapeRegExp, sanitizeFileName, slash } from '../shared' -import { task } from '../utils/task' import { buildMPAClient } from './buildMPAClient' // https://github.com/vitejs/vite/blob/a55d0b34400e3360c4100d05e422ae9cf10fa07b/packages/vite/src/node/constants.ts#L50 @@ -135,20 +134,12 @@ export async function bundle( configFile: config.vite?.configFile }) - let { clientResult, serverResult } = await task( - 'building client + server bundles', - async () => { - const clientResult = config.mpa - ? null - : ((await build( - await resolveViteConfig(false) - )) as Rolldown.RolldownOutput) - const serverResult = (await build( - await resolveViteConfig(true) - )) as Rolldown.RolldownOutput - return { clientResult, serverResult } - } - ) + let clientResult = config.mpa + ? null + : ((await build(await resolveViteConfig(false))) as Rolldown.RolldownOutput) + const serverResult = (await build( + await resolveViteConfig(true) + )) as Rolldown.RolldownOutput if (config.mpa) { // in MPA mode, we need to copy over the non-js asset files from the diff --git a/src/node/build/generateSitemap.ts b/src/node/build/generateSitemap.ts index 79b9c0fe..2ed98277 100644 --- a/src/node/build/generateSitemap.ts +++ b/src/node/build/generateSitemap.ts @@ -10,68 +10,63 @@ import { } from 'sitemap' import type { SiteConfig } from '../config' import type { PageMeta } from '../plugin' -import { task } from '../utils/task' export async function generateSitemap( siteConfig: SiteConfig, pageMetaMap: Record ) { - if (!siteConfig.sitemap?.hostname) return + const locales = siteConfig.userConfig.locales || {} + const defaultLang = + locales.root?.lang || siteConfig.userConfig.lang || 'en-US' - await task('generating sitemap', async () => { - const locales = siteConfig.userConfig.locales || {} - const defaultLang = - locales.root?.lang || siteConfig.userConfig.lang || 'en-US' + // locale directories whose pages are translations of each other + const localeDirs = Object.keys(locales).filter( + (locale) => locale !== 'root' && locales[locale].lang + ) - // locale directories whose pages are translations of each other - const localeDirs = Object.keys(locales).filter( - (locale) => locale !== 'root' && locales[locale].lang - ) + // group each page with its translations under a locale-independent key + const pageGroups: Record< + string, + { lang: string; url: string; lastmod?: number }[] + > = {} - // group each page with its translations under a locale-independent key - const pageGroups: Record< - string, - { lang: string; url: string; lastmod?: number }[] - > = {} + for (const sourcePage of siteConfig.pages) { + const page = siteConfig.rewrites.map[sourcePage] || sourcePage + const localeDir = page.split('/')[0] - for (const sourcePage of siteConfig.pages) { - const page = siteConfig.rewrites.map[sourcePage] || sourcePage - const localeDir = page.split('/')[0] + const url = page + .replace(/(^|\/)index\.md$/, '$1') + .replace(/\.md$/, siteConfig.cleanUrls ? '' : '.html') - const url = page - .replace(/(^|\/)index\.md$/, '$1') - .replace(/\.md$/, siteConfig.cleanUrls ? '' : '.html') + const key = localeDirs.includes(localeDir) + ? page.slice(localeDir.length + 1) + : page - const key = localeDirs.includes(localeDir) - ? page.slice(localeDir.length + 1) - : page + ;(pageGroups[key] ??= []).push({ + lang: locales[localeDir]?.lang || defaultLang, + url, + lastmod: pageMetaMap[page]?.lastUpdated || undefined + }) + } - ;(pageGroups[key] ??= []).push({ - lang: locales[localeDir]?.lang || defaultLang, - url, - lastmod: pageMetaMap[page]?.lastUpdated || undefined - }) - } + // translated pages link to all their variants (including themselves) + let items: SitemapItem[] = Object.values(pageGroups).flatMap((variants) => + variants.length < 2 + ? { url: variants[0].url, lastmod: variants[0].lastmod } + : variants.map(({ url, lastmod }) => ({ + url, + lastmod, + links: variants + })) + ) + items = (await siteConfig.sitemap?.transformItems?.(items)) || items - // translated pages link to all their variants (including themselves) - let items: SitemapItem[] = Object.values(pageGroups).flatMap((variants) => - variants.length < 2 - ? { url: variants[0].url, lastmod: variants[0].lastmod } - : variants.map(({ url, lastmod }) => ({ - url, - lastmod, - links: variants - })) - ) - items = (await siteConfig.sitemap?.transformItems?.(items)) || items + const sitemapPath = path.join(siteConfig.outDir, 'sitemap.xml') + const sitemapStream = new SitemapStream(siteConfig.sitemap) - const sitemapPath = path.join(siteConfig.outDir, 'sitemap.xml') - const sitemapStream = new SitemapStream(siteConfig.sitemap) - - items.forEach((item) => sitemapStream.write(item)) - sitemapStream.end() - await pipeline(sitemapStream, fs.createWriteStream(sitemapPath)) - }) + items.forEach((item) => sitemapStream.write(item)) + sitemapStream.end() + await pipeline(sitemapStream, fs.createWriteStream(sitemapPath)) } // ============================== Patched Types ===============================