| 
									
										
										
										
											2019-04-23 20:50:11 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2019-04-23 20:50:11 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-21 08:38:13 +02:00
										 |  |  | import * as chalk from 'chalk'; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | import {readFileSync, writeFileSync} from 'fs'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 20:50:11 +02:00
										 |  |  | import {compareFileSizeData} from './file_size_compare'; | 
					
						
							|  |  |  | import {FileSizeData} from './file_size_data'; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | import {SizeTracker} from './size_tracker'; | 
					
						
							| 
									
										
										
										
											2019-04-23 20:50:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | if (require.main === module) { | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |   const [filePath, sourceMapPath, goldenPath, maxPercentageDiffArg, maxSizeDiffArg, writeGoldenArg, requiredCompileMode] = | 
					
						
							|  |  |  |       process.argv.slice(2); | 
					
						
							| 
									
										
										
										
											2019-04-23 20:50:11 +02:00
										 |  |  |   const status = main( | 
					
						
							|  |  |  |       require.resolve(filePath), require.resolve(sourceMapPath), require.resolve(goldenPath), | 
					
						
							| 
									
										
										
										
											2019-09-11 19:46:40 +02:00
										 |  |  |       writeGoldenArg === 'true', parseInt(maxPercentageDiffArg), parseInt(maxSizeDiffArg), | 
					
						
							|  |  |  |       requiredCompileMode); | 
					
						
							| 
									
										
										
										
											2019-04-23 20:50:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   process.exit(status ? 0 : 1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function main( | 
					
						
							|  |  |  |     filePath: string, sourceMapPath: string, goldenSizeMapPath: string, writeGolden: boolean, | 
					
						
							| 
									
										
										
										
											2019-11-22 12:11:48 -08:00
										 |  |  |     maxPercentageDiff: number, maxByteDiff: number, requiresIvy: string): boolean { | 
					
						
							| 
									
										
										
										
											2019-04-23 20:50:11 +02:00
										 |  |  |   const {sizeResult} = new SizeTracker(filePath, sourceMapPath); | 
					
						
							| 
									
										
										
										
											2019-11-22 12:11:48 -08:00
										 |  |  |   const ivyEnabled = process.env['angular_ivy_enabled'] == 'True'; | 
					
						
							| 
									
										
										
										
											2019-09-11 19:46:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-22 12:11:48 -08:00
										 |  |  |   if (requiresIvy && ivyEnabled) { | 
					
						
							| 
									
										
										
										
											2019-09-11 19:46:40 +02:00
										 |  |  |     console.error(chalk.red( | 
					
						
							|  |  |  |         `Expected the size-tracking tool to be run with: ` + | 
					
						
							| 
									
										
										
										
											2019-11-22 12:11:48 -08:00
										 |  |  |         `--config=${requiresIvy ? 'ivy' : 'view-engine'}`)); | 
					
						
							| 
									
										
										
										
											2019-09-11 19:46:40 +02:00
										 |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-23 20:50:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (writeGolden) { | 
					
						
							|  |  |  |     writeFileSync(goldenSizeMapPath, JSON.stringify(sizeResult, null, 2)); | 
					
						
							|  |  |  |     console.error(chalk.green(`Updated golden size data in ${goldenSizeMapPath}`)); | 
					
						
							| 
									
										
										
										
											2019-09-11 19:46:40 +02:00
										 |  |  |     return true; | 
					
						
							| 
									
										
										
										
											2019-04-23 20:50:11 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 14:09:42 -08:00
										 |  |  |   const expectedSizeData = JSON.parse(readFileSync(goldenSizeMapPath, 'utf8')) as FileSizeData; | 
					
						
							| 
									
										
										
										
											2019-05-03 15:48:23 +02:00
										 |  |  |   const differences = | 
					
						
							|  |  |  |       compareFileSizeData(sizeResult, expectedSizeData, {maxByteDiff, maxPercentageDiff}); | 
					
						
							| 
									
										
										
										
											2019-04-23 20:50:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (!differences.length) { | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   console.error( | 
					
						
							|  |  |  |       `Computed file size data does not match golden size data. ` + | 
					
						
							|  |  |  |       `The following differences were found:\n`); | 
					
						
							|  |  |  |   differences.forEach(({filePath, message}) => { | 
					
						
							|  |  |  |     const failurePrefix = filePath ? `"${filePath}": ` : ''; | 
					
						
							|  |  |  |     console.error(chalk.red(`    ${failurePrefix}${message}`)); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const bazelTargetName = process.env['TEST_TARGET']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   console.error(`\nThe golden file can be updated with the following command:`); | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |   console.error(`    yarn bazel run --config=${ivyEnabled ? 'ivy' : 'view-engine'} ${ | 
					
						
							|  |  |  |       bazelTargetName}.accept`);
 | 
					
						
							| 
									
										
										
										
											2019-09-11 19:46:40 +02:00
										 |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2019-04-23 20:50:11 +02:00
										 |  |  | } |