import type * as d from '../../../declarations' ; import { DIST_HYDRATE_SCRIPT , isOutputTargetDist , isOutputTargetHydrate , isOutputTargetWww , } from '../../output-targets/output-utils' ; import { isBoolean , isString } from '@utils' ; import { isAbsolute , join } from 'path' ; import { NODE_BUILTINS } from '../../sys/modules' ; export const validateHydrateScript = ( config , userOutputs ) => { const output = [ ] ; const hasHydrateOutputTarget = userOutputs . some ( isOutputTargetHydrate ) ; if ( ! hasHydrateOutputTarget ) { const hasWwwOutput = userOutputs . filter ( isOutputTargetWww ) . some ( ( o ) => isString ( o . indexHtml ) ) ; const shouldBuildHydrate = config ?. flags . prerender || config ?. flags . ssr ; if ( hasWwwOutput && shouldBuildHydrate ) { let hydrateDir ; const distOutput = userOutputs . find ( isOutputTargetDist ) ; if ( distOutput != null && isString ( distOutput . dir ) ) { hydrateDir = join ( distOutput . dir , 'hydrate' ) ; } else { hydrateDir = 'dist/hydrate' ; } const hydrateForWwwOutputTarget = { type : DIST_HYDRATE_SCRIPT , dir : hydrateDir , } ; userOutputs . push ( hydrateForWwwOutputTarget ) ; } } const hydrateOutputTargets = userOutputs . filter ( isOutputTargetHydrate ) ; hydrateOutputTargets . forEach ( ( outputTarget ) => { if ( ! isString ( outputTarget . dir ) ) { outputTarget . dir = 'hydrate' ; } if ( ! isAbsolute ( outputTarget . dir ) ) { outputTarget . dir = join ( config . rootDir , outputTarget . dir ) ; } if ( ! isBoolean ( outputTarget . empty ) ) { outputTarget . empty = true ; } outputTarget . external = outputTarget . external || [ ] ; outputTarget . external . push ( ... NODE_BUILTINS ) ; output . push ( outputTarget ) ; } ) ; return output ; } ;