@bundle-stats/utils 3.2.9 → 3.4.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/lib/constants.js +8 -1
  3. package/lib/constants.js.map +1 -1
  4. package/lib/index.js +15 -0
  5. package/lib/index.js.map +1 -1
  6. package/lib/jobs/create-job.js +2 -2
  7. package/lib/jobs/create-job.js.map +1 -1
  8. package/lib/jobs/create-jobs.js +2 -2
  9. package/lib/jobs/create-jobs.js.map +1 -1
  10. package/lib/transformers/budgets-insights.js +85 -0
  11. package/lib/transformers/budgets-insights.js.map +1 -0
  12. package/lib/transformers/index.js +28 -0
  13. package/lib/transformers/index.js.map +1 -0
  14. package/lib/webpack/constants.js +26 -12
  15. package/lib/webpack/constants.js.map +1 -1
  16. package/lib/webpack/extract/assets-size-total-insight.js +4 -4
  17. package/lib/webpack/extract/assets-size-total-insight.js.map +1 -1
  18. package/lib/webpack/extract/assets-size.js +3 -4
  19. package/lib/webpack/extract/assets-size.js.map +1 -1
  20. package/lib/webpack/extract/index.js +26 -1
  21. package/lib/webpack/extract/index.js.map +1 -1
  22. package/lib/webpack/extract/modules-packages-duplicate.js +2 -2
  23. package/lib/webpack/extract/modules-packages-duplicate.js.map +1 -1
  24. package/lib/webpack/metrics.js +27 -16
  25. package/lib/webpack/metrics.js.map +1 -1
  26. package/lib-esm/constants.js +7 -0
  27. package/lib-esm/constants.js.map +1 -1
  28. package/lib-esm/index.js +3 -0
  29. package/lib-esm/index.js.map +1 -1
  30. package/lib-esm/jobs/create-job.js +2 -2
  31. package/lib-esm/jobs/create-job.js.map +1 -1
  32. package/lib-esm/jobs/create-jobs.js +2 -2
  33. package/lib-esm/jobs/create-jobs.js.map +1 -1
  34. package/lib-esm/transformers/budgets-insights.js +77 -0
  35. package/lib-esm/transformers/budgets-insights.js.map +1 -0
  36. package/lib-esm/transformers/index.js +3 -0
  37. package/lib-esm/transformers/index.js.map +1 -0
  38. package/lib-esm/webpack/constants.js +25 -11
  39. package/lib-esm/webpack/constants.js.map +1 -1
  40. package/lib-esm/webpack/extract/assets-size-total-insight.js +4 -4
  41. package/lib-esm/webpack/extract/assets-size-total-insight.js.map +1 -1
  42. package/lib-esm/webpack/extract/assets-size.js +3 -4
  43. package/lib-esm/webpack/extract/assets-size.js.map +1 -1
  44. package/lib-esm/webpack/extract/index.js +3 -1
  45. package/lib-esm/webpack/extract/index.js.map +1 -1
  46. package/lib-esm/webpack/extract/modules-packages-duplicate.js +2 -2
  47. package/lib-esm/webpack/extract/modules-packages-duplicate.js.map +1 -1
  48. package/lib-esm/webpack/metrics.js +27 -16
  49. package/lib-esm/webpack/metrics.js.map +1 -1
  50. package/package.json +4 -4
  51. package/src/constants.ts +7 -0
  52. package/src/index.js +2 -0
  53. package/src/jobs/create-job.js +2 -2
  54. package/src/jobs/create-jobs.js +2 -2
  55. package/src/transformers/budgets-insights.ts +143 -0
  56. package/src/transformers/index.ts +1 -0
  57. package/src/webpack/{constants.js → constants.ts} +28 -15
  58. package/src/webpack/extract/assets-size-total-insight.js +4 -4
  59. package/src/webpack/extract/assets-size.js +3 -5
  60. package/src/webpack/extract/index.js +7 -5
  61. package/src/webpack/extract/modules-packages-duplicate.js +2 -2
  62. package/src/webpack/{metrics.js → metrics.ts} +27 -16
@@ -0,0 +1,143 @@
1
+ import get from 'lodash/get';
2
+
3
+ import { InsightType } from '../constants';
4
+ import { getGlobalMetricType } from '../utils';
5
+
6
+ interface OptionBudgetMetric {
7
+ metric: string;
8
+ value: number;
9
+ }
10
+
11
+ interface Options {
12
+ budgets?: Array<OptionBudgetMetric>;
13
+ }
14
+
15
+ interface BudgetInsight {
16
+ currentValue: number;
17
+ budgetValue: number;
18
+ failed: boolean;
19
+ }
20
+
21
+ interface BudgetInsightInfo {
22
+ type: InsightType;
23
+ message: {
24
+ template: string;
25
+ data: Record<string, unknown>;
26
+ };
27
+ source: {
28
+ metricId: string;
29
+ budgetInsight: BudgetInsight;
30
+ };
31
+ }
32
+
33
+ interface BudgetInsights {
34
+ [metricId: string]: BudgetInsight;
35
+ }
36
+
37
+ interface BudgetsInsightsOutput {
38
+ insights: {
39
+ budgets: BudgetInsights;
40
+ };
41
+ }
42
+
43
+ export const getExtract =
44
+ (source: string) => (
45
+ _: unknown,
46
+ currentExtractedData: any,
47
+ __: unknown,
48
+ options: Options = {},
49
+ ): BudgetsInsightsOutput | null => {
50
+ const { budgets: budgetsOptions } = options;
51
+
52
+ if (!budgetsOptions) {
53
+ return null;
54
+ }
55
+
56
+ const insights: Array<[string, BudgetInsight]> = [];
57
+
58
+ budgetsOptions.forEach((budgetOption) => {
59
+ const { metric: budgetMetricId, value: budgetValue } = budgetOption;
60
+ const currentValue = get(currentExtractedData, `metrics.${budgetMetricId}.value`);
61
+ const metric = getGlobalMetricType(`${source}.${budgetMetricId}`);
62
+
63
+ if (!currentValue) {
64
+ return;
65
+ }
66
+
67
+ const isValueOverBudget = currentValue > budgetValue;
68
+
69
+ const budgetInsight = {
70
+ currentValue,
71
+ budgetValue,
72
+ failed: metric.biggerIsBetter ? !isValueOverBudget : isValueOverBudget,
73
+ };
74
+
75
+ insights.push([budgetMetricId, budgetInsight]);
76
+ });
77
+
78
+ if (insights.length === 0) {
79
+ return null;
80
+ }
81
+
82
+ return {
83
+ insights: {
84
+ budgets: Object.fromEntries(insights),
85
+ },
86
+ };
87
+ };
88
+
89
+ const resolveDiffLabel = (budgetInsight: BudgetInsight): string => {
90
+ const { currentValue, budgetValue } = budgetInsight;
91
+
92
+ if (currentValue > budgetValue) {
93
+ return 'over';
94
+ }
95
+
96
+ if (currentValue < budgetValue) {
97
+ return 'under';
98
+ }
99
+
100
+ return 'equal with';
101
+ };
102
+
103
+ const resolveType = (budgetInsight: BudgetInsight): InsightType => {
104
+ if (budgetInsight.failed) {
105
+ return InsightType.ERROR;
106
+ }
107
+
108
+ if (budgetInsight.currentValue === budgetInsight.budgetValue) {
109
+ return InsightType.WARNING;
110
+ }
111
+
112
+ return InsightType.SUCCESS;
113
+ };
114
+
115
+ // Add escapes for (|) to avoid lodash.template syntax errors
116
+ // eslint-disable-next-line no-useless-escape
117
+ export const INFO_MESSAGE_TEMPLATE = '<%= metricLabel %> value \(<%= currentValue %>\) is <%= diffLabel %> <%= budgetValue %> budget';
118
+
119
+ export const getInfo = (globalMetricId: string, budgetInsight: BudgetInsight): BudgetInsightInfo => {
120
+ const metric = getGlobalMetricType(globalMetricId);
121
+
122
+ const diffLabel = resolveDiffLabel(budgetInsight);
123
+ const type = resolveType(budgetInsight);
124
+ const currentValue = metric.formatter(budgetInsight.currentValue);
125
+ const budgetValue = metric.formatter(budgetInsight.budgetValue);
126
+
127
+ return {
128
+ type,
129
+ message: {
130
+ template: INFO_MESSAGE_TEMPLATE,
131
+ data: {
132
+ metricLabel: metric.label,
133
+ diffLabel,
134
+ currentValue,
135
+ budgetValue,
136
+ },
137
+ },
138
+ source: {
139
+ metricId: globalMetricId,
140
+ budgetInsight,
141
+ },
142
+ };
143
+ };
@@ -0,0 +1 @@
1
+ export * as budgetInsights from './budgets-insights';
@@ -12,17 +12,32 @@ export const SECTIONS = [
12
12
  SECTION_WEBPACK_PACKAGES,
13
13
  ];
14
14
 
15
- export const SUMMARY_METRIC_PATHS = [
16
- 'totalSizeByTypeALL',
17
- 'totalInitialSizeJS',
18
- 'totalInitialSizeCSS',
19
- 'cacheInvalidation',
20
- 'moduleCount',
21
- 'chunkCount',
22
- 'assetCount',
23
- 'packageCount',
24
- 'duplicatePackagesCount',
25
- ];
15
+ export enum MetricId {
16
+ BundleSize = 'totalSizeByTypeALL',
17
+ TotalInitialSizeJS = 'totalInitialSizeJS',
18
+ TotalInitialSizeCSS = 'totalInitialSizeCSS',
19
+ CacheInvalidation = 'cacheInvalidation',
20
+ ModuleName = 'moduleCount',
21
+ ChunkCount = 'chunkCount',
22
+ AssetCount = 'assetCount',
23
+ PackageCount = 'packageCount',
24
+ DuplicatePackagesCount = 'duplicatePackagesCount',
25
+ }
26
+
27
+ export const SIZE_BY_TYPE_METRIC_ID_ALL = 'ALL';
28
+ export const SIZE_BY_TYPE_METRIC_ID_PREFIX = 'totalSizeByType';
29
+
30
+ export enum SizeByTypeMetricId {
31
+ JS = 'totalSizeByTypeJS',
32
+ CSS = 'totalSizeByTypeCSS',
33
+ IMG = 'totalSizeByTypeIMG',
34
+ Media = 'totalSizeByTypeMEDIA',
35
+ Font = 'totalSizeByTypeFONT',
36
+ HTML = 'totalSizeByTypeHTML',
37
+ Other = 'totalSizeByTypeOTHER',
38
+ }
39
+
40
+ export const SUMMARY_METRIC_PATHS = Object.values(MetricId);
26
41
 
27
42
  const PACKAGE_PREFIX = /(?:node_modules|~)(?:\/\.pnpm)?/;
28
43
  const PACKAGE_SLUG = /[a-zA-Z0-9]+(?:[-|_|.]+[a-zA-Z0-9]+)*/;
@@ -30,7 +45,7 @@ const VERSION = /@[\w|\-|_|.]+/;
30
45
 
31
46
  // Extract package paths from module path
32
47
  // https://regex101.com/r/22Leep/6
33
- /* eslint-disable prettier/prettier */
48
+ /* eslint-disable prettier/prettier, @typescript-eslint/indent */
34
49
  export const MODULE_PATH_PACKAGES = new RegExp([
35
50
  // match dependency directory (eg: `node_modules/`, `node_modules/.pnpm/`)
36
51
  `(?:${PACKAGE_PREFIX.source}/)`,
@@ -54,11 +69,9 @@ export const MODULE_PATH_PACKAGES = new RegExp([
54
69
  ')*',
55
70
  '/',
56
71
  ].join(''), 'g');
57
- /* eslint-enable prettier/prettier */
58
72
 
59
73
  // Extract package name from package path
60
74
  // https://regex101.com/r/tTlU0W/6
61
- /* eslint-disable prettier/prettier */
62
75
  export const PACKAGE_PATH_NAME = new RegExp([
63
76
  `(?:${PACKAGE_PREFIX.source}/)`,
64
77
  // match dependency
@@ -80,4 +93,4 @@ export const PACKAGE_PATH_NAME = new RegExp([
80
93
  ')*',
81
94
  '/',
82
95
  ].join(''), 'g');
83
- /* eslint-enable prettier/prettier */
96
+ /* eslint-enable prettier/prettier, @typescript-eslint/indent */
@@ -4,21 +4,21 @@ import template from 'lodash/template';
4
4
  import { INSIGHT_INFO } from '../../config/insights';
5
5
  import { getMetricRunInfo } from '../../utils/metrics';
6
6
  import { getMetricType } from '../utils';
7
+ import { MetricId } from '../constants';
7
8
 
8
9
  const INFO_TEMPLATE = template(
9
10
  '<%= metric %> — <%= displayValue %> (<%= displayDeltaPercentage %>).',
10
11
  );
11
- const METRIC_NAME = 'totalSizeByTypeALL';
12
12
 
13
13
  export const extractAssetsSizeTotalInsight = (
14
14
  webpackStats,
15
15
  currentExtractedData,
16
16
  baselineBundleStats,
17
17
  ) => {
18
- const currentValue = get(currentExtractedData, ['metrics', METRIC_NAME, 'value'], 0);
19
- const baselineValue = get(baselineBundleStats, ['metrics', 'webpack', METRIC_NAME, 'value'], 0);
18
+ const currentValue = get(currentExtractedData, ['metrics', MetricId.BundleSize, 'value'], 0);
19
+ const baselineValue = get(baselineBundleStats, ['metrics', 'webpack', MetricId.BundleSize, 'value'], 0);
20
20
 
21
- const metric = getMetricType(METRIC_NAME);
21
+ const metric = getMetricType(MetricId.BundleSize);
22
22
  const info = getMetricRunInfo(metric, currentValue, baselineValue);
23
23
  const { displayDeltaPercentage, displayValue } = info;
24
24
 
@@ -4,11 +4,9 @@ import sum from 'lodash/sum';
4
4
 
5
5
  import { FILE_TYPE_CSS, FILE_TYPE_JS, FILE_TYPES } from '../../config/file-types';
6
6
  import { getFileType } from '../../utils/file-types';
7
+ import { SIZE_BY_TYPE_METRIC_ID_PREFIX, SIZE_BY_TYPE_METRIC_ID_ALL } from '../constants';
7
8
 
8
- const METRIC_NAME_ALL = 'ALL';
9
- const METRIC_NAME_PREFIX = 'totalSizeByType';
10
-
11
- const getMetricName = (fileType) => `${METRIC_NAME_PREFIX}${fileType}`;
9
+ const getMetricName = (fileType) => `${SIZE_BY_TYPE_METRIC_ID_PREFIX}${fileType}`;
12
10
 
13
11
  const generateInitialSizeByType = () => FILE_TYPES.reduce((accumulator, fileType) => ({
14
12
  ...accumulator,
@@ -52,7 +50,7 @@ export const extractAssetsSize = (webpackStats, currentExtractedData) => {
52
50
 
53
51
  const sizes = calculateTotalByType(bundleAssets);
54
52
  const generic = {
55
- [getMetricName(METRIC_NAME_ALL)]: {
53
+ [getMetricName(SIZE_BY_TYPE_METRIC_ID_ALL)]: {
56
54
  value: sum(map(bundleAssets, 'value')),
57
55
  },
58
56
  ...calculateInitialTotals(bundleAssets),
@@ -1,5 +1,6 @@
1
1
  import merge from 'lodash/merge';
2
2
 
3
+ import * as budgetsInsights from '../../transformers/budgets-insights';
3
4
  import { extractAssets } from './assets';
4
5
  import { extractAssetsCacheInvalidation } from './assets-cache-invalidation';
5
6
  import { extractAssetsChunkCount } from './assets-chunk-count';
@@ -26,10 +27,11 @@ const extractFns = [
26
27
  extractModulesPackages,
27
28
  extractModulesPackagesCount,
28
29
  extractModulesPackagesDuplicate,
30
+ budgetsInsights.getExtract('webpack'),
29
31
  ];
30
32
 
31
- export const extract = (webpackStats, baseline) => extractFns.reduce((agg, extractFn) => merge(
32
- {},
33
- agg,
34
- extractFn(webpackStats, agg, baseline),
35
- ), {});
33
+ export const extract = (webpackStats, baseline, options) =>
34
+ extractFns.reduce(
35
+ (agg, extractFn) => merge({}, agg, extractFn(webpackStats, agg, baseline, options)),
36
+ {},
37
+ );
@@ -3,7 +3,7 @@ import map from 'lodash/map';
3
3
  import orderBy from 'lodash/orderBy';
4
4
  import sum from 'lodash/sum';
5
5
 
6
- import { INSIGHT_WARNING } from '../../config';
6
+ import { InsightType } from '../../constants';
7
7
 
8
8
  export const extractModulesPackagesDuplicate = (webpackStats, currentExtractedData) => {
9
9
  const source = get(currentExtractedData, 'metrics.packages', {});
@@ -89,7 +89,7 @@ export const extractModulesPackagesDuplicate = (webpackStats, currentExtractedDa
89
89
  return {
90
90
  insights: {
91
91
  duplicatePackages: {
92
- type: INSIGHT_WARNING,
92
+ type: InsightType.WARNING,
93
93
  data,
94
94
  },
95
95
  },
@@ -3,62 +3,66 @@ import {
3
3
  METRIC_TYPE_NUMERIC,
4
4
  METRIC_TYPE_PERCENTAGE,
5
5
  } from '../config/metrics';
6
+ import { MetricId, SizeByTypeMetricId } from './constants';
6
7
 
7
8
  export const metrics = {
8
- totalSizeByTypeALL: {
9
+ [MetricId.BundleSize]: {
9
10
  label: 'Bundle Size',
10
11
  description: 'Sum of all assets emitted by webpack.',
11
12
  url: 'https://relative-ci.com/documentation/metrics-and-data#assets',
12
13
  type: METRIC_TYPE_FILE_SIZE,
14
+ biggerIsBetter: false,
13
15
  },
14
- totalInitialSizeJS: {
16
+ [MetricId.TotalInitialSizeJS]: {
15
17
  label: 'Initial JS',
16
18
  description: 'Sum of all JavaScript assets that are labeled as initial(eg: vendor.js, app.js).',
17
19
  url: 'https://relative-ci.com/documentation/metrics-and-data#assets',
18
20
  type: METRIC_TYPE_FILE_SIZE,
21
+ biggerIsBetter: false,
19
22
  },
20
- totalInitialSizeCSS: {
23
+ [MetricId.TotalInitialSizeCSS]: {
21
24
  label: 'Initial CSS',
22
25
  description: 'Sum of all CSS assets that are labeled as initial(eg: vendor.css, app.css).',
23
26
  url: 'https://relative-ci.com/documentation/metrics-and-data#assets',
24
27
  type: METRIC_TYPE_FILE_SIZE,
28
+ biggerIsBetter: false,
25
29
  },
26
- cacheInvalidation: {
30
+ [MetricId.CacheInvalidation]: {
27
31
  label: 'Cache Invalidation',
28
32
  description: 'Ratio between the total changed assets and Bundle Size.',
29
33
  url: 'https://relative-ci.com/documentation/metrics-and-data#assets',
30
34
  type: METRIC_TYPE_PERCENTAGE,
31
35
  biggerIsBetter: false,
32
36
  },
33
- chunkCount: {
37
+ [MetricId.ChunkCount]: {
34
38
  label: 'Chunks',
35
39
  description: 'Total number of chunks generated by webpack.',
36
40
  url: 'https://relative-ci.com/documentation/metrics-and-data#assets',
37
41
  type: METRIC_TYPE_NUMERIC,
38
42
  biggerIsBetter: false,
39
43
  },
40
- assetCount: {
44
+ [MetricId.AssetCount]: {
41
45
  label: 'Assets',
42
46
  description: 'Total number of assets emitted by webpack.',
43
47
  url: 'https://relative-ci.com/documentation/metrics-and-data#assets',
44
48
  type: METRIC_TYPE_NUMERIC,
45
49
  biggerIsBetter: false,
46
50
  },
47
- moduleCount: {
51
+ [MetricId.ModuleName]: {
48
52
  label: 'Modules',
49
53
  description: 'Total number of modules bundled by webpack.',
50
54
  url: 'https://relative-ci.com/documentation/metrics-and-data#modules-1',
51
55
  type: METRIC_TYPE_NUMERIC,
52
56
  biggerIsBetter: false,
53
57
  },
54
- packageCount: {
58
+ [MetricId.PackageCount]: {
55
59
  label: 'Packages',
56
60
  description: 'Total number of packages bundled by webpack.',
57
61
  url: 'https://relative-ci.com/documentation/metrics-and-data#packages',
58
62
  type: METRIC_TYPE_NUMERIC,
59
63
  biggerIsBetter: false,
60
64
  },
61
- duplicatePackagesCount: {
65
+ [MetricId.DuplicatePackagesCount]: {
62
66
  label: 'Duplicate Packages',
63
67
  description: 'Total number of packages that are duplicated.',
64
68
  url: 'https://relative-ci.com/documentation/metrics-and-data#packages',
@@ -66,33 +70,40 @@ export const metrics = {
66
70
  biggerIsBetter: false,
67
71
  },
68
72
  sizes: {
69
- totalSizeByTypeJS: {
73
+ [SizeByTypeMetricId.JS]: {
70
74
  label: 'JS',
71
75
  type: METRIC_TYPE_FILE_SIZE,
76
+ biggerIsBetter: false,
72
77
  },
73
- totalSizeByTypeCSS: {
78
+ [SizeByTypeMetricId.CSS]: {
74
79
  label: 'CSS',
75
80
  type: METRIC_TYPE_FILE_SIZE,
81
+ biggerIsBetter: false,
76
82
  },
77
- totalSizeByTypeIMG: {
83
+ [SizeByTypeMetricId.IMG]: {
78
84
  label: 'IMG',
79
85
  type: METRIC_TYPE_FILE_SIZE,
86
+ biggerIsBetter: false,
80
87
  },
81
- totalSizeByTypeMEDIA: {
88
+ [SizeByTypeMetricId.Media]: {
82
89
  label: 'Media',
83
90
  type: METRIC_TYPE_FILE_SIZE,
91
+ biggerIsBetter: false,
84
92
  },
85
- totalSizeByTypeFONT: {
93
+ [SizeByTypeMetricId.Font]: {
86
94
  label: 'Fonts',
87
95
  type: METRIC_TYPE_FILE_SIZE,
96
+ biggerIsBetter: false,
88
97
  },
89
- totalSizeByTypeHTML: {
98
+ [SizeByTypeMetricId.HTML]: {
90
99
  label: 'HTML',
91
100
  type: METRIC_TYPE_FILE_SIZE,
101
+ biggerIsBetter: false,
92
102
  },
93
- totalSizeByTypeOTHER: {
103
+ [SizeByTypeMetricId.Other]: {
94
104
  label: 'Other',
95
105
  type: METRIC_TYPE_FILE_SIZE,
106
+ biggerIsBetter: false,
96
107
  },
97
108
  },
98
109
  };