2019-04-23 14:50:11 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2019-04-23 14:50:11 -04: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 02:38:13 -04:00
|
|
|
import * as chalk from 'chalk';
|
2020-04-13 19:40:21 -04:00
|
|
|
import {readFileSync, writeFileSync} from 'fs';
|
|
|
|
|
2019-04-23 14:50:11 -04:00
|
|
|
import {compareFileSizeData} from './file_size_compare';
|
|
|
|
import {FileSizeData} from './file_size_data';
|
2020-04-13 19:40:21 -04:00
|
|
|
import {SizeTracker} from './size_tracker';
|
2019-04-23 14:50:11 -04:00
|
|
|
|
|
|
|
if (require.main === module) {
|
2020-04-13 19:40:21 -04:00
|
|
|
const [filePath, sourceMapPath, goldenPath, maxPercentageDiffArg, maxSizeDiffArg, writeGoldenArg, requiredCompileMode] =
|
|
|
|
process.argv.slice(2);
|
2019-04-23 14:50:11 -04:00
|
|
|
const status = main(
|
|
|
|
require.resolve(filePath), require.resolve(sourceMapPath), require.resolve(goldenPath),
|
2019-09-11 13:46:40 -04:00
|
|
|
writeGoldenArg === 'true', parseInt(maxPercentageDiffArg), parseInt(maxSizeDiffArg),
|
|
|
|
requiredCompileMode);
|
2019-04-23 14:50:11 -04:00
|
|
|
|
|
|
|
process.exit(status ? 0 : 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function main(
|
|
|
|
filePath: string, sourceMapPath: string, goldenSizeMapPath: string, writeGolden: boolean,
|
2019-11-22 15:11:48 -05:00
|
|
|
maxPercentageDiff: number, maxByteDiff: number, requiresIvy: string): boolean {
|
2019-04-23 14:50:11 -04:00
|
|
|
const {sizeResult} = new SizeTracker(filePath, sourceMapPath);
|
2019-11-22 15:11:48 -05:00
|
|
|
const ivyEnabled = process.env['angular_ivy_enabled'] == 'True';
|
2019-09-11 13:46:40 -04:00
|
|
|
|
2019-11-22 15:11:48 -05:00
|
|
|
if (requiresIvy && ivyEnabled) {
|
2019-09-11 13:46:40 -04:00
|
|
|
console.error(chalk.red(
|
|
|
|
`Expected the size-tracking tool to be run with: ` +
|
2019-11-22 15:11:48 -05:00
|
|
|
`--config=${requiresIvy ? 'ivy' : 'view-engine'}`));
|
2019-09-11 13:46:40 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-23 14:50:11 -04:00
|
|
|
|
|
|
|
if (writeGolden) {
|
|
|
|
writeFileSync(goldenSizeMapPath, JSON.stringify(sizeResult, null, 2));
|
|
|
|
console.error(chalk.green(`Updated golden size data in ${goldenSizeMapPath}`));
|
2019-09-11 13:46:40 -04:00
|
|
|
return true;
|
2019-04-23 14:50:11 -04:00
|
|
|
}
|
|
|
|
|
2021-02-04 17:09:42 -05:00
|
|
|
const expectedSizeData = JSON.parse(readFileSync(goldenSizeMapPath, 'utf8')) as FileSizeData;
|
2019-05-03 09:48:23 -04:00
|
|
|
const differences =
|
|
|
|
compareFileSizeData(sizeResult, expectedSizeData, {maxByteDiff, maxPercentageDiff});
|
2019-04-23 14:50:11 -04: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 19:40:21 -04:00
|
|
|
console.error(` yarn bazel run --config=${ivyEnabled ? 'ivy' : 'view-engine'} ${
|
|
|
|
bazelTargetName}.accept`);
|
2019-09-11 13:46:40 -04:00
|
|
|
return false;
|
2019-04-23 14:50:11 -04:00
|
|
|
}
|