@atlaspack/utils 2.14.5-canary.49 → 2.14.5-canary.490

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 (197) hide show
  1. package/CHANGELOG.md +655 -0
  2. package/benchmark.js +23 -0
  3. package/dist/DefaultMap.js +41 -0
  4. package/dist/Deferred.js +16 -0
  5. package/dist/PromiseQueue.js +107 -0
  6. package/dist/TapStream.js +23 -0
  7. package/dist/alternatives.js +97 -0
  8. package/dist/ansi-html.js +12 -0
  9. package/dist/blob.js +29 -0
  10. package/dist/bundle-url.js +32 -0
  11. package/dist/collection.js +106 -0
  12. package/dist/config.js +137 -0
  13. package/dist/countLines.js +12 -0
  14. package/dist/debounce.js +15 -0
  15. package/dist/debug-tools.js +40 -0
  16. package/dist/dependency-location.js +22 -0
  17. package/dist/escape-html.js +19 -0
  18. package/dist/generateBuildMetrics.js +111 -0
  19. package/dist/generateCertificate.js +124 -0
  20. package/dist/getCertificate.js +13 -0
  21. package/dist/getExisting.js +20 -0
  22. package/dist/getModuleParts.js +27 -0
  23. package/dist/getRootDir.js +46 -0
  24. package/dist/getTimeId.js +13 -0
  25. package/dist/glob.js +129 -0
  26. package/dist/hash.js +45 -0
  27. package/dist/http-server.js +55 -0
  28. package/dist/index.js +148 -0
  29. package/dist/is-url.js +15 -0
  30. package/dist/isDirectoryInside.js +11 -0
  31. package/dist/objectHash.js +20 -0
  32. package/dist/openInBrowser.js +61 -0
  33. package/dist/parseCSSImport.js +14 -0
  34. package/dist/path.js +36 -0
  35. package/dist/prettifyTime.js +6 -0
  36. package/dist/prettyDiagnostic.js +108 -0
  37. package/dist/progress-message.js +37 -0
  38. package/dist/relativeBundlePath.js +13 -0
  39. package/dist/relativeUrl.js +11 -0
  40. package/dist/replaceBundleReferences.js +131 -0
  41. package/dist/schema.js +404 -0
  42. package/dist/shared-buffer.js +24 -0
  43. package/dist/sourcemap.js +121 -0
  44. package/dist/stream.js +69 -0
  45. package/dist/throttle.js +12 -0
  46. package/dist/urlJoin.js +22 -0
  47. package/lib/DefaultMap.js +42 -0
  48. package/lib/Deferred.js +30 -0
  49. package/lib/PromiseQueue.js +112 -0
  50. package/lib/TapStream.js +34 -0
  51. package/lib/alternatives.js +116 -0
  52. package/lib/ansi-html.js +20 -0
  53. package/lib/blob.js +40 -0
  54. package/lib/bundle-url.js +34 -0
  55. package/lib/collection.js +111 -0
  56. package/lib/config.js +173 -0
  57. package/lib/countLines.js +15 -0
  58. package/lib/debounce.js +18 -0
  59. package/lib/debug-tools.js +41 -0
  60. package/lib/dependency-location.js +21 -0
  61. package/lib/escape-html.js +22 -0
  62. package/lib/generateBuildMetrics.js +121 -0
  63. package/lib/generateCertificate.js +129 -0
  64. package/lib/getCertificate.js +18 -0
  65. package/lib/getExisting.js +25 -0
  66. package/lib/getModuleParts.js +30 -0
  67. package/lib/getRootDir.js +52 -0
  68. package/lib/getTimeId.js +10 -0
  69. package/lib/glob.js +109 -0
  70. package/lib/hash.js +50 -0
  71. package/lib/http-server.js +85 -0
  72. package/lib/index.js +642 -37310
  73. package/lib/is-url.js +24 -0
  74. package/lib/isDirectoryInside.js +18 -0
  75. package/lib/objectHash.js +26 -0
  76. package/lib/openInBrowser.js +76 -0
  77. package/lib/parseCSSImport.js +15 -0
  78. package/lib/path.js +39 -0
  79. package/lib/prettifyTime.js +9 -0
  80. package/lib/prettyDiagnostic.js +155 -0
  81. package/lib/progress-message.js +46 -0
  82. package/lib/relativeBundlePath.js +22 -0
  83. package/lib/relativeUrl.js +24 -0
  84. package/lib/replaceBundleReferences.js +182 -0
  85. package/lib/schema.js +352 -0
  86. package/lib/shared-buffer.js +31 -0
  87. package/lib/sourcemap.js +137 -0
  88. package/lib/stream.js +76 -0
  89. package/lib/throttle.js +15 -0
  90. package/lib/types/DefaultMap.d.ts +13 -0
  91. package/lib/types/Deferred.d.ts +8 -0
  92. package/lib/types/PromiseQueue.d.ts +25 -0
  93. package/lib/types/TapStream.d.ts +6 -0
  94. package/lib/types/alternatives.d.ts +3 -0
  95. package/lib/types/ansi-html.d.ts +1 -0
  96. package/lib/types/blob.d.ts +4 -0
  97. package/lib/types/bundle-url.d.ts +4 -0
  98. package/lib/types/collection.d.ts +33 -0
  99. package/lib/types/config.d.ts +17 -0
  100. package/lib/types/countLines.d.ts +1 -0
  101. package/lib/types/debounce.d.ts +1 -0
  102. package/lib/types/debug-tools.d.ts +9 -0
  103. package/lib/types/dependency-location.d.ts +14 -0
  104. package/lib/types/escape-html.d.ts +1 -0
  105. package/lib/types/generateBuildMetrics.d.ts +18 -0
  106. package/lib/types/generateCertificate.d.ts +5 -0
  107. package/lib/types/getCertificate.d.ts +5 -0
  108. package/lib/types/getExisting.d.ts +8 -0
  109. package/lib/types/getModuleParts.d.ts +4 -0
  110. package/lib/types/getRootDir.d.ts +2 -0
  111. package/lib/types/getTimeId.d.ts +1 -0
  112. package/lib/types/glob.d.ts +9 -0
  113. package/lib/types/hash.d.ts +7 -0
  114. package/lib/types/http-server.d.ts +19 -0
  115. package/lib/types/index.d.ts +49 -0
  116. package/lib/types/is-url.d.ts +1 -0
  117. package/lib/types/isDirectoryInside.d.ts +2 -0
  118. package/lib/types/objectHash.d.ts +3 -0
  119. package/lib/types/openInBrowser.d.ts +1 -0
  120. package/lib/types/parseCSSImport.d.ts +1 -0
  121. package/lib/types/path.d.ts +8 -0
  122. package/lib/types/prettifyTime.d.ts +1 -0
  123. package/lib/types/prettyDiagnostic.d.ts +18 -0
  124. package/lib/types/progress-message.d.ts +3 -0
  125. package/lib/types/relativeBundlePath.d.ts +4 -0
  126. package/lib/types/relativeUrl.d.ts +1 -0
  127. package/lib/types/replaceBundleReferences.d.ts +39 -0
  128. package/lib/types/schema.d.ts +107 -0
  129. package/lib/types/shared-buffer.d.ts +2 -0
  130. package/lib/types/sourcemap.d.ts +15 -0
  131. package/lib/types/stream.d.ts +8 -0
  132. package/lib/types/throttle.d.ts +1 -0
  133. package/lib/types/urlJoin.d.ts +5 -0
  134. package/lib/urlJoin.js +35 -0
  135. package/package.json +30 -21
  136. package/src/{DefaultMap.js → DefaultMap.ts} +8 -12
  137. package/src/Deferred.ts +26 -0
  138. package/src/{PromiseQueue.js → PromiseQueue.ts} +40 -35
  139. package/src/{TapStream.js → TapStream.ts} +8 -7
  140. package/src/{alternatives.js → alternatives.ts} +15 -16
  141. package/src/{ansi-html.js → ansi-html.ts} +1 -1
  142. package/src/{blob.js → blob.ts} +3 -5
  143. package/src/{bundle-url.js → bundle-url.ts} +3 -5
  144. package/src/{collection.js → collection.ts} +23 -20
  145. package/src/{config.js → config.ts} +21 -23
  146. package/src/{countLines.js → countLines.ts} +0 -2
  147. package/src/{debounce.js → debounce.ts} +3 -5
  148. package/src/debug-tools.ts +50 -0
  149. package/src/{dependency-location.js → dependency-location.ts} +15 -11
  150. package/src/{escape-html.js → escape-html.ts} +5 -3
  151. package/src/{generateBuildMetrics.js → generateBuildMetrics.ts} +19 -21
  152. package/src/{generateCertificate.js → generateCertificate.ts} +8 -6
  153. package/src/{getCertificate.js → getCertificate.ts} +6 -5
  154. package/src/{getExisting.js → getExisting.ts} +4 -3
  155. package/src/{getModuleParts.js → getModuleParts.ts} +3 -2
  156. package/src/{getRootDir.js → getRootDir.ts} +1 -3
  157. package/src/getTimeId.ts +12 -0
  158. package/src/{glob.js → glob.ts} +13 -14
  159. package/src/{hash.js → hash.ts} +23 -18
  160. package/src/{http-server.js → http-server.ts} +34 -40
  161. package/src/{index.js → index.ts} +14 -10
  162. package/src/{is-url.js → is-url.ts} +1 -2
  163. package/src/{isDirectoryInside.js → isDirectoryInside.ts} +1 -2
  164. package/src/{objectHash.js → objectHash.ts} +1 -4
  165. package/src/{openInBrowser.js → openInBrowser.ts} +3 -5
  166. package/src/{parseCSSImport.js → parseCSSImport.ts} +0 -2
  167. package/src/{path.js → path.ts} +2 -4
  168. package/src/{prettifyTime.js → prettifyTime.ts} +0 -2
  169. package/src/{prettyDiagnostic.js → prettyDiagnostic.ts} +48 -25
  170. package/src/progress-message.ts +50 -0
  171. package/src/{relativeBundlePath.js → relativeBundlePath.ts} +4 -4
  172. package/src/{relativeUrl.js → relativeUrl.ts} +0 -1
  173. package/src/{replaceBundleReferences.js → replaceBundleReferences.ts} +67 -51
  174. package/src/schema.ts +552 -0
  175. package/src/{shared-buffer.js → shared-buffer.ts} +5 -3
  176. package/src/{sourcemap.js → sourcemap.ts} +28 -10
  177. package/src/{stream.js → stream.ts} +30 -22
  178. package/src/throttle.ts +13 -0
  179. package/src/{urlJoin.js → urlJoin.ts} +1 -3
  180. package/test/{DefaultMap.test.js → DefaultMap.test.ts} +4 -6
  181. package/test/{PromiseQueue.test.js → PromiseQueue.test.ts} +5 -6
  182. package/test/{collection.test.js → collection.test.ts} +0 -2
  183. package/test/{config.test.js → config.test.ts} +0 -3
  184. package/test/{objectHash.test.js → objectHash.test.ts} +4 -5
  185. package/test/{prettifyTime.test.js → prettifyTime.test.ts} +0 -1
  186. package/test/{replaceBundleReferences.test.js → replaceBundleReferences.test.ts} +0 -32
  187. package/test/schema.test.ts +748 -0
  188. package/test/{sourcemap.test.js → sourcemap.test.ts} +0 -1
  189. package/test/{throttle.test.js → throttle.test.ts} +1 -3
  190. package/test/{urlJoin.test.js → urlJoin.test.ts} +0 -2
  191. package/tsconfig.json +36 -0
  192. package/tsconfig.tsbuildinfo +1 -0
  193. package/lib/index.js.map +0 -1
  194. package/src/Deferred.js +0 -23
  195. package/src/progress-message.js +0 -22
  196. package/src/schema.js +0 -504
  197. package/src/throttle.js +0 -15
@@ -1,9 +1,7 @@
1
- // @flow strict-local
2
-
3
1
  export class DefaultMap<K, V> extends Map<K, V> {
4
- _getDefault: (K) => V;
2
+ _getDefault: (arg1: K) => V;
5
3
 
6
- constructor(getDefault: (K) => V, entries?: Iterable<[K, V]>) {
4
+ constructor(getDefault: (arg1: K) => V, entries?: Iterable<[K, V]>) {
7
5
  super(entries);
8
6
  this._getDefault = getDefault;
9
7
  }
@@ -17,8 +15,7 @@ export class DefaultMap<K, V> extends Map<K, V> {
17
15
  this.set(key, ret);
18
16
  }
19
17
 
20
- // $FlowFixMe
21
- return ret;
18
+ return ret as V;
22
19
  }
23
20
  }
24
21
 
@@ -26,11 +23,11 @@ interface Key {}
26
23
 
27
24
  // Duplicated from DefaultMap implementation for Flow
28
25
  // Roughly mirrors https://github.com/facebook/flow/blob/2eb5a78d92c167117ba9caae070afd2b9f598599/lib/core.js#L617
29
- export class DefaultWeakMap<K: Key, V> extends WeakMap<K, V> {
30
- _getDefault: (K) => V;
26
+ export class DefaultWeakMap<K extends Key, V> extends WeakMap<K, V> {
27
+ _getDefault: (arg1: K) => V;
31
28
 
32
- constructor(getDefault: (K) => V, entries?: Iterable<[K, V]>) {
33
- super(entries);
29
+ constructor(getDefault: (arg1: K) => V, entries?: Iterable<[K, V]>) {
30
+ super(entries as Iterable<readonly [K, V]>);
34
31
  this._getDefault = getDefault;
35
32
  }
36
33
 
@@ -43,7 +40,6 @@ export class DefaultWeakMap<K: Key, V> extends WeakMap<K, V> {
43
40
  this.set(key, ret);
44
41
  }
45
42
 
46
- // $FlowFixMe
47
- return ret;
43
+ return ret as V;
48
44
  }
49
45
  }
@@ -0,0 +1,26 @@
1
+ import invariant from 'assert';
2
+
3
+ export type Deferred<T> = {
4
+ resolve(arg1: T): void;
5
+ reject(arg1: unknown): void;
6
+ };
7
+
8
+ export function makeDeferredWithPromise<T>(): {
9
+ deferred: Deferred<T>;
10
+ promise: Promise<T>;
11
+ } {
12
+ let deferred: Deferred<T> | null | undefined;
13
+ let promise = new Promise<T>(
14
+ (
15
+ resolve: (result: Promise<T> | T) => void,
16
+ reject: (error?: any) => void,
17
+ ) => {
18
+ deferred = {resolve, reject};
19
+ },
20
+ );
21
+
22
+ // Promise constructor callback executes synchronously, so this is defined
23
+ invariant(deferred != null);
24
+
25
+ return {deferred, promise};
26
+ }
@@ -1,16 +1,16 @@
1
- // @flow strict-local
2
-
3
1
  import {makeDeferredWithPromise, type Deferred} from './Deferred';
4
2
 
5
- type PromiseQueueOpts = {|maxConcurrent: number|};
3
+ type PromiseQueueOpts = {
4
+ maxConcurrent: number;
5
+ };
6
6
 
7
7
  export default class PromiseQueue<T> {
8
- _deferred: ?Deferred<Array<T>>;
8
+ _deferred: Deferred<Array<T>> | null | undefined;
9
9
  _maxConcurrent: number;
10
10
  _numRunning: number = 0;
11
11
  _queue: Array<() => Promise<void>> = [];
12
- _runPromise: ?Promise<Array<T>> = null;
13
- _error: mixed;
12
+ _runPromise: Promise<Array<T>> | null | undefined = null;
13
+ _error: unknown;
14
14
  _count: number = 0;
15
15
  _results: Array<T> = [];
16
16
  _addSubscriptions: Set<() => void> = new Set();
@@ -28,30 +28,35 @@ export default class PromiseQueue<T> {
28
28
  }
29
29
 
30
30
  add(fn: () => Promise<T>): void {
31
- new Promise((resolve, reject) => {
32
- let i = this._count++;
33
- let wrapped = () =>
34
- fn().then(
35
- (result) => {
36
- this._results[i] = result;
37
- resolve(result);
38
- },
39
- (err) => {
40
- reject(err);
41
- throw err;
42
- },
43
- );
44
-
45
- this._queue.push(wrapped);
46
-
47
- for (const addFn of this._addSubscriptions) {
48
- addFn();
49
- }
50
-
51
- if (this._numRunning > 0 && this._numRunning < this._maxConcurrent) {
52
- this._next();
53
- }
54
- }).catch(() => {});
31
+ new Promise(
32
+ (
33
+ resolve: (result: Promise<T> | T) => void,
34
+ reject: (error?: any) => void,
35
+ ) => {
36
+ let i = this._count++;
37
+ let wrapped = () =>
38
+ fn().then(
39
+ (result) => {
40
+ this._results[i] = result;
41
+ resolve(result);
42
+ },
43
+ (err) => {
44
+ reject(err);
45
+ throw err;
46
+ },
47
+ );
48
+
49
+ this._queue.push(wrapped);
50
+
51
+ for (const addFn of this._addSubscriptions) {
52
+ addFn();
53
+ }
54
+
55
+ if (this._numRunning > 0 && this._numRunning < this._maxConcurrent) {
56
+ this._next();
57
+ }
58
+ },
59
+ ).catch(() => {});
55
60
  }
56
61
 
57
62
  subscribeToAdd(fn: () => void): () => void {
@@ -71,9 +76,9 @@ export default class PromiseQueue<T> {
71
76
  return Promise.resolve([]);
72
77
  }
73
78
 
74
- let {deferred, promise} = makeDeferredWithPromise();
79
+ let {deferred, promise} = makeDeferredWithPromise<Array<T>>();
75
80
  this._deferred = deferred;
76
- this._runPromise = promise;
81
+ this._runPromise = promise as Promise<Array<T>>;
77
82
 
78
83
  while (this._queue.length && this._numRunning < this._maxConcurrent) {
79
84
  this._next();
@@ -84,7 +89,7 @@ export default class PromiseQueue<T> {
84
89
 
85
90
  async _next(): Promise<void> {
86
91
  let fn = this._queue.shift();
87
- await this._runFn(fn);
92
+ await this._runFn(fn as () => unknown);
88
93
  if (this._queue.length) {
89
94
  this._next();
90
95
  } else if (this._numRunning === 0) {
@@ -92,11 +97,11 @@ export default class PromiseQueue<T> {
92
97
  }
93
98
  }
94
99
 
95
- async _runFn(fn: () => mixed): Promise<void> {
100
+ async _runFn(fn: () => unknown): Promise<void> {
96
101
  this._numRunning++;
97
102
  try {
98
103
  await fn();
99
- } catch (e) {
104
+ } catch (e: any) {
100
105
  // Only store the first error that occurs.
101
106
  // We don't reject immediately so that any other concurrent
102
107
  // requests have time to complete.
@@ -1,5 +1,3 @@
1
- // @flow strict-local
2
-
3
1
  import {Transform} from 'stream';
4
2
 
5
3
  /*
@@ -7,21 +5,24 @@ import {Transform} from 'stream';
7
5
  * callback. Continues to pass data chunks down the stream.
8
6
  */
9
7
  export default class TapStream extends Transform {
10
- _tap: (Buffer) => mixed;
11
- constructor(tap: (Buffer) => mixed, options: mixed) {
12
- super({...options});
8
+ _tap: (arg1: Buffer) => unknown;
9
+ constructor(tap: (arg1: Buffer) => unknown, options: unknown) {
10
+ super({...(options as any)});
13
11
  this._tap = tap;
14
12
  }
15
13
 
16
14
  _transform(
17
15
  chunk: Buffer | string,
18
16
  encoding: string,
19
- callback: (err: ?Error, chunk?: Buffer | string) => mixed,
17
+ callback: (
18
+ err?: Error | null | undefined,
19
+ chunk?: Buffer | string,
20
+ ) => unknown,
20
21
  ) {
21
22
  try {
22
23
  this._tap(Buffer.from(chunk));
23
24
  callback(null, chunk);
24
- } catch (err) {
25
+ } catch (err: any) {
25
26
  callback(err);
26
27
  }
27
28
  }
@@ -1,6 +1,5 @@
1
- // @flow
2
1
  import path from 'path';
3
- import type {FileSystem} from '@atlaspack/types';
2
+ import type {FileSystem} from '@atlaspack/types-internal';
4
3
  import {fuzzySearch} from './schema';
5
4
  import {relativePath} from './path';
6
5
  import {resolveConfig} from './config';
@@ -48,7 +47,7 @@ export async function findAlternativeNodeModules(
48
47
  potentialModules.push(...modules);
49
48
  }
50
49
  }
51
- } catch (err) {
50
+ } catch (err: any) {
52
51
  // ignore
53
52
  }
54
53
 
@@ -68,16 +67,16 @@ async function findAllFilesUp({
68
67
  collected,
69
68
  leadingDotSlash = true,
70
69
  includeDirectories = true,
71
- }: {|
72
- fs: FileSystem,
73
- dir: string,
74
- root: string,
75
- basedir: string,
76
- maxlength: number,
77
- collected: Array<string>,
78
- leadingDotSlash?: boolean,
79
- includeDirectories?: boolean,
80
- |}): Promise<mixed> {
70
+ }: {
71
+ fs: FileSystem;
72
+ dir: string;
73
+ root: string;
74
+ basedir: string;
75
+ maxlength: number;
76
+ collected: Array<string>;
77
+ leadingDotSlash?: boolean;
78
+ includeDirectories?: boolean;
79
+ }): Promise<unknown> {
81
80
  let dirContent = (await fs.readdir(dir)).sort();
82
81
  return Promise.all(
83
82
  dirContent.map(async (item) => {
@@ -111,9 +110,9 @@ export async function findAlternativeFiles(
111
110
  fileSpecifier: string,
112
111
  dir: string,
113
112
  projectRoot: string,
114
- leadingDotSlash?: boolean = true,
115
- includeDirectories?: boolean = true,
116
- includeExtension?: boolean = false,
113
+ leadingDotSlash: boolean = true,
114
+ includeDirectories: boolean = true,
115
+ includeExtension: boolean = false,
117
116
  ): Promise<Array<string>> {
118
117
  let potentialFiles: Array<string> = [];
119
118
  // Find our root, we won't recommend files above the package root as that's bad practise
@@ -1,4 +1,4 @@
1
- // @flow strict-local
1
+ // @ts-expect-error ansi-html-community is not typed
2
2
  import ansiHTML from 'ansi-html-community';
3
3
  import {escapeHTML} from './escape-html';
4
4
 
@@ -1,6 +1,4 @@
1
- // @flow strict-local
2
-
3
- import type {Blob} from '@atlaspack/types';
1
+ import type {Blob} from '@atlaspack/types-internal';
4
2
 
5
3
  import {Buffer} from 'buffer';
6
4
  import {bufferStream} from './';
@@ -12,7 +10,7 @@ export function blobToBuffer(blob: Blob): Promise<Buffer> {
12
10
  } else if (blob instanceof Buffer) {
13
11
  return Promise.resolve(Buffer.from(blob));
14
12
  } else {
15
- return Promise.resolve(Buffer.from(blob, 'utf8'));
13
+ return Promise.resolve(Buffer.from(blob as string, 'utf8'));
16
14
  }
17
15
  }
18
16
 
@@ -22,6 +20,6 @@ export async function blobToString(blob: Blob): Promise<string> {
22
20
  } else if (blob instanceof Buffer) {
23
21
  return blob.toString();
24
22
  } else {
25
- return blob;
23
+ return blob as string;
26
24
  }
27
25
  }
@@ -1,6 +1,4 @@
1
- // @flow strict-local
2
-
3
- let bundleURL: ?string = null;
1
+ let bundleURL: string | null | undefined = null;
4
2
  function getBundleURLCached(): string {
5
3
  if (bundleURL == null) {
6
4
  bundleURL = _getBundleURL();
@@ -13,7 +11,7 @@ function _getBundleURL(): string {
13
11
  // Attempt to find the URL of the current script and use that as the base URL
14
12
  try {
15
13
  throw new Error();
16
- } catch (err) {
14
+ } catch (err: any) {
17
15
  let stack: string = typeof err.stack === 'string' ? err.stack : '';
18
16
  let matches = stack.match(/(https?|file|ftp):\/\/[^)\n]+/g);
19
17
  if (matches) {
@@ -24,7 +22,7 @@ function _getBundleURL(): string {
24
22
  return '/';
25
23
  }
26
24
 
27
- export function getBaseURL(url: ?string): string {
25
+ export function getBaseURL(url?: string | null): string {
28
26
  if (url == null) {
29
27
  return '/';
30
28
  }
@@ -1,20 +1,16 @@
1
- // @flow strict-local
2
-
3
1
  export function unique<T>(array: Array<T>): Array<T> {
4
2
  return [...new Set(array)];
5
3
  }
6
4
 
7
5
  export function objectSortedEntries(obj: {
8
- +[string]: mixed,
9
- ...
10
- }): Array<[string, mixed]> {
6
+ readonly [key: string]: unknown;
7
+ }): Array<[string, unknown]> {
11
8
  return Object.entries(obj).sort(([keyA], [keyB]) => keyA.localeCompare(keyB));
12
9
  }
13
10
 
14
11
  export function objectSortedEntriesDeep(object: {
15
- +[string]: mixed,
16
- ...
17
- }): Array<[string, mixed]> {
12
+ readonly [key: string]: unknown;
13
+ }): Array<[string, unknown]> {
18
14
  let sortedEntries = objectSortedEntries(object);
19
15
  for (let i = 0; i < sortedEntries.length; i++) {
20
16
  sortedEntries[i][1] = sortEntry(sortedEntries[i][1]);
@@ -22,13 +18,13 @@ export function objectSortedEntriesDeep(object: {
22
18
  return sortedEntries;
23
19
  }
24
20
 
25
- function sortEntry(entry: mixed) {
21
+ function sortEntry(entry: unknown): unknown {
26
22
  if (Array.isArray(entry)) {
27
23
  return entry.map(sortEntry);
28
24
  }
29
25
 
30
26
  if (typeof entry === 'object' && entry != null) {
31
- return objectSortedEntriesDeep(entry);
27
+ return objectSortedEntriesDeep(entry as {readonly [key: string]: unknown});
32
28
  }
33
29
 
34
30
  return entry;
@@ -44,11 +40,8 @@ function sortEntry(entry: mixed) {
44
40
  * @param {*} b Set B
45
41
  * @returns A \ B
46
42
  */
47
- export function setDifference<T>(
48
- a: $ReadOnlySet<T>,
49
- b: $ReadOnlySet<T>,
50
- ): Set<T> {
51
- let difference = new Set();
43
+ export function setDifference<T>(a: ReadonlySet<T>, b: ReadonlySet<T>): Set<T> {
44
+ let difference = new Set<T>();
52
45
  for (let e of a) {
53
46
  if (!b.has(e)) {
54
47
  difference.add(e);
@@ -69,10 +62,10 @@ export function setDifference<T>(
69
62
  * @returns A Δ B
70
63
  */
71
64
  export function setSymmetricDifference<T>(
72
- a: $ReadOnlySet<T>,
73
- b: $ReadOnlySet<T>,
65
+ a: ReadonlySet<T>,
66
+ b: ReadonlySet<T>,
74
67
  ): Set<T> {
75
- let difference = new Set();
68
+ let difference = new Set<T>();
76
69
  for (let e of a) {
77
70
  if (!b.has(e)) {
78
71
  difference.add(e);
@@ -86,7 +79,7 @@ export function setSymmetricDifference<T>(
86
79
  return difference;
87
80
  }
88
81
 
89
- export function setIntersect<T>(a: Set<T>, b: $ReadOnlySet<T>): void {
82
+ export function setIntersect<T>(a: Set<T>, b: ReadonlySet<T>): void {
90
83
  for (let entry of a) {
91
84
  if (!b.has(entry)) {
92
85
  a.delete(entry);
@@ -94,11 +87,21 @@ export function setIntersect<T>(a: Set<T>, b: $ReadOnlySet<T>): void {
94
87
  }
95
88
  }
96
89
 
90
+ export function setIntersectStatic<T>(a: Set<T>, b: Set<T>): Set<T> {
91
+ let intersection = new Set<T>();
92
+ for (let entry of a) {
93
+ if (b.has(entry)) {
94
+ intersection.add(entry);
95
+ }
96
+ }
97
+ return intersection;
98
+ }
99
+
97
100
  export function setUnion<T>(a: Iterable<T>, b: Iterable<T>): Set<T> {
98
101
  return new Set([...a, ...b]);
99
102
  }
100
103
 
101
- export function setEqual<T>(a: $ReadOnlySet<T>, b: $ReadOnlySet<T>): boolean {
104
+ export function setEqual<T>(a: ReadonlySet<T>, b: ReadonlySet<T>): boolean {
102
105
  if (a.size != b.size) {
103
106
  return false;
104
107
  }
@@ -1,23 +1,22 @@
1
- // @flow
2
-
3
- import type {ConfigResult, File, FilePath} from '@atlaspack/types';
4
- import type {FileSystem} from '@atlaspack/fs';
1
+ import type {ConfigResult, File, FilePath} from '@atlaspack/types-internal';
2
+ import type {FileSystem} from '@atlaspack/types-internal';
5
3
  import ThrowableDiagnostic from '@atlaspack/diagnostic';
6
4
  import path from 'path';
5
+ // @ts-expect-error TS7016
7
6
  import clone from 'clone';
8
7
  import json5 from 'json5';
9
8
  import {parse as toml} from '@iarna/toml';
10
- import LRU from 'lru-cache';
9
+ import {LRUCache as LRU} from 'lru-cache';
11
10
 
12
- export type ConfigOutput = {|
13
- config: ConfigResult,
14
- files: Array<File>,
15
- |};
11
+ export type ConfigOutput = {
12
+ config: ConfigResult;
13
+ files: Array<File>;
14
+ };
16
15
 
17
- export type ConfigOptions = {|
18
- parse?: boolean,
19
- parser?: (string) => any,
20
- |};
16
+ export type ConfigOptions = {
17
+ parse?: boolean;
18
+ parser?: (arg1: string) => any;
19
+ };
21
20
 
22
21
  const configCache = new LRU<FilePath, ConfigOutput>({max: 500});
23
22
  const resolveCache = new Map();
@@ -27,7 +26,7 @@ export function resolveConfig(
27
26
  filepath: FilePath,
28
27
  filenames: Array<FilePath>,
29
28
  projectRoot: FilePath,
30
- ): Promise<?FilePath> {
29
+ ): Promise<FilePath | null | undefined> {
31
30
  // Cache the result of resolving config for this directory.
32
31
  // This is automatically invalidated at the end of the current build.
33
32
  let key = path.dirname(filepath) + filenames.join(',');
@@ -50,7 +49,7 @@ export function resolveConfigSync(
50
49
  filepath: FilePath,
51
50
  filenames: Array<FilePath>,
52
51
  projectRoot: FilePath,
53
- ): ?FilePath {
52
+ ): FilePath | null | undefined {
54
53
  return fs.findAncestorFile(filenames, path.dirname(filepath), projectRoot);
55
54
  }
56
55
 
@@ -59,7 +58,7 @@ export async function loadConfig(
59
58
  filepath: FilePath,
60
59
  filenames: Array<FilePath>,
61
60
  projectRoot: FilePath,
62
- opts: ?ConfigOptions,
61
+ opts?: ConfigOptions | null,
63
62
  ): Promise<ConfigOutput | null> {
64
63
  let parse = opts?.parse ?? true;
65
64
  let configFile = await resolveConfig(fs, filepath, filenames, projectRoot);
@@ -73,7 +72,6 @@ export async function loadConfig(
73
72
  let extname = path.extname(configFile).slice(1);
74
73
  if (extname === 'js' || extname === 'cjs') {
75
74
  let output = {
76
- // $FlowFixMe
77
75
  config: clone(module.require(configFile)),
78
76
  files: [{filePath: configFile}],
79
77
  };
@@ -83,7 +81,7 @@ export async function loadConfig(
83
81
  }
84
82
 
85
83
  return readConfig(fs, configFile, opts);
86
- } catch (err) {
84
+ } catch (err: any) {
87
85
  if (err.code === 'MODULE_NOT_FOUND' || err.code === 'ENOENT') {
88
86
  return null;
89
87
  }
@@ -96,14 +94,14 @@ export async function loadConfig(
96
94
  }
97
95
 
98
96
  loadConfig.clear = () => {
99
- configCache.reset();
97
+ configCache.clear();
100
98
  resolveCache.clear();
101
99
  };
102
100
 
103
101
  export async function readConfig(
104
102
  fs: FileSystem,
105
103
  configFile: FilePath,
106
- opts: ?ConfigOptions,
104
+ opts?: ConfigOptions | null,
107
105
  ): Promise<ConfigOutput | null> {
108
106
  let parse = opts?.parse ?? true;
109
107
  let cachedOutput = configCache.get(String(parse) + configFile);
@@ -121,7 +119,7 @@ export async function readConfig(
121
119
  let parse = opts?.parser ?? getParser(extname);
122
120
  try {
123
121
  config = parse(configContent);
124
- } catch (e) {
122
+ } catch (e: any) {
125
123
  if (extname !== '' && extname !== 'json') {
126
124
  throw e;
127
125
  }
@@ -161,7 +159,7 @@ export async function readConfig(
161
159
 
162
160
  configCache.set(String(parse) + configFile, output);
163
161
  return output;
164
- } catch (err) {
162
+ } catch (err: any) {
165
163
  if (err.code === 'MODULE_NOT_FOUND' || err.code === 'ENOENT') {
166
164
  return null;
167
165
  }
@@ -170,7 +168,7 @@ export async function readConfig(
170
168
  }
171
169
  }
172
170
 
173
- function getParser(extname) {
171
+ function getParser(extname: string) {
174
172
  switch (extname) {
175
173
  case 'toml':
176
174
  return toml;
@@ -1,5 +1,3 @@
1
- // @flow strict-local
2
-
3
1
  export default function countLines(
4
2
  string: string,
5
3
  startIndex: number = 0,
@@ -1,10 +1,8 @@
1
- // @flow strict-local
2
-
3
- export default function debounce<TArgs: Array<mixed>>(
4
- fn: (...args: TArgs) => mixed,
1
+ export default function debounce<TArgs extends Array<unknown>>(
2
+ fn: (...args: TArgs) => unknown,
5
3
  delay: number,
6
4
  ): (...args: TArgs) => void {
7
- let timeout;
5
+ let timeout: NodeJS.Timeout | undefined | null;
8
6
 
9
7
  return function (...args: TArgs) {
10
8
  if (timeout) {
@@ -0,0 +1,50 @@
1
+ /*
2
+ * These tools are intended for Atlaspack developers, to provide extra utilities
3
+ * to make debugging Atlaspack issues more straightforward.
4
+ *
5
+ * To enable a tool, set the `ATLASPACK_DEBUG_TOOLS` environment variable to a
6
+ * comma-separated list of tool names. For example:
7
+ * `ATLASPACK_DEBUG_TOOLS="asset-file-names-in-output,simple-cli-reporter"`
8
+ *
9
+ * You can enable all tools by setting `ATLASPACK_DEBUG_TOOLS=all`.
10
+ */
11
+
12
+ type DebugTools = {
13
+ ['asset-file-names-in-output']: boolean;
14
+ ['simple-cli-reporter']: boolean;
15
+ ['bundle-stats']: boolean;
16
+ ['scope-hoisting-stats']: boolean;
17
+ ['debug-prelude']: boolean; // Native packager only, here to avoid warnings
18
+ };
19
+
20
+ export let debugTools: DebugTools = {
21
+ 'asset-file-names-in-output': false,
22
+ 'simple-cli-reporter': false,
23
+ 'bundle-stats': false,
24
+ 'scope-hoisting-stats': false,
25
+ 'debug-prelude': false,
26
+ };
27
+
28
+ const envVarValue = process.env.ATLASPACK_DEBUG_TOOLS ?? '';
29
+
30
+ for (let tool of envVarValue.split(',')) {
31
+ tool = tool.trim();
32
+
33
+ if (tool === 'all') {
34
+ for (let key in debugTools) {
35
+ debugTools[key as keyof DebugTools] = true;
36
+ }
37
+ break;
38
+ } else if (debugTools.hasOwnProperty(tool)) {
39
+ debugTools[tool as keyof DebugTools] = true;
40
+ } else if (tool === '') {
41
+ continue;
42
+ } else {
43
+ // eslint-disable-next-line no-console
44
+ console.warn(
45
+ `Unknown debug tool option: ${tool}. Valid options are: ${Object.keys(
46
+ debugTools,
47
+ ).join(', ')}`,
48
+ );
49
+ }
50
+ }
@@ -1,20 +1,24 @@
1
- // @flow
2
-
3
1
  export default function createDependencyLocation(
4
- start: {|
5
- line: number,
6
- column: number,
7
- |},
2
+ start: {
3
+ line: number;
4
+ column: number;
5
+ },
8
6
  specifier: string,
9
7
  lineOffset: number = 0,
10
8
  columnOffset: number = 0,
11
9
  // Imports are usually wrapped in quotes
12
10
  importWrapperLength: number = 2,
13
- ): {|
14
- end: {|column: number, line: number|},
15
- filePath: string,
16
- start: {|column: number, line: number|},
17
- |} {
11
+ ): {
12
+ end: {
13
+ column: number;
14
+ line: number;
15
+ };
16
+ filePath: string;
17
+ start: {
18
+ column: number;
19
+ line: number;
20
+ };
21
+ } {
18
22
  return {
19
23
  filePath: specifier,
20
24
  start: {