import type * as d from '../../../declarations' ; import { catchError , COLLECTION_MANIFEST_FILE_NAME , flatOne , generatePreamble , normalizePath , sortBy } from '@utils' ; import { isOutputTargetDistCollection } from '../output-utils' ; import { join , relative } from 'path' ; import { typescriptVersion , version } from '../../../version' ; export const outputCollection = async ( config , compilerCtx , buildCtx , changedModuleFiles ) : Promise < void > => { const outputTargets = config . outputTargets . filter ( isOutputTargetDistCollection ) ; if ( outputTargets . length === 0 ) { return ; } const bundlingEventMessage = `generate collections${ config . sourceMap ? ' + source maps' : '' }` ; const timespan = buildCtx . createTimeSpan ( `${ bundlingEventMessage } started` , true ) ; try { await Promise . all ( changedModuleFiles . map ( async ( mod ) => { let code = mod . staticSourceFileText ; if ( config . preamble ) { code = `${ generatePreamble ( config ) }\n${ code }` ; } const mapCode = mod . sourceMapFileText ; await Promise . all ( outputTargets . map ( async ( o ) => { const relPath = relative ( config . srcDir , mod . jsFilePath ) ; const filePath = join ( o . collectionDir , relPath ) ; await compilerCtx . fs . writeFile ( filePath , code , { outputTargetType : o . type } ) ; if ( mod . sourceMapPath ) { const relativeSourceMapPath = relative ( config . srcDir , mod . sourceMapPath ) ; const sourceMapOutputFilePath = join ( o . collectionDir , relativeSourceMapPath ) ; await compilerCtx . fs . writeFile ( sourceMapOutputFilePath , mapCode , { outputTargetType : o . type } ) ; } } ) ) ; } ) ) ; await writeCollectionManifests ( config , compilerCtx , buildCtx , outputTargets ) ; } catch ( e ) { catchError ( buildCtx . diagnostics , e ) ; } timespan . finish ( `${ bundlingEventMessage } finished` ) ; } ; const writeCollectionManifests = async ( config , compilerCtx , buildCtx , outputTargets ) => { const collectionData = JSON . stringify ( serializeCollectionManifest ( config , compilerCtx , buildCtx ) , null , 2 ) ; return Promise . all ( outputTargets . map ( ( o ) => writeCollectionManifest ( compilerCtx , collectionData , o ) ) ) ; } ; const writeCollectionManifest = async ( compilerCtx , collectionData , outputTarget ) => { const collectionDir = normalizePath ( outputTarget . collectionDir ) ; const collectionFilePath = normalizePath ( join ( collectionDir , COLLECTION_MANIFEST_FILE_NAME ) ) ; await compilerCtx . fs . writeFile ( collectionFilePath , collectionData ) ; } ; const serializeCollectionManifest = ( config , compilerCtx , buildCtx ) => { const collectionManifest = { entries : buildCtx . moduleFiles . filter ( ( mod ) => ! mod . isCollectionDependency && mod . cmps . length > 0 ) . map ( ( mod ) => relative ( config . srcDir , mod . jsFilePath ) ) , compiler : { name : '@stencil/core' , version , typescriptVersion , } , collections : serializeCollectionDependencies ( compilerCtx ) , bundles : config . bundles . map ( ( b ) => ( { components : b . components . slice ( ) . sort ( ) , } ) ) , } ; if ( config . globalScript ) { const mod = compilerCtx . moduleMap . get ( normalizePath ( config . globalScript ) ) ; if ( mod ) { collectionManifest . global = relative ( config . srcDir , mod . jsFilePath ) ; } } return collectionManifest ; } ; const serializeCollectionDependencies = ( compilerCtx ) : d . CollectionDependencyData [ ] => { const collectionDeps = compilerCtx . collections . map ( ( c ) => ( { name : c . collectionName , tags : flatOne ( c . moduleFiles . map ( ( m ) => m . cmps ) ) . map ( ( cmp ) => cmp . tagName ) . sort ( ) , } ) ) ; return sortBy ( collectionDeps , ( item ) => item . name ) ; } ;