@@ -241,15 +241,6 @@ module.exports = function (api) {
241241 { name : "IS_STANDALONE" , value : env === "standalone" } ,
242242 "flag-IS_STANDALONE" ,
243243 ] ,
244- [
245- pluginToggleBooleanFlag ,
246- {
247- name : "USE_ESM_OR_STANDALONE" ,
248- value : outputType === "module" || env === "standalone" ,
249- } ,
250- "flag-USE_ESM_OR_STANDALONE" ,
251- ] ,
252-
253244 [
254245 pluginToggleBooleanFlag ,
255246 {
@@ -384,7 +375,7 @@ function importInteropTest(source) {
384375
385376// env vars from the cli are always strings, so !!ENV_VAR returns true for "false"
386377function bool ( value ) {
387- return value && value !== "false" && value !== "0" ;
378+ return Boolean ( value ) && value !== "false" && value !== "0" ;
388379}
389380
390381// A minimum semver GTE implementation
@@ -507,24 +498,72 @@ function pluginPolyfillsOldNode({ template, types: t }) {
507498 * @returns {import("@babel/core").PluginObj }
508499 */
509500function pluginToggleBooleanFlag ( { types : t } , { name, value } ) {
510- function check ( test ) {
511- let keepConsequent = value ;
501+ if ( typeof value !== "boolean" ) throw new Error ( `.value must be a boolean` ) ;
502+
503+ function evaluate ( test ) {
504+ const res = {
505+ replace : replacement => ( { replacement, value : null , unrelated : false } ) ,
506+ value : value => ( { replacement : null , value, unrelated : false } ) ,
507+ unrelated : ( ) => ( {
508+ replacement : test . node ,
509+ value : null ,
510+ unrelated : true ,
511+ } ) ,
512+ } ;
513+
514+ if ( test . isIdentifier ( { name } ) || test . matchesPattern ( name ) ) {
515+ return res . value ( value ) ;
516+ }
512517
513518 if ( test . isUnaryExpression ( { operator : "!" } ) ) {
514- test = test . get ( "argument" ) ;
515- keepConsequent = ! keepConsequent ;
519+ const arg = evaluate ( test . get ( "argument" ) ) ;
520+ return arg . unrelated
521+ ? res . unrelated ( )
522+ : arg . replacement
523+ ? res . replacement ( t . unaryExpression ( "!" , arg . replacement ) )
524+ : res . value ( ! arg . value ) ;
516525 }
517- return {
518- test,
519- keepConsequent,
520- } ;
526+
527+ if ( test . isLogicalExpression ( { operator : "||" } ) ) {
528+ const left = evaluate ( test . get ( "left" ) ) ;
529+ const right = evaluate ( test . get ( "right" ) ) ;
530+
531+ if ( left . value === true || right . value === true ) return res . value ( true ) ;
532+ if ( left . value === false && right . value === false ) {
533+ return res . value ( false ) ;
534+ }
535+ if ( left . value === false ) return res . replace ( right . replacement ) ;
536+ if ( right . value === false ) return res . replace ( left . replacement ) ;
537+ if ( left . unrelated && right . unrelated ) return res . unrelated ( ) ;
538+ console . log ( left , right ) ;
539+ return res . replace (
540+ t . logicalExpression ( "||" , left . replacement , right . replacement )
541+ ) ;
542+ }
543+
544+ if ( test . isLogicalExpression ( { operator : "&&" } ) ) {
545+ const left = evaluate ( test . get ( "left" ) ) ;
546+ const right = evaluate ( test . get ( "right" ) ) ;
547+
548+ if ( left . value === true && right . value === true ) return res . value ( true ) ;
549+ if ( left . value === false || right . value === false ) {
550+ return res . value ( false ) ;
551+ }
552+ if ( left . value === true ) return res . replace ( right . replacement ) ;
553+ if ( right . value === true ) return res . replace ( left . replacement ) ;
554+ if ( left . unrelated && right . unrelated ) return res . unrelated ( ) ;
555+ return res . replace (
556+ t . logicalExpression ( "&&" , left . replacement , right . replacement )
557+ ) ;
558+ }
559+
560+ return res . unrelated ( ) ;
521561 }
522562
523563 return {
524564 visitor : {
525565 "IfStatement|ConditionalExpression" ( path ) {
526- // eslint-disable-next-line prefer-const
527- let { test, keepConsequent } = check ( path . get ( "test" ) ) ;
566+ let test = path . get ( "test" ) ;
528567
529568 // yarn-plugin-conditions injects bool(process.env.BABEL_8_BREAKING)
530569 // tests, to properly cast the env variable to a boolean.
@@ -536,32 +575,26 @@ function pluginToggleBooleanFlag({ types: t }, { name, value }) {
536575 test = test . get ( "arguments" ) [ 0 ] ;
537576 }
538577
539- if ( ! test . isIdentifier ( { name } ) && ! test . matchesPattern ( name ) ) return ;
578+ const res = evaluate ( test ) ;
540579
541- path . replaceWith (
542- keepConsequent
543- ? path . node . consequent
544- : path . node . alternate || t . emptyStatement ( )
545- ) ;
580+ if ( res . unrelated ) return ;
581+ if ( res . replacement ) {
582+ path . get ( "test" ) . replaceWith ( res . replacement ) ;
583+ } else {
584+ path . replaceWith (
585+ res . value
586+ ? path . node . consequent
587+ : path . node . alternate || t . emptyStatement ( )
588+ ) ;
589+ }
546590 } ,
547591 LogicalExpression ( path ) {
548- const { test, keepConsequent } = check ( path . get ( "left" ) ) ;
549-
550- if ( ! test . matchesPattern ( name ) ) return ;
551-
552- switch ( path . node . operator ) {
553- case "&&" :
554- path . replaceWith (
555- keepConsequent ? path . node . right : t . booleanLiteral ( false )
556- ) ;
557- break ;
558- case "||" :
559- path . replaceWith (
560- keepConsequent ? t . booleanLiteral ( true ) : path . node . right
561- ) ;
562- break ;
563- default :
564- throw path . buildCodeFrameError ( "This check could not be stripped." ) ;
592+ const res = evaluate ( path . get ( "test" ) ) ;
593+ if ( res . unrelated ) return ;
594+ if ( res . replacement ) {
595+ path . get ( "test" ) . replaceWith ( res . replacement ) ;
596+ } else {
597+ path . replaceWith ( t . booleanLiteral ( res . value ) ) ;
565598 }
566599 } ,
567600 MemberExpression ( path ) {
0 commit comments