@bundle-stats/utils 3.3.0-alpha.1 → 3.3.1-beta.8

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 (88) hide show
  1. package/CHANGELOG.md +29 -12
  2. package/lib/browsertime/index.js +5 -1
  3. package/lib/browsertime/index.js.map +1 -1
  4. package/lib/config/component-links.js +1 -0
  5. package/lib/config/component-links.js.map +1 -1
  6. package/lib/config/file-types.js +3 -3
  7. package/lib/config/file-types.js.map +1 -1
  8. package/lib/config/index.js +5 -1
  9. package/lib/config/index.js.map +1 -1
  10. package/lib/constants.js.map +1 -1
  11. package/lib/i18n.js +2 -1
  12. package/lib/i18n.js.map +1 -1
  13. package/lib/index.js +5 -1
  14. package/lib/index.js.map +1 -1
  15. package/lib/jobs/create-job.js +5 -1
  16. package/lib/jobs/create-job.js.map +1 -1
  17. package/lib/jobs/index.js +5 -1
  18. package/lib/jobs/index.js.map +1 -1
  19. package/lib/lighthouse/index.js +5 -1
  20. package/lib/lighthouse/index.js.map +1 -1
  21. package/lib/report/create.js +5 -1
  22. package/lib/report/create.js.map +1 -1
  23. package/lib/report/index.js +5 -1
  24. package/lib/report/index.js.map +1 -1
  25. package/lib/transformers/index.js +5 -1
  26. package/lib/transformers/index.js.map +1 -1
  27. package/lib/utils/component-links.js +15 -1
  28. package/lib/utils/component-links.js.map +1 -1
  29. package/lib/utils/format.js +5 -1
  30. package/lib/utils/format.js.map +1 -1
  31. package/lib/utils/index.js +5 -1
  32. package/lib/utils/index.js.map +1 -1
  33. package/lib/webpack/constants.js +2 -0
  34. package/lib/webpack/constants.js.map +1 -1
  35. package/lib/webpack/extract/index.js +5 -1
  36. package/lib/webpack/extract/index.js.map +1 -1
  37. package/lib/webpack/extract/modules.js +29 -2
  38. package/lib/webpack/extract/modules.js.map +1 -1
  39. package/lib/webpack/index.js +5 -1
  40. package/lib/webpack/index.js.map +1 -1
  41. package/lib/webpack/metrics.js +14 -0
  42. package/lib/webpack/metrics.js.map +1 -1
  43. package/lib-esm/config/component-links.js +1 -0
  44. package/lib-esm/config/component-links.js.map +1 -1
  45. package/lib-esm/config/file-types.js +3 -3
  46. package/lib-esm/config/file-types.js.map +1 -1
  47. package/lib-esm/constants.js.map +1 -1
  48. package/lib-esm/i18n.js +2 -1
  49. package/lib-esm/i18n.js.map +1 -1
  50. package/lib-esm/utils/component-links.js +14 -0
  51. package/lib-esm/utils/component-links.js.map +1 -1
  52. package/lib-esm/webpack/constants.js +2 -0
  53. package/lib-esm/webpack/constants.js.map +1 -1
  54. package/lib-esm/webpack/extract/modules.js +29 -2
  55. package/lib-esm/webpack/extract/modules.js.map +1 -1
  56. package/lib-esm/webpack/metrics.js +14 -0
  57. package/lib-esm/webpack/metrics.js.map +1 -1
  58. package/package.json +16 -16
  59. package/src/config/component-links.js +1 -0
  60. package/src/config/file-types.js +3 -3
  61. package/src/constants.ts +3 -7
  62. package/src/i18n.js +2 -1
  63. package/src/index.js +0 -2
  64. package/src/jobs/create-job.js +2 -2
  65. package/src/jobs/create-jobs.js +2 -2
  66. package/src/utils/component-links.js +23 -7
  67. package/src/webpack/constants.js +91 -0
  68. package/src/webpack/extract/assets-size-total-insight.js +4 -4
  69. package/src/webpack/extract/assets-size.js +5 -3
  70. package/src/webpack/extract/index.js +5 -7
  71. package/src/webpack/extract/modules-packages-duplicate.js +2 -2
  72. package/src/webpack/extract/modules.ts +35 -4
  73. package/src/webpack/{metrics.ts → metrics.js} +30 -27
  74. package/types/config/component-links.d.ts +1 -0
  75. package/types/constants.d.ts +3 -0
  76. package/types/i18n.d.ts +1 -0
  77. package/types/utils/component-links.d.ts +26 -12
  78. package/types/webpack/constants.d.ts +2 -0
  79. package/types/webpack/metrics.d.ts +14 -0
  80. package/lib/webpack/extract/budgets-insights.js +0 -39
  81. package/lib/webpack/extract/budgets-insights.js.map +0 -1
  82. package/lib-esm/webpack/extract/budgets-insights.js +0 -33
  83. package/lib-esm/webpack/extract/budgets-insights.js.map +0 -1
  84. package/src/transformers/budgets-insights.ts +0 -143
  85. package/src/transformers/index.ts +0 -1
  86. package/src/webpack/constants.ts +0 -96
  87. package/src/webpack/extract/budgets-insights.ts +0 -73
  88. package/types/webpack/extract/budgets-insights.d.ts +0 -22
@@ -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 { InsightType } from '../../constants';
6
+ import { INSIGHT_WARNING } from '../../config';
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: InsightType.WARNING,
92
+ type: INSIGHT_WARNING,
93
93
  data,
94
94
  },
95
95
  },
@@ -1,4 +1,5 @@
1
1
  import isEmpty from 'lodash/isEmpty';
2
+ import round from 'lodash/round';
2
3
 
3
4
  import { ModuleMetric, WebpackMetricsModules } from '../../constants';
4
5
  import { getModuleName, normalizeChunkId } from '../utils';
@@ -10,7 +11,7 @@ interface WebpackModule {
10
11
  }
11
12
 
12
13
  interface WebpackModuleWithConcatenatedModules extends WebpackModule {
13
- modules?: Array<Pick<WebpackModule, 'name' | 'size'>>;
14
+ modules: Array<Pick<WebpackModule, 'name' | 'size'>>;
14
15
  }
15
16
 
16
17
  /*
@@ -20,7 +21,13 @@ export const extractModules = (webpackStats?: any): WebpackMetricsModules => {
20
21
  const modulesSource: Array<WebpackModuleWithConcatenatedModules> = webpackStats?.modules || [];
21
22
 
22
23
  if (!modulesSource) {
23
- return { metrics: { modules: {} } };
24
+ return {
25
+ metrics: {
26
+ duplicateCode: { value: 0 },
27
+ duplicateModulesCount: { value: 0 },
28
+ modules: {},
29
+ },
30
+ };
24
31
  }
25
32
 
26
33
  // Flatten concatenated modules
@@ -43,25 +50,49 @@ export const extractModules = (webpackStats?: any): WebpackMetricsModules => {
43
50
  return agg;
44
51
  }, [] as Array<WebpackModule>);
45
52
 
53
+ let totalCodeSize = 0;
54
+ let duplicateCodeSize = 0;
55
+ let duplicateModulesCount = 0;
56
+
46
57
  // Normalize module entries
47
58
  const modules = allModules.reduce((agg, moduleEntry) => {
48
59
  const { name, size, chunks } = moduleEntry;
49
60
  const normalizedName = getModuleName(name);
50
61
 
51
- // skip modules with no chunks
62
+ // skip modules that do not belong to any chunk
52
63
  if (isEmpty(chunks)) {
53
64
  return agg;
54
65
  }
55
66
 
67
+ const instances = chunks.length;
68
+ const duplicateInstances = instances - 1;
69
+
70
+ duplicateModulesCount += duplicateInstances;
71
+ duplicateCodeSize += duplicateInstances * size;
72
+ totalCodeSize += instances * size;
73
+
56
74
  // eslint-disable-next-line no-param-reassign
57
75
  agg[normalizedName] = {
58
76
  name,
59
77
  value: size,
60
78
  chunkIds: chunks.map(normalizeChunkId),
79
+ duplicated: Boolean(duplicateInstances),
61
80
  };
62
81
 
63
82
  return agg;
64
83
  }, {} as Record<string, ModuleMetric>);
65
84
 
66
- return { metrics: { modules } };
85
+ const duplicateCode = totalCodeSize ? round((duplicateCodeSize / totalCodeSize) * 100, 2) : 0;
86
+
87
+ return {
88
+ metrics: {
89
+ modules,
90
+ duplicateCode: {
91
+ value: duplicateCode,
92
+ },
93
+ duplicateModulesCount: {
94
+ value: duplicateModulesCount,
95
+ },
96
+ },
97
+ };
67
98
  };
@@ -3,66 +3,76 @@ import {
3
3
  METRIC_TYPE_NUMERIC,
4
4
  METRIC_TYPE_PERCENTAGE,
5
5
  } from '../config/metrics';
6
- import { MetricId, SizeByTypeMetricId } from './constants';
7
6
 
8
7
  export const metrics = {
9
- [MetricId.BundleSize]: {
8
+ totalSizeByTypeALL: {
10
9
  label: 'Bundle Size',
11
10
  description: 'Sum of all assets emitted by webpack.',
12
11
  url: 'https://relative-ci.com/documentation/metrics-and-data#assets',
13
12
  type: METRIC_TYPE_FILE_SIZE,
14
- biggerIsBetter: false,
15
13
  },
16
- [MetricId.TotalInitialSizeJS]: {
14
+ totalInitialSizeJS: {
17
15
  label: 'Initial JS',
18
16
  description: 'Sum of all JavaScript assets that are labeled as initial(eg: vendor.js, app.js).',
19
17
  url: 'https://relative-ci.com/documentation/metrics-and-data#assets',
20
18
  type: METRIC_TYPE_FILE_SIZE,
21
- biggerIsBetter: false,
22
19
  },
23
- [MetricId.TotalInitialSizeCSS]: {
20
+ totalInitialSizeCSS: {
24
21
  label: 'Initial CSS',
25
22
  description: 'Sum of all CSS assets that are labeled as initial(eg: vendor.css, app.css).',
26
23
  url: 'https://relative-ci.com/documentation/metrics-and-data#assets',
27
24
  type: METRIC_TYPE_FILE_SIZE,
28
- biggerIsBetter: false,
29
25
  },
30
- [MetricId.CacheInvalidation]: {
26
+ cacheInvalidation: {
31
27
  label: 'Cache Invalidation',
32
28
  description: 'Ratio between the total changed assets and Bundle Size.',
33
29
  url: 'https://relative-ci.com/documentation/metrics-and-data#assets',
34
30
  type: METRIC_TYPE_PERCENTAGE,
35
31
  biggerIsBetter: false,
36
32
  },
37
- [MetricId.ChunkCount]: {
33
+ chunkCount: {
38
34
  label: 'Chunks',
39
35
  description: 'Total number of chunks generated by webpack.',
40
36
  url: 'https://relative-ci.com/documentation/metrics-and-data#assets',
41
37
  type: METRIC_TYPE_NUMERIC,
42
38
  biggerIsBetter: false,
43
39
  },
44
- [MetricId.AssetCount]: {
40
+ assetCount: {
45
41
  label: 'Assets',
46
42
  description: 'Total number of assets emitted by webpack.',
47
43
  url: 'https://relative-ci.com/documentation/metrics-and-data#assets',
48
44
  type: METRIC_TYPE_NUMERIC,
49
45
  biggerIsBetter: false,
50
46
  },
51
- [MetricId.ModuleName]: {
47
+ moduleCount: {
52
48
  label: 'Modules',
53
49
  description: 'Total number of modules bundled by webpack.',
54
50
  url: 'https://relative-ci.com/documentation/metrics-and-data#modules-1',
55
51
  type: METRIC_TYPE_NUMERIC,
56
52
  biggerIsBetter: false,
57
53
  },
58
- [MetricId.PackageCount]: {
54
+ duplicateModulesCount: {
55
+ label: 'Duplicate Modules',
56
+ description: 'Total number of modules that belong to more than one chunk.',
57
+ url: 'https://relative-ci.com/documentation/metrics-and-data#modules-1',
58
+ type: METRIC_TYPE_NUMERIC,
59
+ biggerIsBetter: false,
60
+ },
61
+ duplicateCode: {
62
+ label: 'Duplicate Code',
63
+ description: 'The percentage of total duplicate module size from the total module size.',
64
+ url: 'https://relative-ci.com/documentation/metrics-and-data#modules-1',
65
+ type: METRIC_TYPE_PERCENTAGE,
66
+ biggerIsBetter: false,
67
+ },
68
+ packageCount: {
59
69
  label: 'Packages',
60
70
  description: 'Total number of packages bundled by webpack.',
61
71
  url: 'https://relative-ci.com/documentation/metrics-and-data#packages',
62
72
  type: METRIC_TYPE_NUMERIC,
63
73
  biggerIsBetter: false,
64
74
  },
65
- [MetricId.DuplicatePackagesCount]: {
75
+ duplicatePackagesCount: {
66
76
  label: 'Duplicate Packages',
67
77
  description: 'Total number of packages that are duplicated.',
68
78
  url: 'https://relative-ci.com/documentation/metrics-and-data#packages',
@@ -70,40 +80,33 @@ export const metrics = {
70
80
  biggerIsBetter: false,
71
81
  },
72
82
  sizes: {
73
- [SizeByTypeMetricId.JS]: {
83
+ totalSizeByTypeJS: {
74
84
  label: 'JS',
75
85
  type: METRIC_TYPE_FILE_SIZE,
76
- biggerIsBetter: false,
77
86
  },
78
- [SizeByTypeMetricId.CSS]: {
87
+ totalSizeByTypeCSS: {
79
88
  label: 'CSS',
80
89
  type: METRIC_TYPE_FILE_SIZE,
81
- biggerIsBetter: false,
82
90
  },
83
- [SizeByTypeMetricId.IMG]: {
91
+ totalSizeByTypeIMG: {
84
92
  label: 'IMG',
85
93
  type: METRIC_TYPE_FILE_SIZE,
86
- biggerIsBetter: false,
87
94
  },
88
- [SizeByTypeMetricId.Media]: {
95
+ totalSizeByTypeMEDIA: {
89
96
  label: 'Media',
90
97
  type: METRIC_TYPE_FILE_SIZE,
91
- biggerIsBetter: false,
92
98
  },
93
- [SizeByTypeMetricId.Font]: {
99
+ totalSizeByTypeFONT: {
94
100
  label: 'Fonts',
95
101
  type: METRIC_TYPE_FILE_SIZE,
96
- biggerIsBetter: false,
97
102
  },
98
- [SizeByTypeMetricId.HTML]: {
103
+ totalSizeByTypeHTML: {
99
104
  label: 'HTML',
100
105
  type: METRIC_TYPE_FILE_SIZE,
101
- biggerIsBetter: false,
102
106
  },
103
- [SizeByTypeMetricId.Other]: {
107
+ totalSizeByTypeOTHER: {
104
108
  label: 'Other',
105
109
  type: METRIC_TYPE_FILE_SIZE,
106
- biggerIsBetter: false,
107
110
  },
108
111
  },
109
112
  };
@@ -13,6 +13,7 @@ export namespace ASSET_FILTERS {
13
13
  export namespace MODULE_FILTERS {
14
14
  const CHANGED_1: string;
15
15
  export { CHANGED_1 as CHANGED };
16
+ export const DUPLICATED: string;
16
17
  export const FIRST_PARTY: string;
17
18
  export const THIRD_PARTY: string;
18
19
  }
@@ -40,12 +40,15 @@ export interface Metric {
40
40
  export interface ModuleMetric extends Metric {
41
41
  name: string;
42
42
  chunkIds: Array<String>;
43
+ duplicated: boolean;
43
44
  }
44
45
  export interface PackageMetric extends Metric {
45
46
  path: string;
46
47
  }
47
48
  export interface WebpackMetricsModules {
48
49
  metrics: {
50
+ duplicateCode: MetricRun;
51
+ duplicateModulesCount: MetricRun;
49
52
  modules: Record<string, ModuleMetric>;
50
53
  };
51
54
  }
package/types/i18n.d.ts CHANGED
@@ -6,6 +6,7 @@ declare namespace _default {
6
6
  const COMPONENT_LINK_BUNDLE_ASSETS_COUNT: string;
7
7
  const COMPONENT_LINK_BUNDLE_ASSETS_CHUNK_COUNT: string;
8
8
  const COMPONENT_LINK_MODULES: string;
9
+ const COMPONENT_LINK_MODULES_DUPLICATE: string;
9
10
  const COMPONENT_LINK_CHUNK_MODULES: string;
10
11
  const COMPONENT_LINK_PACKAGES_COUNT: string;
11
12
  const COMPONENT_LINK_PACKAGES_DUPLICATE: string;
@@ -86,6 +86,20 @@ export namespace BUNDLE_MODULES {
86
86
  };
87
87
  export { params_5 as params };
88
88
  }
89
+ export namespace BUNDLE_MODULES_DUPLICATE {
90
+ const section_7: string;
91
+ export { section_7 as section };
92
+ const title_7: string;
93
+ export { title_7 as title };
94
+ const params_6: {
95
+ [x: string]: {
96
+ filters: {
97
+ [x: string]: boolean;
98
+ };
99
+ };
100
+ };
101
+ export { params_6 as params };
102
+ }
89
103
  export function getBundleModulesBySearch(search: any): {
90
104
  section: string;
91
105
  title: string;
@@ -108,32 +122,32 @@ export function getBundleModulesByChunk(chunkIds: any, chunkId: any, fileType?:
108
122
  };
109
123
  };
110
124
  export namespace BUNLDE_PACKAGES_COUNT {
111
- const section_7: string;
112
- export { section_7 as section };
113
- const title_7: string;
114
- export { title_7 as title };
115
- const params_6: {
125
+ const section_8: string;
126
+ export { section_8 as section };
127
+ const title_8: string;
128
+ export { title_8 as title };
129
+ const params_7: {
116
130
  [x: string]: {
117
131
  filters: {
118
132
  [x: string]: boolean;
119
133
  };
120
134
  };
121
135
  };
122
- export { params_6 as params };
136
+ export { params_7 as params };
123
137
  }
124
138
  export namespace BUNDLE_PACKAGES_DUPLICATE {
125
- const section_8: string;
126
- export { section_8 as section };
127
- const title_8: string;
128
- export { title_8 as title };
129
- const params_7: {
139
+ const section_9: string;
140
+ export { section_9 as section };
141
+ const title_9: string;
142
+ export { title_9 as title };
143
+ const params_8: {
130
144
  [x: string]: {
131
145
  filters: {
132
146
  [x: string]: boolean;
133
147
  };
134
148
  };
135
149
  };
136
- export { params_7 as params };
150
+ export { params_8 as params };
137
151
  }
138
152
  export function getBundleAssetsFileTypeComponentLink(fileType: any, label: any): {
139
153
  section: string;
@@ -10,6 +10,8 @@ export declare enum MetricId {
10
10
  TotalInitialSizeCSS = "totalInitialSizeCSS",
11
11
  CacheInvalidation = "cacheInvalidation",
12
12
  ModuleName = "moduleCount",
13
+ DuplicateModulesCount = "duplicateModulesCount",
14
+ DuplicateCode = "duplicateCode",
13
15
  ChunkCount = "chunkCount",
14
16
  AssetCount = "assetCount",
15
17
  PackageCount = "packageCount",
@@ -48,6 +48,20 @@ export declare const metrics: {
48
48
  type: string;
49
49
  biggerIsBetter: boolean;
50
50
  };
51
+ duplicateModulesCount: {
52
+ label: string;
53
+ description: string;
54
+ url: string;
55
+ type: string;
56
+ biggerIsBetter: boolean;
57
+ };
58
+ duplicateCode: {
59
+ label: string;
60
+ description: string;
61
+ url: string;
62
+ type: string;
63
+ biggerIsBetter: boolean;
64
+ };
51
65
  packageCount: {
52
66
  label: string;
53
67
  description: string;
@@ -1,39 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const get_1 = __importDefault(require("lodash/get"));
7
- const utils_1 = require("../utils");
8
- function extractBudgetsInsights(_, currentExtractedData, __, options = {}) {
9
- const { budgets: budgetsOptions } = options;
10
- if (!budgetsOptions) {
11
- return null;
12
- }
13
- const insights = [];
14
- budgetsOptions.forEach((budgetOption) => {
15
- const { metric: budgetMetricId, value: budgetValue } = budgetOption;
16
- const currentValue = (0, get_1.default)(currentExtractedData, `metrics.${budgetMetricId}.value`);
17
- const metric = (0, utils_1.getMetricType)(budgetMetricId);
18
- if (!currentValue) {
19
- return;
20
- }
21
- const isValueOverBudget = currentValue > budgetValue;
22
- const budgetInsight = {
23
- currentValue,
24
- budgetValue,
25
- failed: metric.biggerIsBetter ? !isValueOverBudget : isValueOverBudget,
26
- };
27
- insights.push([budgetMetricId, budgetInsight]);
28
- });
29
- if (insights.length === 0) {
30
- return null;
31
- }
32
- return {
33
- insights: {
34
- budgets: Object.fromEntries(insights),
35
- },
36
- };
37
- }
38
- exports.default = extractBudgetsInsights;
39
- //# sourceMappingURL=budgets-insights.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"budgets-insights.js","sourceRoot":"","sources":["../../../src/webpack/extract/budgets-insights.ts"],"names":[],"mappings":";;;;;AAAA,qDAA6B;AAE7B,oCAAyC;AA2BzC,SAAwB,sBAAsB,CAC5C,CAAU,EACV,oBAAyB,EACzB,EAAW,EACX,UAAmB,EAAE;IAErB,MAAM,EAAE,OAAO,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC;IAE5C,IAAI,CAAC,cAAc,EAAE;QACnB,OAAO,IAAI,CAAC;KACb;IAED,MAAM,QAAQ,GAAmC,EAAE,CAAC;IAEpD,cAAc,CAAC,OAAO,CAAC,CAAC,YAAY,EAAE,EAAE;QACtC,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,WAAW,EAAE,GAAG,YAAY,CAAC;QACpE,MAAM,YAAY,GAAG,IAAA,aAAG,EAAC,oBAAoB,EAAE,WAAW,cAAc,QAAQ,CAAC,CAAC;QAClF,MAAM,MAAM,GAAG,IAAA,qBAAa,EAAC,cAAc,CAAC,CAAC;QAE7C,IAAI,CAAC,YAAY,EAAE;YACjB,OAAO;SACR;QAED,MAAM,iBAAiB,GAAG,YAAY,GAAG,WAAW,CAAC;QAErD,MAAM,aAAa,GAAG;YACpB,YAAY;YACZ,WAAW;YACX,MAAM,EAAE,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,iBAAiB;SACvE,CAAC;QAEF,QAAQ,CAAC,IAAI,CAAC,CAAC,cAAc,EAAE,aAAa,CAAC,CAAC,CAAC;IACjD,CAAC,CAAC,CAAC;IAEH,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE;QACzB,OAAO,IAAI,CAAC;KACb;IAED,OAAO;QACL,QAAQ,EAAE;YACR,OAAO,EAAE,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC;SACtC;KACF,CAAC;AACJ,CAAC;AA3CD,yCA2CC"}
@@ -1,33 +0,0 @@
1
- import get from 'lodash/get';
2
- import { getMetricType } from '../utils';
3
- export default function extractBudgetsInsights(_, currentExtractedData, __, options = {}) {
4
- const { budgets: budgetsOptions } = options;
5
- if (!budgetsOptions) {
6
- return null;
7
- }
8
- const insights = [];
9
- budgetsOptions.forEach((budgetOption) => {
10
- const { metric: budgetMetricId, value: budgetValue } = budgetOption;
11
- const currentValue = get(currentExtractedData, `metrics.${budgetMetricId}.value`);
12
- const metric = getMetricType(budgetMetricId);
13
- if (!currentValue) {
14
- return;
15
- }
16
- const isValueOverBudget = currentValue > budgetValue;
17
- const budgetInsight = {
18
- currentValue,
19
- budgetValue,
20
- failed: metric.biggerIsBetter ? !isValueOverBudget : isValueOverBudget,
21
- };
22
- insights.push([budgetMetricId, budgetInsight]);
23
- });
24
- if (insights.length === 0) {
25
- return null;
26
- }
27
- return {
28
- insights: {
29
- budgets: Object.fromEntries(insights),
30
- },
31
- };
32
- }
33
- //# sourceMappingURL=budgets-insights.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"budgets-insights.js","sourceRoot":"","sources":["../../../src/webpack/extract/budgets-insights.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,YAAY,CAAC;AAE7B,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AA2BzC,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAC5C,CAAU,EACV,oBAAyB,EACzB,EAAW,EACX,UAAmB,EAAE;IAErB,MAAM,EAAE,OAAO,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC;IAE5C,IAAI,CAAC,cAAc,EAAE;QACnB,OAAO,IAAI,CAAC;KACb;IAED,MAAM,QAAQ,GAAmC,EAAE,CAAC;IAEpD,cAAc,CAAC,OAAO,CAAC,CAAC,YAAY,EAAE,EAAE;QACtC,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,WAAW,EAAE,GAAG,YAAY,CAAC;QACpE,MAAM,YAAY,GAAG,GAAG,CAAC,oBAAoB,EAAE,WAAW,cAAc,QAAQ,CAAC,CAAC;QAClF,MAAM,MAAM,GAAG,aAAa,CAAC,cAAc,CAAC,CAAC;QAE7C,IAAI,CAAC,YAAY,EAAE;YACjB,OAAO;SACR;QAED,MAAM,iBAAiB,GAAG,YAAY,GAAG,WAAW,CAAC;QAErD,MAAM,aAAa,GAAG;YACpB,YAAY;YACZ,WAAW;YACX,MAAM,EAAE,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,iBAAiB;SACvE,CAAC;QAEF,QAAQ,CAAC,IAAI,CAAC,CAAC,cAAc,EAAE,aAAa,CAAC,CAAC,CAAC;IACjD,CAAC,CAAC,CAAC;IAEH,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE;QACzB,OAAO,IAAI,CAAC;KACb;IAED,OAAO;QACL,QAAQ,EAAE;YACR,OAAO,EAAE,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC;SACtC;KACF,CAAC;AACJ,CAAC"}
@@ -1,143 +0,0 @@
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
- };
@@ -1 +0,0 @@
1
- export * as budgetInsights from './budgets-insights';