11import * as fs from 'fs-extra' ;
22import * as path from 'path' ;
33import * as webpack from 'webpack' ;
4+ import { red } from 'chalk' ;
45
56import { getAppFromConfig } from '../utilities/app-utils' ;
67import { BuildTaskOptions } from '../commands/build' ;
@@ -17,9 +18,9 @@ export default Task.extend({
1718 run : function ( runTaskOptions : BuildTaskOptions ) {
1819 const config = CliConfig . fromProject ( ) . config ;
1920
20- const app = getAppFromConfig ( runTaskOptions . app ) ;
21+ const appConfig = getAppFromConfig ( runTaskOptions . app ) ;
2122
22- const outputPath = runTaskOptions . outputPath || app . outDir ;
23+ const outputPath = runTaskOptions . outputPath || appConfig . outDir ;
2324 if ( this . project . root === path . resolve ( outputPath ) ) {
2425 throw new SilentError ( 'Output path MUST not be project root directory!' ) ;
2526 }
@@ -30,7 +31,7 @@ export default Task.extend({
3031 fs . removeSync ( path . resolve ( this . project . root , outputPath ) ) ;
3132 }
3233
33- const webpackConfig = new NgCliWebpackConfig ( runTaskOptions , app ) . buildConfig ( ) ;
34+ const webpackConfig = new NgCliWebpackConfig ( runTaskOptions , appConfig ) . buildConfig ( ) ;
3435 const webpackCompiler = webpack ( webpackConfig ) ;
3536 const statsConfig = getWebpackStatsConfig ( runTaskOptions . verbose ) ;
3637
@@ -47,6 +48,24 @@ export default Task.extend({
4748 this . ui . writeLine ( statsToString ( json , statsConfig ) ) ;
4849 }
4950
51+ if ( runTaskOptions . target === 'production' && ! ! appConfig . budgets ) {
52+ let budgetErrors : string [ ] = [ ] ;
53+ appConfig . budgets . forEach ( ( budget : { name : string ; budget : number } ) => {
54+ const chunk = json . chunks . filter ( ( c : { names : string [ ] ; } ) => c . names . indexOf ( budget . name ) !== - 1 ) [ 0 ] ;
55+ const asset = json . assets . filter ( ( x : any ) => x . name == chunk . files [ 0 ] ) [ 0 ] ;
56+ const size = asset . size / 1000 ;
57+ if ( size > budget . budget ) {
58+ budgetErrors . push ( `${ budget . name } budget: ${ budget . budget } kB size: ${ size . toPrecision ( 3 ) } kB` ) ;
59+ }
60+ } ) ;
61+ if ( budgetErrors . length > 0 ) {
62+ const budgetError = 'Allowed bundle budgets have been exceeded' ;
63+ this . ui . writeLine ( red ( `\n...${ budgetError } ` ) )
64+ budgetErrors . forEach ( err => this . ui . writeLine ( red ( ` ${ err } ` ) ) ) ;
65+ reject ( budgetError ) ;
66+ }
67+ }
68+
5069 if ( stats . hasWarnings ( ) ) {
5170 this . ui . writeLine ( statsWarningsToString ( json , statsConfig ) ) ;
5271 }
0 commit comments