@@ -27,6 +27,10 @@ import {
2727import isBlockFilename from '../utils/isBlockFilename' ;
2828import { classnamesHelper as generateClassName , HELPER_FN_NAME } from './classNameGenerator' ;
2929// import { TemplateAnalysisError } from '../utils/Errors';
30+ import * as debugGenerator from 'debug' ;
31+
32+ const debug = debugGenerator ( 'css-blocks:jsx' ) ;
33+
3034let { parse } = require ( 'path' ) ;
3135
3236export interface CssBlocksVisitor {
@@ -57,6 +61,7 @@ export default function mkTransform(tranformOpts: { rewriter: Rewriter }): () =>
5761 this . importsToRemove = new Array < NodePath < ImportDeclaration > > ( ) ;
5862 this . statementsToRemove = new Array < NodePath < Statement > > ( ) ;
5963 this . filename = file . opts . filename ;
64+
6065 this . mapping = rewriter . blocks [ this . filename ] ;
6166 if ( this . mapping && this . mapping . analyses ) {
6267 let a = this . mapping . analyses . find ( a => a . template . identifier === this . filename ) ;
@@ -66,13 +71,15 @@ export default function mkTransform(tranformOpts: { rewriter: Rewriter }): () =>
6671 }
6772 let ext = parse ( this . filename ) . ext ;
6873 this . shouldProcess = CAN_PARSE_EXTENSIONS [ ext ] && this . analysis && this . analysis . blockCount ( ) > 0 ;
74+
6975 if ( this . shouldProcess ) {
76+ debug ( `Rewriting discovered dependency ${ this . filename } ` ) ;
7077 this . elementAnalyzer = new JSXElementAnalyzer ( this . analysis . blocks , this . filename ) ;
7178 }
7279 } ,
7380 post ( state : any ) {
7481 for ( let nodePath of this . statementsToRemove ) {
75- if ( nodePath . removed ) continue ;
82+ if ( nodePath . removed ) { continue ; }
7683 nodePath . remove ( ) ;
7784 }
7885 if ( this . dynamicStylesFound ) {
@@ -84,6 +91,7 @@ export default function mkTransform(tranformOpts: { rewriter: Rewriter }): () =>
8491 firstImport . replaceWith ( importDecl ) ;
8592 }
8693 for ( let nodePath of this . importsToRemove ) {
94+ if ( nodePath . removed ) { continue ; }
8795 detectStrayReferenceToImport ( nodePath , this . filename ) ;
8896 nodePath . remove ( ) ;
8997 }
@@ -125,8 +133,7 @@ export default function mkTransform(tranformOpts: { rewriter: Rewriter }): () =>
125133 } ,
126134
127135 JSXOpeningElement ( path : NodePath < JSXOpeningElement > , state : any ) : void {
128- if ( ! this . shouldProcess ) return ;
129-
136+ if ( ! this . shouldProcess ) { return ; }
130137 let elementAnalysis = this . elementAnalyzer . analyzeJSXElement ( path ) ;
131138 if ( elementAnalysis ) {
132139 elementAnalysis . seal ( ) ;
@@ -177,6 +184,7 @@ function detectStrayReferenceToImport(
177184 importDeclPath : NodePath < ImportDeclaration > ,
178185 filename : string
179186) : void {
187+ if ( ! importDeclPath || ! importDeclPath . node ) { return ; }
180188 for ( let specifier of importDeclPath . node . specifiers ) {
181189 let binding = importDeclPath . scope . getBinding ( specifier . local . name ) ;
182190 if ( binding ) {
0 commit comments