@@ -27,6 +27,10 @@ import {
27
27
import isBlockFilename from '../utils/isBlockFilename' ;
28
28
import { classnamesHelper as generateClassName , HELPER_FN_NAME } from './classNameGenerator' ;
29
29
// import { TemplateAnalysisError } from '../utils/Errors';
30
+ import * as debugGenerator from 'debug' ;
31
+
32
+ const debug = debugGenerator ( 'css-blocks:jsx' ) ;
33
+
30
34
let { parse } = require ( 'path' ) ;
31
35
32
36
export interface CssBlocksVisitor {
@@ -57,6 +61,7 @@ export default function mkTransform(tranformOpts: { rewriter: Rewriter }): () =>
57
61
this . importsToRemove = new Array < NodePath < ImportDeclaration > > ( ) ;
58
62
this . statementsToRemove = new Array < NodePath < Statement > > ( ) ;
59
63
this . filename = file . opts . filename ;
64
+
60
65
this . mapping = rewriter . blocks [ this . filename ] ;
61
66
if ( this . mapping && this . mapping . analyses ) {
62
67
let a = this . mapping . analyses . find ( a => a . template . identifier === this . filename ) ;
@@ -66,13 +71,15 @@ export default function mkTransform(tranformOpts: { rewriter: Rewriter }): () =>
66
71
}
67
72
let ext = parse ( this . filename ) . ext ;
68
73
this . shouldProcess = CAN_PARSE_EXTENSIONS [ ext ] && this . analysis && this . analysis . blockCount ( ) > 0 ;
74
+
69
75
if ( this . shouldProcess ) {
76
+ debug ( `Rewriting discovered dependency ${ this . filename } ` ) ;
70
77
this . elementAnalyzer = new JSXElementAnalyzer ( this . analysis . blocks , this . filename ) ;
71
78
}
72
79
} ,
73
80
post ( state : any ) {
74
81
for ( let nodePath of this . statementsToRemove ) {
75
- if ( nodePath . removed ) continue ;
82
+ if ( nodePath . removed ) { continue ; }
76
83
nodePath . remove ( ) ;
77
84
}
78
85
if ( this . dynamicStylesFound ) {
@@ -84,6 +91,7 @@ export default function mkTransform(tranformOpts: { rewriter: Rewriter }): () =>
84
91
firstImport . replaceWith ( importDecl ) ;
85
92
}
86
93
for ( let nodePath of this . importsToRemove ) {
94
+ if ( nodePath . removed ) { continue ; }
87
95
detectStrayReferenceToImport ( nodePath , this . filename ) ;
88
96
nodePath . remove ( ) ;
89
97
}
@@ -125,8 +133,7 @@ export default function mkTransform(tranformOpts: { rewriter: Rewriter }): () =>
125
133
} ,
126
134
127
135
JSXOpeningElement ( path : NodePath < JSXOpeningElement > , state : any ) : void {
128
- if ( ! this . shouldProcess ) return ;
129
-
136
+ if ( ! this . shouldProcess ) { return ; }
130
137
let elementAnalysis = this . elementAnalyzer . analyzeJSXElement ( path ) ;
131
138
if ( elementAnalysis ) {
132
139
elementAnalysis . seal ( ) ;
@@ -177,6 +184,7 @@ function detectStrayReferenceToImport(
177
184
importDeclPath : NodePath < ImportDeclaration > ,
178
185
filename : string
179
186
) : void {
187
+ if ( ! importDeclPath || ! importDeclPath . node ) { return ; }
180
188
for ( let specifier of importDeclPath . node . specifiers ) {
181
189
let binding = importDeclPath . scope . getBinding ( specifier . local . name ) ;
182
190
if ( binding ) {
0 commit comments