Unverified Commit de427196 authored by Janicklas Ralph's avatar Janicklas Ralph Committed by GitHub

Fix font optimization failing on some builds (#25071)

Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent b9b35d40
...@@ -114,7 +114,19 @@ export class FontStylesheetGatheringPlugin { ...@@ -114,7 +114,19 @@ export class FontStylesheetGatheringPlugin {
} }
this.gatheredStylesheets.push(props.href) this.gatheredStylesheets.push(props.href)
if (isWebpack5) {
const buildInfo = parser?.state?.module?.buildInfo
if (buildInfo) {
buildInfo.valueDependencies.set(
FONT_MANIFEST,
this.gatheredStylesheets
)
}
}
} }
// React JSX transform: // React JSX transform:
parser.hooks.call parser.hooks.call
.for('_jsx') .for('_jsx')
...@@ -161,8 +173,24 @@ export class FontStylesheetGatheringPlugin { ...@@ -161,8 +173,24 @@ export class FontStylesheetGatheringPlugin {
} }
compilation.hooks.finishModules.tapAsync( compilation.hooks.finishModules.tapAsync(
this.constructor.name, this.constructor.name,
async (_: any, modulesFinished: Function) => { async (modules: any, modulesFinished: Function) => {
const fontDefinitionPromises = this.gatheredStylesheets.map((url) => let fontStylesheets = this.gatheredStylesheets
if (isWebpack5) {
const fontUrls = new Set<string>()
modules.forEach((module: any) => {
const fontDependencies = module?.buildInfo?.valueDependencies?.get(
FONT_MANIFEST
)
if (fontDependencies) {
fontDependencies.forEach((v: string) => fontUrls.add(v))
}
})
fontStylesheets = Array.from(fontUrls)
}
const fontDefinitionPromises = fontStylesheets.map((url) =>
getFontDefinitionFromNetwork(url) getFontDefinitionFromNetwork(url)
) )
...@@ -173,7 +201,7 @@ export class FontStylesheetGatheringPlugin { ...@@ -173,7 +201,7 @@ export class FontStylesheetGatheringPlugin {
if (css) { if (css) {
const content = await minifyCss(css) const content = await minifyCss(css)
this.manifestContent.push({ this.manifestContent.push({
url: this.gatheredStylesheets[promiseIndex], url: fontStylesheets[promiseIndex],
content, content,
}) })
} }
......
...@@ -210,15 +210,22 @@ describe('Font Optimization', () => { ...@@ -210,15 +210,22 @@ describe('Font Optimization', () => {
expect(testCss).toStrictEqual(snapshotCss) expect(testCss).toStrictEqual(snapshotCss)
}) })
// Re-run build to check if it works when build is cached
it('should work when build is cached', async () => {
await nextBuild(appDir)
const testJson = JSON.parse(
await fs.readFile(builtPage('font-manifest.json'), {
encoding: 'utf-8',
})
)
expect(testJson.length).toBeGreaterThan(0)
})
} }
describe('Font optimization for SSR apps', () => { describe('Font optimization for SSR apps', () => {
beforeAll(async () => { beforeAll(async () => {
await fs.writeFile( await fs.writeFile(nextConfig, `module.exports = { }`, 'utf8')
nextConfig,
`module.exports = { experimental: {optimizeFonts: true} }`,
'utf8'
)
if (fs.pathExistsSync(join(appDir, '.next'))) { if (fs.pathExistsSync(join(appDir, '.next'))) {
await fs.remove(join(appDir, '.next')) await fs.remove(join(appDir, '.next'))
...@@ -237,7 +244,7 @@ describe('Font Optimization', () => { ...@@ -237,7 +244,7 @@ describe('Font Optimization', () => {
beforeAll(async () => { beforeAll(async () => {
await fs.writeFile( await fs.writeFile(
nextConfig, nextConfig,
`module.exports = { target: 'serverless', experimental: {optimizeFonts: true} }`, `module.exports = { target: 'serverless' }`,
'utf8' 'utf8'
) )
await nextBuild(appDir) await nextBuild(appDir)
...@@ -254,7 +261,7 @@ describe('Font Optimization', () => { ...@@ -254,7 +261,7 @@ describe('Font Optimization', () => {
beforeAll(async () => { beforeAll(async () => {
await fs.writeFile( await fs.writeFile(
nextConfig, nextConfig,
`module.exports = { target: 'experimental-serverless-trace', experimental: {optimizeFonts: true} }`, `module.exports = { target: 'experimental-serverless-trace' }`,
'utf8' 'utf8'
) )
await nextBuild(appDir) await nextBuild(appDir)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment