|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { logging } from '@angular-devkit/core'; |
| 10 | +import { lazyModuleFiles, lazyModuleFnImport } from '../../../../testing/test-utils'; |
| 11 | +import { buildApplication } from '../../index'; |
| 12 | +import { Type } from '../../schema'; |
| 13 | +import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup'; |
| 14 | + |
| 15 | +describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { |
| 16 | + const CSS_EXTENSIONS = ['css', 'scss', 'less']; |
| 17 | + const BUDGET_NOT_MET_REGEXP = /Budget .+ was not met by/; |
| 18 | + |
| 19 | + describe('Option: "bundleBudgets"', () => { |
| 20 | + it(`should not warn when size is below threshold`, async () => { |
| 21 | + harness.useTarget('build', { |
| 22 | + ...BASE_OPTIONS, |
| 23 | + optimization: true, |
| 24 | + budgets: [{ type: Type.All, maximumWarning: '100mb' }], |
| 25 | + }); |
| 26 | + |
| 27 | + const { result, logs } = await harness.executeOnce(); |
| 28 | + expect(result?.success).toBe(true); |
| 29 | + expect(logs).not.toContain( |
| 30 | + jasmine.objectContaining<logging.LogEntry>({ |
| 31 | + level: 'warn', |
| 32 | + message: jasmine.stringMatching(BUDGET_NOT_MET_REGEXP), |
| 33 | + }), |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it(`should error when size is above 'maximumError' threshold`, async () => { |
| 38 | + harness.useTarget('build', { |
| 39 | + ...BASE_OPTIONS, |
| 40 | + optimization: true, |
| 41 | + budgets: [{ type: Type.All, maximumError: '100b' }], |
| 42 | + }); |
| 43 | + |
| 44 | + const { result, logs } = await harness.executeOnce(); |
| 45 | + expect(logs).toContain( |
| 46 | + jasmine.objectContaining<logging.LogEntry>({ |
| 47 | + level: 'error', |
| 48 | + message: jasmine.stringMatching(BUDGET_NOT_MET_REGEXP), |
| 49 | + }), |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + it(`should warn when size is above 'maximumWarning' threshold`, async () => { |
| 54 | + harness.useTarget('build', { |
| 55 | + ...BASE_OPTIONS, |
| 56 | + optimization: true, |
| 57 | + budgets: [{ type: Type.All, maximumWarning: '100b' }], |
| 58 | + }); |
| 59 | + |
| 60 | + const { result, logs } = await harness.executeOnce(); |
| 61 | + expect(result?.success).toBe(true); |
| 62 | + expect(logs).toContain( |
| 63 | + jasmine.objectContaining<logging.LogEntry>({ |
| 64 | + level: 'warn', |
| 65 | + message: jasmine.stringMatching(BUDGET_NOT_MET_REGEXP), |
| 66 | + }), |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it(`should warn when lazy bundle is above 'maximumWarning' threshold`, async () => { |
| 71 | + harness.useTarget('build', { |
| 72 | + ...BASE_OPTIONS, |
| 73 | + optimization: true, |
| 74 | + budgets: [{ type: Type.Bundle, name: 'lazy-module', maximumWarning: '100b' }], |
| 75 | + }); |
| 76 | + |
| 77 | + await harness.writeFiles(lazyModuleFiles); |
| 78 | + await harness.writeFiles(lazyModuleFnImport); |
| 79 | + |
| 80 | + const { result, logs } = await harness.executeOnce(); |
| 81 | + expect(result?.success).toBe(true); |
| 82 | + expect(logs).toContain( |
| 83 | + jasmine.objectContaining<logging.LogEntry>({ |
| 84 | + level: 'warn', |
| 85 | + message: jasmine.stringMatching('lazy-module exceeded maximum budget'), |
| 86 | + }), |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + CSS_EXTENSIONS.forEach((ext) => { |
| 91 | + xit(`shows warnings for large component ${ext} when using 'anyComponentStyle' when AOT`, async () => { |
| 92 | + const cssContent = ` |
| 93 | + .foo { color: white; padding: 1px; } |
| 94 | + .buz { color: white; padding: 2px; } |
| 95 | + .bar { color: white; padding: 3px; } |
| 96 | + `; |
| 97 | + |
| 98 | + await harness.writeFiles({ |
| 99 | + [`src/app/app.component.${ext}`]: cssContent, |
| 100 | + [`src/assets/foo.${ext}`]: cssContent, |
| 101 | + [`src/styles.${ext}`]: cssContent, |
| 102 | + }); |
| 103 | + |
| 104 | + await harness.modifyFile('src/app/app.component.ts', (content) => |
| 105 | + content.replace('app.component.css', `app.component.${ext}`), |
| 106 | + ); |
| 107 | + |
| 108 | + harness.useTarget('build', { |
| 109 | + ...BASE_OPTIONS, |
| 110 | + optimization: true, |
| 111 | + aot: true, |
| 112 | + styles: [`src/styles.${ext}`], |
| 113 | + budgets: [{ type: Type.AnyComponentStyle, maximumWarning: '1b' }], |
| 114 | + }); |
| 115 | + |
| 116 | + const { result, logs } = await harness.executeOnce(); |
| 117 | + expect(result?.success).toBe(true); |
| 118 | + expect(logs).toContain( |
| 119 | + jasmine.objectContaining<logging.LogEntry>({ |
| 120 | + level: 'warn', |
| 121 | + message: jasmine.stringMatching(new RegExp(`Warning.+app.component.${ext}`)), |
| 122 | + }), |
| 123 | + ); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe(`should ignore '.map' files`, () => { |
| 128 | + it(`when 'bundle' budget`, async () => { |
| 129 | + harness.useTarget('build', { |
| 130 | + ...BASE_OPTIONS, |
| 131 | + sourceMap: true, |
| 132 | + optimization: true, |
| 133 | + extractLicenses: true, |
| 134 | + budgets: [{ type: Type.Bundle, name: 'main', maximumError: '1mb' }], |
| 135 | + }); |
| 136 | + |
| 137 | + const { result, logs } = await harness.executeOnce(); |
| 138 | + expect(result?.success).toBe(true); |
| 139 | + expect(logs).not.toContain( |
| 140 | + jasmine.objectContaining<logging.LogEntry>({ |
| 141 | + level: 'error', |
| 142 | + message: jasmine.stringMatching(BUDGET_NOT_MET_REGEXP), |
| 143 | + }), |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + it(`when 'intial' budget`, async () => { |
| 148 | + harness.useTarget('build', { |
| 149 | + ...BASE_OPTIONS, |
| 150 | + sourceMap: true, |
| 151 | + optimization: true, |
| 152 | + extractLicenses: true, |
| 153 | + budgets: [{ type: Type.Initial, name: 'main', maximumError: '1mb' }], |
| 154 | + }); |
| 155 | + |
| 156 | + const { result, logs } = await harness.executeOnce(); |
| 157 | + expect(result?.success).toBe(true); |
| 158 | + expect(logs).not.toContain( |
| 159 | + jasmine.objectContaining<logging.LogEntry>({ |
| 160 | + level: 'error', |
| 161 | + message: jasmine.stringMatching(BUDGET_NOT_MET_REGEXP), |
| 162 | + }), |
| 163 | + ); |
| 164 | + }); |
| 165 | + |
| 166 | + it(`when 'all' budget`, async () => { |
| 167 | + harness.useTarget('build', { |
| 168 | + ...BASE_OPTIONS, |
| 169 | + sourceMap: true, |
| 170 | + optimization: true, |
| 171 | + extractLicenses: true, |
| 172 | + budgets: [{ type: Type.All, maximumError: '1mb' }], |
| 173 | + }); |
| 174 | + |
| 175 | + const { result, logs } = await harness.executeOnce(); |
| 176 | + expect(result?.success).toBe(true); |
| 177 | + expect(logs).not.toContain( |
| 178 | + jasmine.objectContaining<logging.LogEntry>({ |
| 179 | + level: 'error', |
| 180 | + message: jasmine.stringMatching(BUDGET_NOT_MET_REGEXP), |
| 181 | + }), |
| 182 | + ); |
| 183 | + }); |
| 184 | + |
| 185 | + it(`when 'any' budget`, async () => { |
| 186 | + harness.useTarget('build', { |
| 187 | + ...BASE_OPTIONS, |
| 188 | + sourceMap: true, |
| 189 | + optimization: true, |
| 190 | + extractLicenses: true, |
| 191 | + budgets: [{ type: Type.Any, maximumError: '1mb' }], |
| 192 | + }); |
| 193 | + |
| 194 | + const { result, logs } = await harness.executeOnce(); |
| 195 | + expect(result?.success).toBe(true); |
| 196 | + expect(logs).not.toContain( |
| 197 | + jasmine.objectContaining<logging.LogEntry>({ |
| 198 | + level: 'error', |
| 199 | + message: jasmine.stringMatching(BUDGET_NOT_MET_REGEXP), |
| 200 | + }), |
| 201 | + ); |
| 202 | + }); |
| 203 | + }); |
| 204 | + }); |
| 205 | +}); |
0 commit comments