refactor(build): use async fs and minify while rendering pages

Swap the remaining sync fs calls in renderPage for fs/promises,
switch head-script minification to the async minify API, and make
resolvePageImports async for the realpath call. Pages render under
buildConcurrency, so blocking calls here stall the whole batch.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
pull/5342/head
Divyansh Singh 6 days ago
parent a35c9c900a
commit 840a69dcb6

@ -1,8 +1,7 @@
import { isBooleanAttr } from '@vue/shared'
import fs from 'node:fs'
import { mkdir, writeFile } from 'node:fs/promises'
import { mkdir, realpath, rm, writeFile } from 'node:fs/promises'
import path from 'node:path'
import { minifySync, normalizePath, type Rolldown } from 'vite'
import { minify, normalizePath, type Rolldown } from 'vite'
import { version } from '../../../package.json'
import type { SiteConfig } from '../config'
import {
@ -86,7 +85,7 @@ export async function renderPage(
// resolve imports for index.js + page.md.js and inject script tags
// for them as well so we fetch everything as early as possible
// without having to wait for entry chunks to parse
...resolvePageImports(config, page, result, appChunk),
...(await resolvePageImports(config, page, result, appChunk)),
pageClientJsFileName
])
]
@ -148,7 +147,7 @@ export async function renderPage(
if (matchingChunk) {
if (!matchingChunk.code.includes('import')) {
inlinedScript = `<script type="module">${matchingChunk.code}</script>`
fs.rmSync(path.resolve(config.outDir, matchingChunk.fileName), {
await rm(path.resolve(config.outDir, matchingChunk.fileName), {
force: true
})
} else {
@ -208,7 +207,7 @@ export async function renderPage(
await writeFile(htmlFileName, transformedHtml || html)
}
function resolvePageImports(
async function resolvePageImports(
config: SiteConfig,
page: string,
result: Rolldown.RolldownOutput,
@ -220,7 +219,7 @@ function resolvePageImports(
let srcPath = path.resolve(config.srcDir, page)
try {
if (!config.vite?.resolve?.preserveSymlinks) {
srcPath = fs.realpathSync(srcPath)
srcPath = await realpath(srcPath)
}
} catch (e) {
// if the page is a virtual page generated by a dynamic route this would
@ -248,7 +247,7 @@ async function renderHead(head: HeadConfig[]): Promise<string> {
tag === 'script' &&
(attrs.type === undefined || attrs.type.includes('javascript'))
) {
innerHTML = minifySync('inline-script.js', innerHTML).code
innerHTML = (await minify('inline-script.js', innerHTML)).code
}
return `${openTag}${innerHTML}</${tag}>`
} else {

Loading…
Cancel
Save