refactor(build): own all task spinners in build()

bundle() and generateSitemap() are plain workers now; build() wraps
them in tasks alongside render. Keeps the MPA asset copies and the
MPA client build under the bundles spinner instead of running after
it resolves, and skips the sitemap spinner when sitemap generation
is off.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
pull/5341/head
Divyansh Singh 5 days ago
parent 6699cedc00
commit 6bb45f00b6

@ -56,7 +56,10 @@ export async function build(
const pageMetaMap = Object.create(null) as Record<string, PageMeta>
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()

@ -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

@ -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<string, PageMeta>
) {
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 ===============================

Loading…
Cancel
Save