|
1 | 1 | import * as crypto from 'crypto';
|
2 | 2 | // eslint-disable-next-line import/no-extraneous-dependencies
|
3 | 3 | import type { Module, ReadOnlyGraph, SerializerOptions } from 'metro';
|
4 |
| -// eslint-disable-next-line import/no-extraneous-dependencies |
5 |
| -import type CountingSet from 'metro/src/lib/CountingSet'; |
| 4 | +import type CountingSet from 'metro/src/lib/CountingSet'; // types are in src but exports are in private |
6 | 5 |
|
7 | 6 | // Variant of MixedOutput
|
8 | 7 | // https://github.com/facebook/metro/blob/9b85f83c9cc837d8cd897aa7723be7da5b296067/packages/metro/src/DeltaBundler/types.flow.js#L21
|
@@ -84,16 +83,44 @@ export function determineDebugIdFromBundleSource(code: string): string | undefin
|
84 | 83 | * https://github.com/facebook/metro/blob/fc29a1177f883144674cf85a813b58567f69d545/packages/metro/src/lib/CountingSet.js
|
85 | 84 | */
|
86 | 85 | function resolveSetCreator(): () => CountingSet<string> {
|
| 86 | + const CountingSetFromPrivate = safeRequireCountingSetFromPrivate(); |
| 87 | + if (CountingSetFromPrivate) { |
| 88 | + return () => new CountingSetFromPrivate.default(); |
| 89 | + } |
| 90 | + |
| 91 | + const CountingSetFromSrc = safeRequireCountingSetFromSrc(); |
| 92 | + if (CountingSetFromSrc) { |
| 93 | + return () => new CountingSetFromSrc.default(); |
| 94 | + } |
| 95 | + |
| 96 | + return () => new Set() as unknown as CountingSet<string>; |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * CountingSet was added in Metro 0.72.0 before that NodeJS Set was used. |
| 101 | + * |
| 102 | + * https://github.com/facebook/metro/blob/fc29a1177f883144674cf85a813b58567f69d545/packages/metro/src/lib/CountingSet.js |
| 103 | + */ |
| 104 | +function safeRequireCountingSetFromSrc(): { default: new <T>() => CountingSet<T> } | undefined { |
| 105 | + try { |
| 106 | + // eslint-disable-next-line @typescript-eslint/no-var-requires, import/no-extraneous-dependencies |
| 107 | + return require('metro/src/lib/CountingSet'); |
| 108 | + } catch (e) { |
| 109 | + return undefined; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * CountingSet was moved to private in Metro 0.83.0. (all src exports were moved to private) |
| 115 | + * |
| 116 | + * https://github.com/facebook/metro/commit/ae6f42372ed361611b5672705f22081c2022cf28 |
| 117 | + */ |
| 118 | +function safeRequireCountingSetFromPrivate(): { default: new <T>() => CountingSet<T> } | undefined { |
87 | 119 | try {
|
88 | 120 | // eslint-disable-next-line @typescript-eslint/no-var-requires, import/no-extraneous-dependencies
|
89 |
| - const { default: MetroSet } = require('metro/src/lib/CountingSet'); |
90 |
| - return () => new MetroSet(); |
| 121 | + return require('metro/private/lib/CountingSet'); |
91 | 122 | } catch (e) {
|
92 |
| - if (e instanceof Error && 'code' in e && e.code === 'MODULE_NOT_FOUND') { |
93 |
| - return () => new Set() as unknown as CountingSet<string>; |
94 |
| - } else { |
95 |
| - throw e; |
96 |
| - } |
| 123 | + return undefined; |
97 | 124 | }
|
98 | 125 | }
|
99 | 126 |
|
|
0 commit comments