import { createStructuralDirectiveTransform , processIf , IfNode , createIfStatement , createBlockStatement , createCallExpression , IfBranchNode , BlockStatement , NodeTypes } from '@vue/compiler-dom' ; import { SSRTransformContext , processChildrenAsStatement } from '../ssrCodegenTransform' ; export const ssrTransformIf = createStructuralDirectiveTransform ( /^(if|else|else-if)$/ , processIf ) ; export function ssrProcessIf ( node , context , disableNestedFragments = false ) { const [ rootBranch ] = node . branches ; const ifStatement = createIfStatement ( rootBranch . condition ! , processIfBranch ( rootBranch , context , disableNestedFragments ) ) ; context . pushStatement ( ifStatement ) ; let currentIf = ifStatement ; for ( let i = 1 ; i < node . branches . length ; i ++ ) { const branch = node . branches [ i ] ; const branchBlockStatement = processIfBranch ( branch , context , disableNestedFragments ) ; if ( branch . condition ) { currentIf = currentIf . alternate = createIfStatement ( branch . condition , branchBlockStatement ) ; } else { currentIf . alternate = branchBlockStatement ; } } if ( ! currentIf . alternate ) { currentIf . alternate = createBlockStatement ( [ createCallExpression ( `_push` , [ '`<!---->`' ] ) ] ) ; } } function processIfBranch ( branch , context , disableNestedFragments = false ) { const { children } = branch ; const needFragmentWrapper = ! disableNestedFragments && ( children . length !== 1 || children [ 0 ] . type !== NodeTypes . ELEMENT ) && ! ( children . length === 1 && children [ 0 ] . type === NodeTypes . FOR ) ; return processChildrenAsStatement ( children , context , needFragmentWrapper ) ; }