1010 * recursively extracting nested media from sub-sub-compositions.
1111 */
1212
13- import { readFileSync , existsSync , mkdirSync } from "fs" ;
13+ import { createReadStream , existsSync , mkdirSync , readFileSync } from "fs" ;
1414import { join , dirname , resolve , basename } from "path" ;
1515import { parseHTML } from "linkedom" ;
1616import {
@@ -1655,6 +1655,28 @@ export async function localizeRemoteFontFaces(
16551655}
16561656
16571657const LOCAL_FONTFACE_URL_RE = / u r l \( [ " ' ] ? (? ! d a t a : | h t t p s ? : \/ \/ ) ( [ ^ " ' ) ] + ) [ " ' ] ? \) / gi;
1658+ // Base64 expands bytes by ~33%, then immutable HTML replacements retain more
1659+ // string copies while compiling. Files up to and including 5 MiB remain inline;
1660+ // the first byte above that stays file-backed. This conservative ceiling keeps
1661+ // verified 19 MiB+ TTC collections out of the V8 heap while both local and
1662+ // distributed file servers continue serving project assets at authored paths.
1663+ const MAX_LOCAL_FONT_DATA_URI_BYTES = 5 * 1024 * 1024 ;
1664+
1665+ type LocalFontRead = { kind : "file-backed" } | { kind : "inline" ; buffer : Buffer } ;
1666+
1667+ async function readLocalFont ( absPath : string ) : Promise < LocalFontRead > {
1668+ const chunks : Buffer [ ] = [ ] ;
1669+ let totalBytes = 0 ;
1670+ for await ( const chunk of createReadStream ( absPath ) ) {
1671+ const buffer = Buffer . isBuffer ( chunk ) ? chunk : Buffer . from ( chunk ) ;
1672+ totalBytes += buffer . length ;
1673+ if ( totalBytes > MAX_LOCAL_FONT_DATA_URI_BYTES ) {
1674+ return { kind : "file-backed" } ;
1675+ }
1676+ chunks . push ( buffer ) ;
1677+ }
1678+ return { kind : "inline" , buffer : Buffer . concat ( chunks , totalBytes ) } ;
1679+ }
16581680
16591681// fallow-ignore-next-line complexity
16601682async function embedLocalFontFaces ( html : string , projectDir : string ) : Promise < string > {
@@ -1664,6 +1686,7 @@ async function embedLocalFontFaces(html: string, projectDir: string): Promise<st
16641686 let result = html ;
16651687 const embeddedPaths = new Set < string > ( ) ;
16661688 const dataUriByAbsolutePath = new Map < string , string > ( ) ;
1689+ const fileBackedAbsolutePaths = new Set < string > ( ) ;
16671690
16681691 let styleMatch : RegExpExecArray | null ;
16691692 while ( ( styleMatch = styleBlockRe . exec ( html ) ) !== null ) {
@@ -1679,16 +1702,27 @@ async function embedLocalFontFaces(html: string, projectDir: string): Promise<st
16791702 if ( ! localPath || embeddedPaths . has ( localPath ) ) continue ;
16801703 const absPath = localPath . startsWith ( "/" ) ? localPath : resolve ( projectDir , localPath ) ;
16811704 if ( ! isPathInside ( absPath , projectDir ) ) continue ;
1682- if ( ! existsSync ( absPath ) ) continue ;
16831705 const ext = absPath . match ( / \. ( w o f f 2 ? | t t f | o t f | t t c ) $ / i) ?. [ 1 ] ?. toLowerCase ( ) ?? "ttf" ;
16841706 try {
1707+ if ( fileBackedAbsolutePaths . has ( absPath ) ) {
1708+ embeddedPaths . add ( localPath ) ;
1709+ continue ;
1710+ }
16851711 let dataUri = dataUriByAbsolutePath . get ( absPath ) ;
16861712 if ( ! dataUri ) {
1687- const buffer = readFileSync ( absPath ) ;
1688- dataUri = await toDataUri ( buffer , ext ) ;
1713+ const font = await readLocalFont ( absPath ) ;
1714+ if ( font . kind === "file-backed" ) {
1715+ fileBackedAbsolutePaths . add ( absPath ) ;
1716+ defaultLogger . info (
1717+ `[Compiler] Kept large local font file-backed: ${ localPath } (> ${ ( MAX_LOCAL_FONT_DATA_URI_BYTES / 1024 / 1024 ) . toFixed ( 1 ) } MB)` ,
1718+ ) ;
1719+ embeddedPaths . add ( localPath ) ;
1720+ continue ;
1721+ }
1722+ dataUri = await toDataUri ( font . buffer , ext ) ;
16891723 dataUriByAbsolutePath . set ( absPath , dataUri ) ;
16901724 defaultLogger . info (
1691- `[Compiler] Embedded local font file: ${ localPath } (${ ( buffer . length / 1024 ) . toFixed ( 0 ) } KB → data URI)` ,
1725+ `[Compiler] Embedded local font file: ${ localPath } (${ ( font . buffer . length / 1024 ) . toFixed ( 0 ) } KB → data URI)` ,
16921726 ) ;
16931727 }
16941728 result = result . replaceAll ( localPath , dataUri ) ;
0 commit comments