import type * as d from '../../declarations' ; import { buildAbort , buildFinish } from './build-finish' ; import { catchError , isString , readPackageJson } from '@utils' ; import { createDocument } from '@stencil/core/mock-doc' ; import { emptyOutputTargets } from '../output-targets/empty-dir' ; import { generateGlobalStyles } from '../style/global-styles' ; import { generateOutputTargets } from '../output-targets' ; import { runTsProgram } from '../transpile/run-program' ; import { writeBuild } from './write-build' ; import ts from 'typescript' ; export const build = async ( config , compilerCtx , buildCtx , tsBuilder ) => { try { process . chdir ( config . rootDir ) ; await emptyOutputTargets ( config , compilerCtx , buildCtx ) ; if ( buildCtx . hasError ) return buildAbort ( buildCtx ) ; if ( config . srcIndexHtml ) { const indexSrcHtml = await compilerCtx . fs . readFile ( config . srcIndexHtml ) ; if ( isString ( indexSrcHtml ) ) { buildCtx . indexDoc = createDocument ( indexSrcHtml ) ; } } await readPackageJson ( config , compilerCtx , buildCtx ) ; if ( buildCtx . hasError ) return buildAbort ( buildCtx ) ; const tsTimeSpan = buildCtx . createTimeSpan ( 'transpile started' ) ; const componentDtsChanged = await runTsProgram ( config , compilerCtx , buildCtx , tsBuilder ) ; tsTimeSpan . finish ( 'transpile finished' ) ; if ( buildCtx . hasError ) return buildAbort ( buildCtx ) ; if ( config . watch && componentDtsChanged ) { return null ; } buildCtx . stylesPromise = generateGlobalStyles ( config , compilerCtx , buildCtx ) ; if ( buildCtx . hasError ) return buildAbort ( buildCtx ) ; await generateOutputTargets ( config , compilerCtx , buildCtx ) ; if ( buildCtx . hasError ) return buildAbort ( buildCtx ) ; await buildCtx . stylesPromise ; await writeBuild ( config , compilerCtx , buildCtx ) ; } catch ( e ) { catchError ( buildCtx . diagnostics , e ) ; } compilerCtx . changedFiles . clear ( ) ; return buildFinish ( buildCtx ) ; } ;