import { BlockFactory , resolveConfiguration } from "@css-blocks/core" ; import { POSITION_UNKNOWN , } from "@opticss/element-analysis" ; import { SerializedTemplateInfo , TemplateInfo , TemplateInfoFactory , } from "@opticss/template-api" ; import { postcss } from "opticss" ; import * as path from "path" ; import { Configuration as WebpackConfiguration } from "webpack" ; import * as merge from "webpack-merge" ; import { CssAssets , CssBlocksPlugin } from "../../src/index" ; import { TestAnalyzer } from "../util/TestAnalyzer" ; import { BLOCK_FIXTURES_DIRECTORY } from "../util/testPaths" ; import { config as defaultOutputConfig } from "./defaultOutputConfig" ; declare module "@opticss/template-api" { interface TemplateTypes { "WebpackPlugin.TestTemplate" : TestTemplateInfo ; } } export class TestTemplateInfo implements TemplateInfo < "WebpackPlugin.TestTemplate" > { type : "WebpackPlugin.TestTemplate" ; identifier : string ; index : number ; constructor ( identifier , index ) { this . type = "WebpackPlugin.TestTemplate" ; this . identifier = identifier ; this . index = index ; } serialize ( ) { return { type : this . type , identifier : this . identifier , data : [ this . index ] , } ; } static deserialize ( identifier , index ) { return new TestTemplateInfo ( identifier , < number > index ) ; } } TemplateInfoFactory . constructors [ "WebpackPlugin.TestTemplate" ] = TestTemplateInfo . deserialize ; function fixture ( name ) { return path . resolve ( BLOCK_FIXTURES_DIRECTORY , name + ".block.css" ) ; } export function config ( ) { let blockOpts = resolveConfiguration ( { } ) ; let factory = new BlockFactory ( blockOpts , postcss ) ; let block1 = factory . getBlock ( fixture ( "concat-1" ) ) ; let block2 = factory . getBlock ( fixture ( "concat-2" ) ) ; return Promise . all ( [ block1 , block2 ] ) . then ( blocks => { let analyzer = new TestAnalyzer ( new BlockFactory ( blockOpts ) ) ; analyzer . newAnalysis ( new TestTemplateInfo ( "test.html" , 1 ) ) ; blocks . forEach ( ( b , i ) => { analyzer . eachAnalysis ( a => { a . addBlock ( `concat-${ i }` , b ) ; let el = a . startElement ( POSITION_UNKNOWN ) ; el . addStaticClass ( b . rootClass ) ; a . endElement ( el ) ; } ) ; } ) ; let cssBlocks = new CssBlocksPlugin ( { outputCssFile : "css-blocks.css" , analyzer , } ) ; let config = { entry : "./test/fixtures/javascripts/foo.js" , output : { filename : "bundle.template.js" , } , module : { rules : [ { test : /\.css$/ , exclude : /\.block\.css$/ , use : { loader : "css-loader" } , } , ] , } , plugins : [ cssBlocks , new CssAssets ( { emitSourceMaps : true , inlineSourceMaps : false , } ) , ] , } ; return merge ( defaultOutputConfig ( ) as any , config as any ) as any ; } ) ; }