import { Transform } from 'stream' ; import { isReact } from '../config' ; import { criticalStylesToString , extractAllUnmatchableAsString } from '../getCSS' ; import { CacheLine , StyleDefinition } from '../types' ; import { assertIsReady } from '../utils/async' ; import { createLine , createUsedFilter } from '../utils/cache' ; import { findLastBrace } from '../utils/string' ; export const process = ( chunk , line , callback ) : string => isReact ( ) ? processReact ( chunk , line , callback ) : processPlain ( chunk , line , callback ) ; export const processPlain = ( chunk , line , callback ) : string => { const data = line . tail + chunk ; const lastBrace = findLastBrace ( data ) ; const usedString = data . substring ( 0 , lastBrace ) ; callback ( usedString ) ; line . tail = data . substring ( lastBrace ) ; return usedString ; } ; export const processReact = ( chunk , _line , callback ) : string => { callback ( chunk ) ; return chunk ; } ; export const createCriticalStyleStream = ( def ) => { const line = createLine ( ) ; let injections = [ ] ; const filter = createUsedFilter ( ) ; const styleCallback = ( content ) => { const style = criticalStylesToString ( content , def , filter ) ; style && injections . push ( style ) ; } ; let tick = 0 ; return new Transform ( { transform ( chunk , _ , _callback ) { assertIsReady ( def ) ; injections = [ ] ; if ( tick === 0 ) { const staticStyles = extractAllUnmatchableAsString ( def ) ; staticStyles && injections . push ( staticStyles ) ; } tick ++ ; const chunkData = Buffer . from ( process ( chunk . toString ( 'utf-8' ) , line , styleCallback ) , 'utf-8' ) ; _callback ( undefined , injections . join ( '' ) + chunkData ) ; } , flush ( flushCallback ) { flushCallback ( undefined , line . tail ) ; } , } ) ; } ;