11import type * as Babel from '@babel/core' ;
2- import type { types as t } from '@babel/core' ;
3- import type { NodePath } from '@babel/core' ;
2+ import type { NodePath , types as t } from '@babel/core' ;
43import { CallIdentifierExpression , CallStatementPath } from './babel-type-helpers' ;
4+ import { ImportUtil } from 'babel-import-util' ;
55
66export interface Options {
77 module : boolean | undefined ;
88 global : string | undefined ;
99 assertPredicateIndex : number | undefined ;
10- isDebug : boolean ;
10+ isDebug : boolean | "@embroider/macros" ;
1111}
1212
1313interface MacroExpressionOpts {
@@ -27,18 +27,21 @@ export default class Builder {
2727 private module : boolean | undefined ;
2828 private global : string | undefined ;
2929 private assertPredicateIndex : number | undefined ;
30- private isDebug : boolean ;
30+ private isDebug : boolean | '@embroider/macros' ;
31+ private util : ImportUtil ;
3132
3233 private expressions : [ CallStatementPath , ( debugIdentifier : t . Expression ) => t . Expression ] [ ] = [ ] ;
3334
3435 constructor (
3536 readonly t : typeof Babel . types ,
37+ util : ImportUtil ,
3638 options : Options
3739 ) {
3840 this . module = options . module ;
3941 this . global = options . global ;
4042 this . assertPredicateIndex = options . assertPredicateIndex ;
4143 this . isDebug = options . isDebug ;
44+ this . util = util ;
4245 }
4346
4447 /**
@@ -212,12 +215,24 @@ export default class Builder {
212215 */
213216 expandMacros ( ) {
214217 let t = this . t ;
215- let flag = t . booleanLiteral ( this . isDebug ) ;
216218 for ( let i = 0 ; i < this . expressions . length ; i ++ ) {
217219 let expression = this . expressions [ i ] ;
218220 let exp = expression [ 0 ] ;
221+ let flag = this . _debugExpression ( exp ) ;
219222 let logicalExp = expression [ 1 ] ;
220223 exp . replaceWith ( t . parenthesizedExpression ( logicalExp ( flag ) ) ) ;
224+ exp . scope . crawl ( ) ;
225+ }
226+ }
227+
228+ _debugExpression ( target : NodePath ) {
229+ if ( typeof this . isDebug === 'boolean' ) {
230+ return this . t . booleanLiteral ( this . isDebug ) ;
231+ } else {
232+ return this . t . callExpression (
233+ this . util . import ( target , '@embroider/macros' , 'isDevelopingApp' ) ,
234+ [ ]
235+ ) ;
221236 }
222237 }
223238
0 commit comments