@atlaspack/bundler-default 2.14.5-canary.2 → 2.14.5-canary.200

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.
package/src/memoize.ts ADDED
@@ -0,0 +1,32 @@
1
+ import ManyKeysMap from 'many-keys-map';
2
+
3
+ let caches: Array<any> = [];
4
+
5
+ export function clearCaches() {
6
+ for (let cache of caches) {
7
+ cache.clear();
8
+ }
9
+ }
10
+
11
+ export function memoize<Args extends Array<unknown>, Return>(
12
+ fn: (...args: Args) => Return,
13
+ ): (...args: Args) => Return {
14
+ let cache = new ManyKeysMap();
15
+ caches.push(cache);
16
+
17
+ return function (...args: Args): Return {
18
+ // Navigate through the cache hierarchy
19
+ let cached = cache.get(args);
20
+ if (cached !== undefined) {
21
+ // If the result is cached, return it
22
+ return cached;
23
+ }
24
+
25
+ // Calculate the result and cache it
26
+ // @ts-expect-error TS2683
27
+ const result = fn.apply(this, args);
28
+ cache.set(args, result);
29
+
30
+ return result;
31
+ };
32
+ }
package/src/stats.ts ADDED
@@ -0,0 +1,97 @@
1
+ import {relative} from 'path';
2
+ import {NodeId} from '@atlaspack/graph';
3
+ import {DefaultMap, debugTools} from '@atlaspack/utils';
4
+
5
+ import {Bundle} from './idealGraph';
6
+
7
+ interface MergedBundle {
8
+ id: NodeId;
9
+ reason: string;
10
+ }
11
+
12
+ export class Stats {
13
+ projectRoot: string;
14
+ merges: DefaultMap<NodeId, MergedBundle[]> = new DefaultMap(() => []);
15
+
16
+ constructor(projectRoot: string) {
17
+ this.projectRoot = projectRoot;
18
+ }
19
+
20
+ trackMerge(bundleToKeep: NodeId, bundleToRemove: NodeId, reason: string) {
21
+ if (!debugTools['bundle-stats']) {
22
+ return;
23
+ }
24
+
25
+ this.merges
26
+ .get(bundleToKeep)
27
+ .push(...this.merges.get(bundleToRemove), {id: bundleToRemove, reason});
28
+ this.merges.delete(bundleToRemove);
29
+ }
30
+
31
+ getBundleLabel(bundle: Bundle): string {
32
+ if (bundle.manualSharedBundle) {
33
+ return bundle.manualSharedBundle;
34
+ }
35
+
36
+ if (bundle.mainEntryAsset) {
37
+ let relativePath = relative(
38
+ this.projectRoot,
39
+ bundle.mainEntryAsset.filePath,
40
+ );
41
+
42
+ if (relativePath.length > 100) {
43
+ relativePath =
44
+ relativePath.slice(0, 50) + '...' + relativePath.slice(-50);
45
+ }
46
+
47
+ return relativePath;
48
+ }
49
+
50
+ return `shared`;
51
+ }
52
+
53
+ report(getBundle: (bundleId: NodeId) => Bundle | null | undefined): void {
54
+ if (!debugTools['bundle-stats']) {
55
+ return;
56
+ }
57
+
58
+ type MergeResult = Record<string, string | number>;
59
+ let mergeResults: Array<MergeResult> = [];
60
+
61
+ let totals: Record<string, string | number> = {
62
+ label: 'Totals',
63
+ merges: 0,
64
+ };
65
+
66
+ for (let [bundleId, mergedBundles] of this.merges) {
67
+ let bundle = getBundle(bundleId);
68
+ if (!bundle) {
69
+ continue;
70
+ }
71
+
72
+ let result: MergeResult = {
73
+ label: this.getBundleLabel(bundle),
74
+ size: bundle.size,
75
+ merges: mergedBundles.length,
76
+ };
77
+
78
+ for (let merged of mergedBundles) {
79
+ result[merged.reason] = ((result[merged.reason] as number) || 0) + 1;
80
+ totals[merged.reason] = ((totals[merged.reason] as number) || 0) + 1;
81
+ }
82
+
83
+ (totals.merges as number) += mergedBundles.length;
84
+ mergeResults.push(result);
85
+ }
86
+
87
+ mergeResults.sort((a, b) => {
88
+ // Sort by bundle size descending
89
+ return (b.size as number) - (a.size as number);
90
+ });
91
+
92
+ mergeResults.push(totals);
93
+
94
+ // eslint-disable-next-line no-console
95
+ console.table(mergeResults);
96
+ }
97
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "../../../tsconfig.json",
3
+ "include": ["src"]
4
+ }