Skip to content

Commit 468c049

Browse files
committed
feat: expose page and assets on build hooks TransformContext
1 parent b869883 commit 468c049

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

src/node/build/build.ts

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ export async function build(
5959
(chunk) => chunk.type === 'asset' && chunk.fileName.endsWith('.css')
6060
) as OutputAsset
6161

62+
const assets = (siteConfig.mpa ? serverResult : clientResult).output
63+
.filter(
64+
(chunk) => chunk.type === 'asset' && !chunk.fileName.endsWith('.css')
65+
)
66+
.map((asset) => siteConfig.site.base + asset.fileName)
67+
6268
// We embed the hash map and site config strings into each page directly
6369
// so that it doesn't alter the main chunk's hash on every build.
6470
// It's also embedded as a string and JSON.parsed from the client because
@@ -79,6 +85,7 @@ export async function build(
7985
clientResult,
8086
appChunk,
8187
cssChunk,
88+
assets,
8289
pageToHashMap,
8390
hashMapString,
8491
siteDataString

src/node/build/render.ts

+26-22
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export async function renderPage(
2626
result: RollupOutput | null,
2727
appChunk: OutputChunk | undefined,
2828
cssChunk: OutputAsset | undefined,
29+
assets: string[],
2930
pageToHashMap: Record<string, string>,
3031
hashMapString: string,
3132
siteDataString: string
@@ -86,21 +87,18 @@ export async function renderPage(
8687
preloadLinks = preloadLinks.filter((link) => shouldPreload(link, page))
8788
}
8889

89-
const preloadLinksString = preloadLinks
90-
.map((file) => {
91-
return `<link rel="modulepreload" href="${
92-
EXTERNAL_URL_RE.test(file) ? '' : siteData.base // don't add base to external urls
93-
}${file}">`
94-
})
95-
.join('\n ')
90+
const toHeadTags = (files: string[], rel: string): HeadConfig[] =>
91+
files.map((file) => [
92+
'link',
93+
{
94+
rel,
95+
// don't add base to external urls
96+
href: (EXTERNAL_URL_RE.test(file) ? '' : siteData.base) + file
97+
}
98+
])
9699

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

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

112-
const headBeforeTransform = mergeHead(
113-
siteData.head,
114-
filterOutHeadDescription(pageData.frontmatter.head)
115-
)
110+
const headBeforeTransform = [
111+
...preloadHeadTags,
112+
...prefetchHeadTags,
113+
...mergeHead(
114+
siteData.head,
115+
filterOutHeadDescription(pageData.frontmatter.head)
116+
)
117+
]
116118

117119
const head = mergeHead(
118120
headBeforeTransform,
119121
(await config.transformHead?.({
122+
page,
120123
siteConfig: config,
121124
siteData,
122125
pageData,
123126
title,
124127
description,
125128
head: headBeforeTransform,
126-
content
129+
content,
130+
assets
127131
})) || []
128132
)
129133

@@ -165,8 +169,6 @@ export async function renderPage(
165169
? `<script type="module" src="${siteData.base}${appChunk.fileName}"></script>`
166170
: ``
167171
}
168-
${preloadLinksString}
169-
${prefetchLinkString}
170172
${await renderHead(head)}
171173
</head>
172174
<body>${teleports?.body || ''}
@@ -179,13 +181,15 @@ export async function renderPage(
179181

180182
await fs.ensureDir(path.dirname(htmlFileName))
181183
const transformedHtml = await config.transformHtml?.(html, htmlFileName, {
184+
page,
182185
siteConfig: config,
183186
siteData,
184187
pageData,
185188
title,
186189
description,
187190
head,
188-
content
191+
content,
192+
assets
189193
})
190194
await fs.writeFile(htmlFileName, transformedHtml || html)
191195
}

src/node/config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export interface UserConfig<ThemeConfig = any>
4343
srcExclude?: string[]
4444
outDir?: string
4545
cacheDir?: string
46+
4647
shouldPreload?: (link: string, page: string) => boolean
4748

4849
locales?: LocaleConfig<ThemeConfig>
@@ -142,13 +143,15 @@ export interface UserConfig<ThemeConfig = any>
142143
}
143144

144145
export interface TransformContext {
146+
page: string
145147
siteConfig: SiteConfig
146148
siteData: SiteData
147149
pageData: PageData
148150
title: string
149151
description: string
150152
head: HeadConfig[]
151153
content: string
154+
assets: string[]
152155
}
153156

154157
export type RawConfigExports<ThemeConfig = any> =

0 commit comments

Comments
 (0)