import { posix as path } from 'path' ; import { gte } from 'semver' ; import { rollup , OutputOptions , RollupFileOptions , RollupSingleFileBuild } from 'rollup' ; import { AngularPackage } from '../angular-package' ; import { RollupConfigurationBuilder } from './rollup-configuration-builder' ; import { getInstalledDependencyVersion } from '../utilities/get-installed-dependency-version' ; import { angularDependencies } from './dependencies/angular-dependencies' ; import { rxjs6Dependencies } from './dependencies/rxjs6-dependencies' ; import { rxjs5Dependencies } from './dependencies/rxjs5-dependencies' ; import { typescriptDependencies } from './dependencies/typescript-dependencies' ; import { AngularPackageLogger } from '../logger/angular-package-logger' ; /**
 * Angular Package Bundler
 */ export class AngularPackageBundler { /**
     * Angular Package
     */ private readonly angularPackage : AngularPackage ; param angularPackage constructor ( angularPackage ) { this . angularPackage = angularPackage ; } param target returns public async bundle ( target ) { const { inputOptions , outputOptions } = await this . buildRollupConfiguration ( target ) ; try { const bundle = await rollup ( inputOptions ) ; await bundle . write ( outputOptions ) ; } catch ( error ) { this . handleRollupError ( error , target . toUpperCase ( ) , outputOptions . file ) ; } } param target returns private async buildRollupConfiguration ( target ) { const entryFileName = `${ this . angularPackage . packageName . split ( '/' ) . pop ( ) }.js` ; const entryFile = target === 'fesm2015' ? path . join ( this . angularPackage . root , this . angularPackage . outDir , 'temp' , 'esm2015' , entryFileName ) : path . join ( this . angularPackage . root , this . angularPackage . outDir , 'temp' , 'esm5' , entryFileName ) ; const outDir = target === 'umd' ? path . join ( this . angularPackage . root , this . angularPackage . outDir , 'temp' , 'bundles' ) : path . join ( this . angularPackage . root , this . angularPackage . outDir , 'temp' , target ) ; const expectedDependencies = await this . getExpectedDependencies ( ) ; return new RollupConfigurationBuilder ( ) . setPackageName ( this . angularPackage . packageName ) . setEntry ( entryFile ) . setTarget ( target ) . setOutDir ( outDir ) . setDependencies ( this . angularPackage . dependencies , expectedDependencies ) . build ( ) ; } returns private async getExpectedDependencies ( ) { const rxjsVersion = await getInstalledDependencyVersion ( 'rxjs' ) ; const rxjsDependencies = gte ( rxjsVersion , '6.0.0' ) ? rxjs6Dependencies : rxjs5Dependencies ; return { ... angularDependencies , ... rxjsDependencies , ... typescriptDependencies } ; } param error param target param file private handleRollupError ( error , target , file ) { const relativeFilePath = file . split ( '/' ) . slice ( - 2 ) . join ( '/' ) ; const errorMessage = [ `An error occured while creating the ${ target } bundle.` , '' , `Message:    ${ error . message }` , '' , 'Caused by:  Rollup' , `File:       ./${ relativeFilePath } [to be generated]` , '' , 'Tip: For known pitfalls, also see https://github.com/dominique-mueller/angular-package-builder#known-pitfalls-with-solutions' ] . join ( '\n' ) ; AngularPackageLogger . logMessage ( errorMessage , 'error' ) ; throw new Error ( errorMessage ) ; } }