import { postcss } from "opticss" ; import { BlockCompiler } from "../../src/BlockCompiler" ; import { BlockFactory } from "../../src/BlockParser" ; import { Configuration , OutputMode } from "../../src/configuration" ; import { Options , ResolvedConfiguration , resolveConfiguration } from "../../src/configuration" ; import * as errors from "../../src/errors" ; interface MockConfigOpts { dfnFiles ? : string [ ] ; } /**
 * CSS Blocks PostCSS plugin.
 */ class Plugin { private config : ResolvedConfiguration ; private postcss : typeof postcss ; private mockConfigOpts : MockConfigOpts ; param postcssImpl param opts constructor ( postcssImpl , opts ? , mockConfigOpts ? ) { this . config = resolveConfiguration ( opts ) ; this . postcss = postcssImpl ; this . mockConfigOpts = mockConfigOpts || { } ; } param root param result public process ( root , result ) { let sourceFile ; if ( result && result . opts && result . opts . from ) { sourceFile = result . opts . from ; } else { throw new errors . MissingSourcePath ( ) ; } let identifier = this . config . importer . identifier ( null , sourceFile , this . config ) ; let defaultName = this . config . importer . defaultName ( identifier , this . config ) ; let factory = new BlockFactory ( this . config , this . postcss ) ; const isDfnFile = this . mockConfigOpts . dfnFiles ? this . mockConfigOpts . dfnFiles . includes ( identifier ) : false ; return factory . parseRoot ( root , sourceFile , defaultName , isDfnFile ) . then ( ( block ) => { let compiler = new BlockCompiler ( postcss , this . config ) ; return compiler . compile ( block , root , new Set ( ) ) ; } ) ; } } type temp = { ( postcssImpl ) : ( config ? : Partial < Readonly < Configuration > > , mockConfigOpts ? : MockConfigOpts ) => postcss . Plugin < Partial < Readonly < Configuration > > > ; OutputMode : typeof OutputMode ; CssBlockError : typeof errors . CssBlockError ; InvalidBlockSyntax : typeof errors . InvalidBlockSyntax ; MissingSourcePath : typeof errors . MissingSourcePath ; } ; function makeApi ( ) { let cssBlocks ; cssBlocks = < temp > function ( postcssImpl ) { return ( config ? , mockConfigOpts ? ) => { let plugin = new Plugin ( postcssImpl , config , mockConfigOpts ) ; return plugin . process . bind ( plugin ) ; } ; } ; cssBlocks . OutputMode = OutputMode ; cssBlocks . CssBlockError = errors . CssBlockError ; cssBlocks . InvalidBlockSyntax = errors . InvalidBlockSyntax ; cssBlocks . MissingSourcePath = errors . MissingSourcePath ; return cssBlocks ; } export = makeApi ( ) ;