refactor: clean up types

pull/5297/head
Divyansh Singh 2 months ago
parent c0e2e18094
commit 470c4e018a

@ -65,39 +65,42 @@ export async function build(
const { render } = await nativeImport(entryPath) const { render } = await nativeImport(entryPath)
await task('rendering pages', async () => { await task('rendering pages', async () => {
const appChunk = const clientOutput: (Rolldown.OutputChunk | Rolldown.OutputAsset)[] =
clientResult && clientResult?.output || []
(clientResult.output.find(
(chunk) => const appChunk = clientOutput.find(
chunk.type === 'chunk' && (chunk): chunk is Rolldown.OutputChunk =>
chunk.isEntry && chunk.type === 'chunk' &&
chunk.facadeModuleId?.endsWith('.js') chunk.isEntry &&
) as Rolldown.OutputChunk) !!chunk.facadeModuleId?.endsWith('.js')
)
const cssChunk = (
siteConfig.mpa ? serverResult : clientResult!
).output.find(
(chunk) => chunk.type === 'asset' && chunk.fileName.endsWith('.css')
) as Rolldown.OutputAsset
const assets = (siteConfig.mpa ? serverResult : clientResult!).output
.filter(
(chunk) => chunk.type === 'asset' && !chunk.fileName.endsWith('.css')
)
.map((asset) => siteConfig.site.base + asset.fileName)
// default theme special handling: inject font preload const isDefaultTheme = clientOutput.some(
// custom themes will need to use `transformHead` to inject this (chunk): chunk is Rolldown.OutputChunk =>
const additionalHeadTags: HeadConfig[] = [] chunk.type === 'chunk' &&
const isDefaultTheme = chunk.name === 'theme' &&
clientResult && chunk.moduleIds.some((id) => id.includes('client/theme-default'))
clientResult.output.some( )
(chunk) =>
chunk.type === 'chunk' && // ----
chunk.name === 'theme' &&
chunk.moduleIds.some((id) => id.includes('client/theme-default'))
)
const resultOutput: (Rolldown.OutputChunk | Rolldown.OutputAsset)[] =
(siteConfig.mpa ? serverResult : clientResult)?.output || []
const cssChunk = resultOutput.find(
(chunk): chunk is Rolldown.OutputAsset =>
chunk.type === 'asset' && chunk.fileName.endsWith('.css')
)
// prettier-ignore
const assets = resultOutput.filter(
(chunk): chunk is Rolldown.OutputAsset =>
chunk.type === 'asset' && !chunk.fileName.endsWith('.css')
).map((asset) => siteConfig.site.base + asset.fileName)
// ----
const additionalHeadTags: HeadConfig[] = []
const metadataScript = generateMetadataScript(pageToHashMap, siteConfig) const metadataScript = generateMetadataScript(pageToHashMap, siteConfig)
if (isDefaultTheme) { if (isDefaultTheme) {

@ -2,6 +2,7 @@ import fs from 'node:fs'
import { cp } from 'node:fs/promises' import { cp } from 'node:fs/promises'
import path from 'node:path' import path from 'node:path'
import { fileURLToPath } from 'node:url' import { fileURLToPath } from 'node:url'
import pMap from 'p-map'
import { import {
build, build,
normalizePath, normalizePath,
@ -102,8 +103,8 @@ export async function bundle(
app: path.resolve(APP_PATH, ssr ? 'ssr.js' : 'index.js'), app: path.resolve(APP_PATH, ssr ? 'ssr.js' : 'index.js'),
...input ...input
}, },
// important so that each page chunk and the index export things for each // important so that each page chunk and the index export things for
// other // each other
preserveEntrySignatures: 'allow-extension', preserveEntrySignatures: 'allow-extension',
output: { output: {
sanitizeFileName, sanitizeFileName,
@ -170,42 +171,40 @@ export async function bundle(
configFile: config.vite?.configFile configFile: config.vite?.configFile
}) })
let clientResult!: Rolldown.RolldownOutput | null let clientResult: Rolldown.RolldownOutput | null = null
let serverResult!: Rolldown.RolldownOutput let serverResult!: Rolldown.RolldownOutput
// prettier-ignore
await task('building client + server bundles', async () => { await task('building client + server bundles', async () => {
clientResult = config.mpa if (!config.mpa) clientResult =
? null (await build(await resolveViteConfig(false))) as Rolldown.RolldownOutput
: ((await build( serverResult =
await resolveViteConfig(false) (await build(await resolveViteConfig(true))) as Rolldown.RolldownOutput
)) as Rolldown.RolldownOutput)
serverResult = (await build(
await resolveViteConfig(true)
)) as Rolldown.RolldownOutput
}) })
if (config.mpa) { if (config.mpa) {
// in MPA mode, we need to copy over the non-js asset files from the // in MPA mode, we need to copy over the non-js asset files from the
// server build since there is no client-side build. // server build since there is no client-side build.
await Promise.all( await pMap(
serverResult.output.map(async (chunk) => { serverResult.output,
async (chunk) => {
if (!chunk.fileName.endsWith('.js')) { if (!chunk.fileName.endsWith('.js')) {
const tempPath = path.resolve(config.tempDir, chunk.fileName) const tempPath = path.resolve(config.tempDir, chunk.fileName)
const outPath = path.resolve(config.outDir, chunk.fileName) const outPath = path.resolve(config.outDir, chunk.fileName)
await cp(tempPath, outPath) await cp(tempPath, outPath)
} }
}) },
{ concurrency: config.buildConcurrency }
) )
// also copy over public dir // also copy over public dir
const publicDir = path.resolve(config.srcDir, 'public') const publicDir = path.resolve(config.srcDir, 'public')
if (fs.existsSync(publicDir)) { if (fs.existsSync(publicDir)) {
// dereference symlinks like vite's own publicDir copy does, and so that // dereference symlinks like vite's own publicDir copy does, and so that
// copying over an existing symlinked file does not fail with EEXIST // copying over an existing symlinked file does not fail with EEXIST
await cp(publicDir, config.outDir, { await cp(publicDir, config.outDir, { recursive: true, dereference: true })
recursive: true,
dereference: true
})
} }
// build <script client> bundle // build <script client> bundle
if (Object.keys(clientJSMap).length) { if (Object.keys(clientJSMap).length) {
clientResult = await buildMPAClient(clientJSMap, config) clientResult = await buildMPAClient(clientJSMap, config)

@ -24,9 +24,9 @@ export async function renderPage(
render: (path: string) => Promise<SSGContext>, render: (path: string) => Promise<SSGContext>,
config: SiteConfig, config: SiteConfig,
page: string, // foo.md page: string, // foo.md
result: Rolldown.RolldownOutput | null, result: Rolldown.RolldownOutput | null | undefined,
appChunk: Rolldown.OutputChunk | null, appChunk: Rolldown.OutputChunk | null | undefined,
cssChunk: Rolldown.OutputAsset | null, cssChunk: Rolldown.OutputAsset | null | undefined,
assets: string[], assets: string[],
pageToHashMap: Record<string, string>, pageToHashMap: Record<string, string>,
metadataScript: { html: string; inHead: boolean }, metadataScript: { html: string; inHead: boolean },
@ -140,10 +140,10 @@ export async function renderPage(
let inlinedScript = '' let inlinedScript = ''
if (config.mpa && result) { if (config.mpa && result) {
const matchingChunk = result.output.find( const matchingChunk = result.output.find(
(chunk) => (chunk): chunk is Rolldown.OutputChunk =>
chunk.type === 'chunk' && chunk.type === 'chunk' &&
chunk.facadeModuleId === slash(path.join(config.srcDir, page)) chunk.facadeModuleId === slash(path.join(config.srcDir, page))
) as Rolldown.OutputChunk )
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>`
@ -227,12 +227,13 @@ function resolvePageImports(
} }
srcPath = normalizePath(srcPath) srcPath = normalizePath(srcPath)
const pageChunk = result.output.find( const pageChunk = result.output.find(
(chunk) => chunk.type === 'chunk' && chunk.facadeModuleId === srcPath (chunk): chunk is Rolldown.OutputChunk =>
) as Rolldown.OutputChunk chunk.type === 'chunk' && chunk.facadeModuleId === srcPath
)
return [ return [
...appChunk.imports, ...appChunk.imports,
// ...appChunk.dynamicImports, // ...appChunk.dynamicImports,
...pageChunk.imports ...(pageChunk?.imports || [])
// ...pageChunk.dynamicImports // ...pageChunk.dynamicImports
] ]
} }

@ -53,14 +53,13 @@ const staticRestoreRE = /__VP_STATIC_(START|END)__/g
// media queries. // media queries.
const scriptClientRE = /<script\b[^>]*client\b[^>]*>([^]*?)<\/script>/ const scriptClientRE = /<script\b[^>]*client\b[^>]*>([^]*?)<\/script>/
const isPageChunk = ( const isPageChunk = <T extends Rolldown.OutputChunk | Rolldown.RenderedChunk>(
chunk: Rolldown.OutputAsset | Rolldown.OutputChunk chunk: Rolldown.OutputAsset | T
): chunk is Rolldown.OutputChunk & { facadeModuleId: string } => ): chunk is T =>
!!( !!(
chunk.type === 'chunk' && chunk.type === 'chunk' &&
chunk.isEntry && chunk.isEntry &&
chunk.facadeModuleId && chunk.facadeModuleId?.endsWith('.md')
chunk.facadeModuleId.endsWith('.md')
) )
const cleanUrl = (url: string): string => url.replace(/[?#].*$/s, '') const cleanUrl = (url: string): string => url.replace(/[?#].*$/s, '')
@ -285,7 +284,7 @@ export async function createVitePressPlugin(
}, },
renderChunk(code, chunk) { renderChunk(code, chunk) {
if (!ssr && isPageChunk(chunk as Rolldown.OutputChunk)) { if (!ssr && isPageChunk(chunk)) {
// For each page chunk, inject marker for start/end of static strings. // For each page chunk, inject marker for start/end of static strings.
// we do this here because in generateBundle the chunks would have been // we do this here because in generateBundle the chunks would have been
// minified and we won't be able to safely locate the strings. // minified and we won't be able to safely locate the strings.

Loading…
Cancel
Save