1
- import {
2
- Analysis ,
3
- Analyzer ,
4
- Block ,
5
- } from "@css-blocks/core" ;
1
+ import { Analysis , Analyzer , Block } from "@css-blocks/core" ;
6
2
import { TemplateIntegrationOptions } from "@opticss/template-api" ;
7
3
import { some , unwrap } from "@opticss/util" ;
8
4
import traverse from "babel-traverse" ;
9
5
import * as babylon from "babylon" ;
10
6
import * as debugGenerator from "debug" ;
11
7
import * as fs from "fs-extra" ;
12
8
import * as path from "path" ;
9
+ import { deprecate } from "util" ;
13
10
14
- import { CssBlocksJSXOptions } from "../options" ;
11
+ import { JSXOptions , JSXOptionsReader } from "../options" ;
15
12
import { JSXParseError } from "../utils/Errors" ;
16
13
17
14
import { JSXTemplate , TEMPLATE_TYPE } from "./Template" ;
18
15
import { elementVisitor , importVisitor } from "./visitors" ;
19
16
20
- const debug = debugGenerator ( "css-blocks:jsx:Analyzer" ) ;
21
-
22
17
export type JSXAnalysis = Analysis < TEMPLATE_TYPE > ;
23
18
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
+
24
32
export class CSSBlocksJSXAnalyzer extends Analyzer < TEMPLATE_TYPE > {
25
- private options : CssBlocksJSXOptions ;
33
+ private options : JSXOptionsReader ;
26
34
27
- public name : string ;
35
+ public name ? : string ;
28
36
public analysisPromises : Map < string , Promise < JSXAnalysis > > ;
29
37
public blockPromises : Map < string , Promise < Block > > ;
38
+ private debug : debugGenerator . IDebugger ;
30
39
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 ) ;
36
55
this . analysisPromises = new Map ( ) ;
37
56
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 ) ;
38
62
}
39
63
40
64
public reset ( ) {
@@ -67,7 +91,7 @@ export class CSSBlocksJSXAnalyzer extends Analyzer<TEMPLATE_TYPE> {
67
91
promises . push ( this . parseFile ( path . join ( dir , entryPoint ) ) ) ;
68
92
}
69
93
await Promise . all ( promises ) ;
70
- debug ( `Found ${ this . analysisPromises . size } analysis promises` ) ;
94
+ this . debug ( `Found ${ this . analysisPromises . size } analysis promises` ) ;
71
95
return this ;
72
96
}
73
97
@@ -105,11 +129,11 @@ export class CSSBlocksJSXAnalyzer extends Analyzer<TEMPLATE_TYPE> {
105
129
process . chdir ( oldDir ) ;
106
130
107
131
// 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.` ) ;
109
133
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 } .` ) ;
111
135
await Promise . all ( childTemplatePromises ) ;
112
- debug ( `All child compilations finished for "${ template . identifier } ".` ) ;
136
+ this . debug ( `All child compilations finished for "${ template . identifier } ".` ) ;
113
137
return analysis ;
114
138
115
139
}
@@ -123,11 +147,11 @@ export class CSSBlocksJSXAnalyzer extends Analyzer<TEMPLATE_TYPE> {
123
147
124
148
let template : JSXTemplate = new JSXTemplate ( filename , data ) ;
125
149
126
- debug ( `Beginning imports crawl of ${ filename } .` ) ;
150
+ this . debug ( `Beginning imports crawl of ${ filename } .` ) ;
127
151
let analysisPromise = this . crawl ( template ) ;
128
152
this . analysisPromises . set ( template . identifier , analysisPromise ) ;
129
153
let analysis = await analysisPromise ;
130
- debug ( `Finished imports crawl of ${ filename } .` ) ;
154
+ this . debug ( `Finished imports crawl of ${ filename } .` ) ;
131
155
132
156
traverse ( unwrap ( analysis . template . ast ) , elementVisitor ( analysis ) ) ;
133
157
// No need to keep detailed template data anymore!
0 commit comments