import { posix as path } from 'path' ; import { OutputOptions , RollupWarning , RollupFileOptions , SourceDescription } from 'rollup' ; import * as rollupCommonjsPlugin from 'rollup-plugin-commonjs' ; import * as rollupNodeResolvePlugin from 'rollup-plugin-node-resolve' ; import * as rollupSourcemaps from 'rollup-plugin-sourcemaps' ; import { getFileNameByPackageName } from '../utilities/get-file-name-by-package-name' ; import { rollupBundlingTargets } from './rollup-bundling-targets' ; import { AngularPackageLogger } from '../logger/angular-package-logger' ; /**
 * Rollup Configuration Builder
 */ export class RollupConfigurationBuilder { /**
     * Rollup input options
     */ private readonly inputOptions : RollupFileOptions ; /**
     * Rollup output options
     */ private readonly outputOptions : OutputOptions ; /**
     * Output directory
     */ private outDir : string ; /**
     * Bundle target
     */ private target : string ; /**
     * Constructor
     */ constructor ( ) { this . inputOptions = < RollupFileOptions > { onwarn : ( warning ) : void => { if ( warning . code === 'THIS_IS_UNDEFINED' ) { return ; } const message = [ `A warning got emitted while creating the ${ this . target . toUpperCase ( ) } bundle.` , '' , `Details:    ${ warning . message }` , '' , `Caused by:  Rollup` , `Code:       ${ warning . code }` ] ; if ( warning . code === 'UNUSED_EXTERNAL_IMPORT' ) { message . push ( '' , 'Tip: Remove the unused import from your source files to get rid of this warning.' ) ; if ( warning . message . indexOf ( 'QueryList' ) !== - 1 && warning . message . indexOf ( '@angular/core' ) !== - 1 ) { message . push ( 'Note: This warning might be caused by a known bug in the Angular Compiler (see https://github.com/angular/angular/issues/21280).' ) ; } } AngularPackageLogger . logMessage ( message . join ( '\n' ) , 'warning' ) ; } , preserveSymlinks : true , plugins : [ rollupNodeResolvePlugin ( ) , rollupCommonjsPlugin ( ) , rollupSourcemaps ( ) , ( ( ) => { let processedFiles = 0 ; return { name : 'logger' , load : ( id ) => { processedFiles ++ ; const normalizedFilePath = id . toString ( ) . replace ( /\\/g , path . sep ) ; const relativeFilePath = normalizedFilePath . indexOf ( 'node_modules' ) === - 1 ? path . relative ( path . dirname ( this . inputOptions . input ) , normalizedFilePath ) : `node_modules${ normalizedFilePath . split ( 'node_modules' ) . slice ( - 1 ) [ 0 ] }` ; AngularPackageLogger . logMessage ( `Processing files (${ processedFiles }) :: ${ relativeFilePath }` ) ; } , ongenerate : ( options , source ) => { AngularPackageLogger . logMessage ( `Generate bundle :: ${ path . basename ( options . file ) }` ) ; } , onwrite : ( options , source ) => { AngularPackageLogger . logMessage ( `Write bundle :: ${ path . basename ( options . file ) }` ) ; } } ; } ) ( ) ] } ; this . outputOptions = { sourcemap : true } ; this . outDir = '' ; } param packageName returns public setPackageName ( packageName ) { this . outputOptions . name = getFileNameByPackageName ( packageName ) ; return this ; } param entryFile returns public setEntry ( entryFile ) { this . inputOptions . input = entryFile ; return this ; } param target returns public setTarget ( target ) { this . target = target ; this . outputOptions . format = rollupBundlingTargets [ target ] ; return this ; } param outDir returns public setOutDir ( outDir ) { this . outDir = outDir ; return this ; } param knownDependencies param expectedDependencies returns public setDependencies ( knownDependencies , expectedDependencies ) { const knownDependencyModules = Object . keys ( knownDependencies ) ; this . inputOptions . external = ( moduleName ) : boolean => { return this . isExternalModule ( moduleName , knownDependencyModules ) ; } ; this . outputOptions . globals = ( moduleName ) : string => { return this . getModuleGlobalName ( moduleName , knownDependencies , expectedDependencies ) ; } ; return this ; } returns public build ( ) { this . outputOptions . file = this . deriveOutFilePath ( ) ; this . outputOptions . sourcemapFile = this . deriveSourcemapFilePath ( ) ; return { inputOptions : this . inputOptions , outputOptions : this . outputOptions } ; } returns private deriveOutFilePath ( ) { const bundleSuffix = this . outputOptions . format === 'umd' ? '.umd' : '' ; const fileName = `${ this . outputOptions . name }${ bundleSuffix }.js` ; return path . join ( this . outDir , fileName ) ; } returns private deriveSourcemapFilePath ( ) { return path . join ( path . dirname ( this . inputOptions . input ) , path . basename ( this . outputOptions . file ) ) ; } param moduleName param knownDependencies returns private isExternalModule ( moduleName , knownDependencies ) { return knownDependencies . some ( ( knownDependency ) : boolean => { return moduleName === knownDependency || moduleName . startsWith ( `${ knownDependency }/` ) ; } ) ; } param moduleName param knownDependencies param expectedDependencies returns private getModuleGlobalName ( moduleName , knownDependencies , expectedDependencies ) { if ( ! ! knownDependencies [ moduleName ] ) { return knownDependencies [ moduleName ] ; } else if ( ! ! expectedDependencies [ moduleName ] ) { return expectedDependencies [ moduleName ] ; } else { return '' ; } } }