Skip to content

Commit

Permalink
feat: expose page and assets on build hooks TransformContext
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 14, 2023
1 parent b869883 commit 468c049
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
7 changes: 7 additions & 0 deletions src/node/build/build.ts
Expand Up @@ -59,6 +59,12 @@ export async function build(
(chunk) => chunk.type === 'asset' && chunk.fileName.endsWith('.css')
) as OutputAsset

const assets = (siteConfig.mpa ? serverResult : clientResult).output
.filter(
(chunk) => chunk.type === 'asset' && !chunk.fileName.endsWith('.css')
)
.map((asset) => siteConfig.site.base + asset.fileName)

// We embed the hash map and site config strings into each page directly
// so that it doesn't alter the main chunk's hash on every build.
// It's also embedded as a string and JSON.parsed from the client because
Expand All @@ -79,6 +85,7 @@ export async function build(
clientResult,
appChunk,
cssChunk,
assets,
pageToHashMap,
hashMapString,
siteDataString
Expand Down
48 changes: 26 additions & 22 deletions src/node/build/render.ts
Expand Up @@ -26,6 +26,7 @@ export async function renderPage(
result: RollupOutput | null,
appChunk: OutputChunk | undefined,
cssChunk: OutputAsset | undefined,
assets: string[],
pageToHashMap: Record<string, string>,
hashMapString: string,
siteDataString: string
Expand Down Expand Up @@ -86,21 +87,18 @@ export async function renderPage(
preloadLinks = preloadLinks.filter((link) => shouldPreload(link, page))
}

const preloadLinksString = preloadLinks
.map((file) => {
return `<link rel="modulepreload" href="${
EXTERNAL_URL_RE.test(file) ? '' : siteData.base // don't add base to external urls
}${file}">`
})
.join('\n ')
const toHeadTags = (files: string[], rel: string): HeadConfig[] =>
files.map((file) => [
'link',
{
rel,
// don't add base to external urls
href: (EXTERNAL_URL_RE.test(file) ? '' : siteData.base) + file
}
])

const prefetchLinkString = prefetchLinks
.map((file) => {
return `<link rel="prefetch" href="${
EXTERNAL_URL_RE.test(file) ? '' : siteData.base // don't add base to external urls
}${file}">`
})
.join('\n ')
const preloadHeadTags = toHeadTags(preloadLinks, 'modulepreload')
const prefetchHeadTags = toHeadTags(prefetchLinks, 'prefetch')

const stylesheetLink = cssChunk
? `<link rel="preload stylesheet" href="${siteData.base}${cssChunk.fileName}" as="style">`
Expand All @@ -109,21 +107,27 @@ export async function renderPage(
const title: string = createTitle(siteData, pageData)
const description: string = pageData.description || siteData.description

const headBeforeTransform = mergeHead(
siteData.head,
filterOutHeadDescription(pageData.frontmatter.head)
)
const headBeforeTransform = [
...preloadHeadTags,
...prefetchHeadTags,
...mergeHead(
siteData.head,
filterOutHeadDescription(pageData.frontmatter.head)
)
]

const head = mergeHead(
headBeforeTransform,
(await config.transformHead?.({
page,
siteConfig: config,
siteData,
pageData,
title,
description,
head: headBeforeTransform,
content
content,
assets
})) || []
)

Expand Down Expand Up @@ -165,8 +169,6 @@ export async function renderPage(
? `<script type="module" src="${siteData.base}${appChunk.fileName}"></script>`
: ``
}
${preloadLinksString}
${prefetchLinkString}
${await renderHead(head)}
</head>
<body>${teleports?.body || ''}
Expand All @@ -179,13 +181,15 @@ export async function renderPage(

await fs.ensureDir(path.dirname(htmlFileName))
const transformedHtml = await config.transformHtml?.(html, htmlFileName, {
page,
siteConfig: config,
siteData,
pageData,
title,
description,
head,
content
content,
assets
})
await fs.writeFile(htmlFileName, transformedHtml || html)
}
Expand Down
3 changes: 3 additions & 0 deletions src/node/config.ts
Expand Up @@ -43,6 +43,7 @@ export interface UserConfig<ThemeConfig = any>
srcExclude?: string[]
outDir?: string
cacheDir?: string

shouldPreload?: (link: string, page: string) => boolean

locales?: LocaleConfig<ThemeConfig>
Expand Down Expand Up @@ -142,13 +143,15 @@ export interface UserConfig<ThemeConfig = any>
}

export interface TransformContext {
page: string
siteConfig: SiteConfig
siteData: SiteData
pageData: PageData
title: string
description: string
head: HeadConfig[]
content: string
assets: string[]
}

export type RawConfigExports<ThemeConfig = any> =
Expand Down

0 comments on commit 468c049

Please sign in to comment.