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 2 months ago
parent a35c9c900a
commit 840a69dcb6

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

Loading…
Cancel
Save