1- import {
2- Analysis ,
3- Analyzer ,
4- Block ,
5- } from "@css-blocks/core" ;
1+ import { Analysis , Analyzer , Block } from "@css-blocks/core" ;
62import { TemplateIntegrationOptions } from "@opticss/template-api" ;
73import { some , unwrap } from "@opticss/util" ;
84import traverse from "babel-traverse" ;
95import * as babylon from "babylon" ;
106import * as debugGenerator from "debug" ;
117import * as fs from "fs-extra" ;
128import * as path from "path" ;
9+ import { deprecate } from "util" ;
1310
14- import { CssBlocksJSXOptions } from "../options" ;
11+ import { JSXOptions , JSXOptionsReader } from "../options" ;
1512import { JSXParseError } from "../utils/Errors" ;
1613
1714import { JSXTemplate , TEMPLATE_TYPE } from "./Template" ;
1815import { elementVisitor , importVisitor } from "./visitors" ;
1916
20- const debug = debugGenerator ( "css-blocks:jsx:Analyzer" ) ;
21-
2217export type JSXAnalysis = Analysis < TEMPLATE_TYPE > ;
2318
19+ export interface AnalyzerOptions {
20+ /** A name that is used (if provided) in logging to distinguish output of different analyzers in a build. */
21+ analyzerName ?: string ;
22+ }
23+
24+ const deprecatedName = deprecate (
25+ ( name : string , options : JSXOptions & AnalyzerOptions ) => {
26+ options . analyzerName = name ;
27+ } ,
28+ "The name parameter of the JSX Analyzer is deprecated and usually unnecessary.\n" +
29+ "Pass only options and set the analyzerName option there if you really need it (you really don't)." ,
30+ ) ;
31+
2432export class CSSBlocksJSXAnalyzer extends Analyzer < TEMPLATE_TYPE > {
25- private options : CssBlocksJSXOptions ;
33+ private options : JSXOptionsReader ;
2634
27- public name : string ;
35+ public name ? : string ;
2836 public analysisPromises : Map < string , Promise < JSXAnalysis > > ;
2937 public blockPromises : Map < string , Promise < Block > > ;
38+ private debug : debugGenerator . IDebugger ;
3039
31- constructor ( name : string , options : Partial < CssBlocksJSXOptions > = { } ) {
32- let opts = new CssBlocksJSXOptions ( options ) ;
33- super ( opts . compilationOptions ) ;
34- this . name = name ;
35- this . options = opts ;
40+ constructor ( options : JSXOptions & AnalyzerOptions ) ;
41+ /**
42+ * @deprecated Use the single argument constructor.
43+ * @param name Deprecated. Pass the analyzerName option instead;
44+ */
45+ constructor ( name : string | JSXOptions & AnalyzerOptions , options : JSXOptions & AnalyzerOptions = { } ) {
46+ // ewww need to get rid of this deprecation soon.
47+ super ( options && options . compilationOptions || ( name && typeof name !== "string" && name . compilationOptions ) || { } ) ;
48+ if ( typeof name === "string" ) {
49+ deprecatedName ( name , options ) ;
50+ } else {
51+ options = name ;
52+ }
53+ this . name = options . analyzerName ;
54+ this . options = new JSXOptionsReader ( options ) ;
3655 this . analysisPromises = new Map ( ) ;
3756 this . blockPromises = new Map ( ) ;
57+ let debugIdent = "css-blocks:jsx:Analyzer" ;
58+ if ( this . name ) {
59+ debugIdent += `:${ this . name } ` ;
60+ }
61+ this . debug = debugGenerator ( debugIdent ) ;
3862 }
3963
4064 public reset ( ) {
@@ -67,7 +91,7 @@ export class CSSBlocksJSXAnalyzer extends Analyzer<TEMPLATE_TYPE> {
6791 promises . push ( this . parseFile ( path . join ( dir , entryPoint ) ) ) ;
6892 }
6993 await Promise . all ( promises ) ;
70- debug ( `Found ${ this . analysisPromises . size } analysis promises` ) ;
94+ this . debug ( `Found ${ this . analysisPromises . size } analysis promises` ) ;
7195 return this ;
7296 }
7397
@@ -105,11 +129,11 @@ export class CSSBlocksJSXAnalyzer extends Analyzer<TEMPLATE_TYPE> {
105129 process . chdir ( oldDir ) ;
106130
107131 // Wait for all block promises to resolve then resolve with the finished analysis.
108- debug ( `Waiting for ${ blockPromises . length } Block imported by "${ template . identifier } " to finish compilation.` ) ;
132+ this . debug ( `Waiting for ${ blockPromises . length } Block imported by "${ template . identifier } " to finish compilation.` ) ;
109133 await Promise . all ( blockPromises ) ;
110- debug ( `Waiting for ${ childTemplatePromises . length } child templates to finish analysis before analysis of ${ template . identifier } .` ) ;
134+ this . debug ( `Waiting for ${ childTemplatePromises . length } child templates to finish analysis before analysis of ${ template . identifier } .` ) ;
111135 await Promise . all ( childTemplatePromises ) ;
112- debug ( `All child compilations finished for "${ template . identifier } ".` ) ;
136+ this . debug ( `All child compilations finished for "${ template . identifier } ".` ) ;
113137 return analysis ;
114138
115139 }
@@ -123,11 +147,11 @@ export class CSSBlocksJSXAnalyzer extends Analyzer<TEMPLATE_TYPE> {
123147
124148 let template : JSXTemplate = new JSXTemplate ( filename , data ) ;
125149
126- debug ( `Beginning imports crawl of ${ filename } .` ) ;
150+ this . debug ( `Beginning imports crawl of ${ filename } .` ) ;
127151 let analysisPromise = this . crawl ( template ) ;
128152 this . analysisPromises . set ( template . identifier , analysisPromise ) ;
129153 let analysis = await analysisPromise ;
130- debug ( `Finished imports crawl of ${ filename } .` ) ;
154+ this . debug ( `Finished imports crawl of ${ filename } .` ) ;
131155
132156 traverse ( unwrap ( analysis . template . ast ) , elementVisitor ( analysis ) ) ;
133157 // No need to keep detailed template data anymore!
0 commit comments