|
|
import { webpack, sources } from 'next/dist/compiled/webpack/webpack' |
|
|
import getRouteFromEntrypoint from '../../../server/get-route-from-entrypoint' |
|
|
import { NEXT_FONT_MANIFEST } from '../../../shared/lib/constants' |
|
|
import { traverseModules } from '../utils' |
|
|
import path from 'path' |
|
|
|
|
|
export type NextFontManifest = { |
|
|
pages: { |
|
|
[path: string]: string[] |
|
|
} |
|
|
app: { |
|
|
[entry: string]: string[] |
|
|
} |
|
|
appUsingSizeAdjust: boolean |
|
|
pagesUsingSizeAdjust: boolean |
|
|
} |
|
|
const PLUGIN_NAME = 'NextFontManifestPlugin' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getPreloadedFontFiles(fontFiles: string[]) { |
|
|
return fontFiles.filter((file: string) => |
|
|
/\.p\.(woff|woff2|eot|ttf|otf)$/.test(file) |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getPageIsUsingSizeAdjust(fontFiles: string[]) { |
|
|
return fontFiles.some((file) => file.includes('-s')) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class NextFontManifestPlugin { |
|
|
private appDir: undefined | string |
|
|
|
|
|
constructor(options: { appDir: undefined | string }) { |
|
|
this.appDir = options.appDir |
|
|
} |
|
|
|
|
|
apply(compiler: webpack.Compiler) { |
|
|
compiler.hooks.make.tap(PLUGIN_NAME, (compilation) => { |
|
|
|
|
|
compilation.hooks.processAssets.tap( |
|
|
{ |
|
|
name: PLUGIN_NAME, |
|
|
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS, |
|
|
}, |
|
|
() => { |
|
|
const nextFontManifest: NextFontManifest = { |
|
|
pages: {}, |
|
|
app: {}, |
|
|
appUsingSizeAdjust: false, |
|
|
pagesUsingSizeAdjust: false, |
|
|
} |
|
|
|
|
|
if (this.appDir) { |
|
|
const appDirBase = path.dirname(this.appDir) + path.sep |
|
|
|
|
|
|
|
|
traverseModules( |
|
|
compilation, |
|
|
(mod, _chunk, chunkGroup) => { |
|
|
if (mod?.request?.includes('/next-font-loader/index.js?')) { |
|
|
if (!mod.buildInfo?.assets) return |
|
|
|
|
|
const chunkEntryName = (appDirBase + chunkGroup.name).replace( |
|
|
/[\\/]/g, |
|
|
path.sep |
|
|
) |
|
|
|
|
|
const modAssets = Object.keys(mod.buildInfo.assets) |
|
|
const fontFiles: string[] = modAssets.filter((file: string) => |
|
|
/\.(woff|woff2|eot|ttf|otf)$/.test(file) |
|
|
) |
|
|
|
|
|
|
|
|
if (!nextFontManifest.appUsingSizeAdjust) { |
|
|
nextFontManifest.appUsingSizeAdjust = |
|
|
getPageIsUsingSizeAdjust(fontFiles) |
|
|
} |
|
|
|
|
|
const preloadedFontFiles = getPreloadedFontFiles(fontFiles) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fontFiles.length > 0) { |
|
|
if (!nextFontManifest.app[chunkEntryName]) { |
|
|
nextFontManifest.app[chunkEntryName] = [] |
|
|
} |
|
|
nextFontManifest.app[chunkEntryName].push( |
|
|
...preloadedFontFiles |
|
|
) |
|
|
} |
|
|
} |
|
|
}, |
|
|
(chunkGroup) => { |
|
|
|
|
|
return !!chunkGroup.name?.startsWith('app/') |
|
|
} |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
for (const entrypoint of compilation.entrypoints.values()) { |
|
|
const pagePath = getRouteFromEntrypoint(entrypoint.name!) |
|
|
|
|
|
if (!pagePath) { |
|
|
continue |
|
|
} |
|
|
|
|
|
|
|
|
const fontFiles: string[] = entrypoint.chunks |
|
|
.flatMap((chunk: any) => [...chunk.auxiliaryFiles]) |
|
|
.filter((file: string) => |
|
|
/\.(woff|woff2|eot|ttf|otf)$/.test(file) |
|
|
) |
|
|
|
|
|
|
|
|
if (!nextFontManifest.pagesUsingSizeAdjust) { |
|
|
nextFontManifest.pagesUsingSizeAdjust = |
|
|
getPageIsUsingSizeAdjust(fontFiles) |
|
|
} |
|
|
|
|
|
const preloadedFontFiles = getPreloadedFontFiles(fontFiles) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fontFiles.length > 0) { |
|
|
nextFontManifest.pages[pagePath] = preloadedFontFiles |
|
|
} |
|
|
} |
|
|
|
|
|
const manifest = JSON.stringify(nextFontManifest, null) |
|
|
|
|
|
compilation.emitAsset( |
|
|
`server/${NEXT_FONT_MANIFEST}.js`, |
|
|
new sources.RawSource( |
|
|
`self.__NEXT_FONT_MANIFEST=${JSON.stringify(manifest)}` |
|
|
) as unknown as webpack.sources.RawSource |
|
|
) |
|
|
|
|
|
|
|
|
compilation.emitAsset( |
|
|
`server/${NEXT_FONT_MANIFEST}.json`, |
|
|
new sources.RawSource( |
|
|
manifest |
|
|
) as unknown as webpack.sources.RawSource |
|
|
) |
|
|
} |
|
|
) |
|
|
}) |
|
|
return |
|
|
} |
|
|
} |
|
|
|