metro 0.84.2 → 0.84.4

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 (42) hide show
  1. package/package.json +15 -16
  2. package/src/Assets.js +20 -13
  3. package/src/Assets.js.flow +20 -13
  4. package/src/Bundler/util.js.flow +3 -3
  5. package/src/DeltaBundler/DeltaCalculator.d.ts +1 -11
  6. package/src/DeltaBundler/DeltaCalculator.js +55 -46
  7. package/src/DeltaBundler/DeltaCalculator.js.flow +72 -61
  8. package/src/DeltaBundler/getTransformCacheKey.js +3 -1
  9. package/src/DeltaBundler/getTransformCacheKey.js.flow +7 -2
  10. package/src/HmrServer.d.ts +15 -3
  11. package/src/HmrServer.js +7 -0
  12. package/src/HmrServer.js.flow +15 -5
  13. package/src/ModuleGraph/worker/collectDependencies.js +52 -0
  14. package/src/ModuleGraph/worker/collectDependencies.js.flow +67 -0
  15. package/src/Server.d.ts +4 -1
  16. package/src/Server.js +42 -5
  17. package/src/Server.js.flow +45 -5
  18. package/src/index.d.ts +22 -3
  19. package/src/index.flow.js +2 -2
  20. package/src/index.flow.js.flow +23 -4
  21. package/src/lib/JsonReporter.js.flow +2 -2
  22. package/src/lib/TerminalReporter.js +39 -41
  23. package/src/lib/TerminalReporter.js.flow +51 -32
  24. package/src/lib/getAppendScripts.js.flow +2 -2
  25. package/src/lib/logToConsole.js +8 -7
  26. package/src/lib/logToConsole.js.flow +7 -7
  27. package/src/lib/reporting.js +16 -7
  28. package/src/lib/reporting.js.flow +16 -5
  29. package/src/node-haste/DependencyGraph/ModuleResolution.d.ts +9 -22
  30. package/src/node-haste/DependencyGraph/ModuleResolution.js +4 -22
  31. package/src/node-haste/DependencyGraph/ModuleResolution.js.flow +10 -59
  32. package/src/node-haste/DependencyGraph/createFileMap.js +1 -2
  33. package/src/node-haste/DependencyGraph/createFileMap.js.flow +4 -3
  34. package/src/node-haste/DependencyGraph.d.ts +2 -5
  35. package/src/node-haste/DependencyGraph.js +22 -11
  36. package/src/node-haste/DependencyGraph.js.flow +24 -13
  37. package/src/node-haste/PackageCache.d.ts +12 -16
  38. package/src/node-haste/PackageCache.js +65 -54
  39. package/src/node-haste/PackageCache.js.flow +103 -79
  40. package/src/node-haste/Package.d.ts +0 -28
  41. package/src/node-haste/Package.js +0 -28
  42. package/src/node-haste/Package.js.flow +0 -39
@@ -66,7 +66,7 @@ function send(sendFns: Array<(string) => void>, message: HmrMessage): void {
66
66
  * getting connected, disconnected or having errors (through the
67
67
  * `onClientConnect`, `onClientDisconnect` and `onClientError` methods).
68
68
  */
69
- export default class HmrServer<TClient: Client> {
69
+ export default class HmrServer<TClient extends Client> {
70
70
  _config: ConfigT;
71
71
  _bundler: IncrementalBundler;
72
72
  _createModuleId: (path: string) => number;
@@ -181,7 +181,8 @@ export default class HmrServer<TClient: Client> {
181
181
  this._clientGroups.set(id, clientGroup);
182
182
 
183
183
  let latestChangeEvent: ?{
184
- logger: ?RootPerfLogger,
184
+ +logger: ?RootPerfLogger,
185
+ +changeId: string,
185
186
  } = null;
186
187
 
187
188
  const debounceCallHandleFileChange = debounceAsyncQueue(async () => {
@@ -240,6 +241,10 @@ export default class HmrServer<TClient: Client> {
240
241
  case 'log-opt-in':
241
242
  client.optedIntoHMR = true;
242
243
  break;
244
+ case 'heartbeat':
245
+ debug('Heartbeat received');
246
+ sendFn(String(message));
247
+ break;
243
248
  default:
244
249
  break;
245
250
  }
@@ -273,7 +278,8 @@ export default class HmrServer<TClient: Client> {
273
278
  group: ClientGroup,
274
279
  options: {isInitialUpdate: boolean},
275
280
  changeEvent: ?{
276
- logger: ?RootPerfLogger,
281
+ +logger: ?RootPerfLogger,
282
+ +changeId?: string,
277
283
  },
278
284
  ): Promise<void> {
279
285
  const logger = !options.isInitialUpdate ? changeEvent?.logger : null;
@@ -308,7 +314,10 @@ export default class HmrServer<TClient: Client> {
308
314
 
309
315
  const message = await this._prepareMessage(group, options, changeEvent);
310
316
  send(sendFns, message);
311
- send(sendFns, {type: 'update-done'});
317
+ send(sendFns, {
318
+ type: 'update-done',
319
+ body: {changeId: changeEvent?.changeId},
320
+ });
312
321
 
313
322
  log({
314
323
  ...createActionEndEntry(processingHmrChange),
@@ -328,7 +337,8 @@ export default class HmrServer<TClient: Client> {
328
337
  group: ClientGroup,
329
338
  options: {isInitialUpdate: boolean},
330
339
  changeEvent: ?{
331
- logger: ?RootPerfLogger,
340
+ +logger: ?RootPerfLogger,
341
+ +changeId?: string,
332
342
  },
333
343
  ): Promise<HmrUpdateMessage | HmrErrorMessage> {
334
344
  const logger = !options.isInitialUpdate ? changeEvent?.logger : null;
@@ -405,6 +405,9 @@ function isOptionalDependency(name, path, state) {
405
405
  if (!allowOptionalDependencies || isExcluded()) {
406
406
  return false;
407
407
  }
408
+ if (isInPromiseChainWithRejectionHandler(path)) {
409
+ return true;
410
+ }
408
411
  let sCount = 0;
409
412
  let p = path;
410
413
  while (p && sCount < 3) {
@@ -422,6 +425,55 @@ function isOptionalDependency(name, path, state) {
422
425
  }
423
426
  return false;
424
427
  }
428
+ function isInPromiseChainWithRejectionHandler(path) {
429
+ let current = path;
430
+ while (current.parentPath != null) {
431
+ const member = current.parentPath;
432
+ if (
433
+ member.node.type !== "MemberExpression" ||
434
+ member.node.object !== current.node ||
435
+ member.node.computed ||
436
+ member.node.property.type !== "Identifier" ||
437
+ member.parentPath == null
438
+ ) {
439
+ return false;
440
+ }
441
+ const call = member.parentPath;
442
+ if (
443
+ call.node.type !== "CallExpression" ||
444
+ call.node.callee !== member.node
445
+ ) {
446
+ return false;
447
+ }
448
+ const propertyName = member.node.property.name;
449
+ const args = call.node.arguments;
450
+ if (
451
+ propertyName === "catch" &&
452
+ args.length >= 1 &&
453
+ isNonNullishCallbackArg(args[0])
454
+ ) {
455
+ return true;
456
+ }
457
+ if (
458
+ propertyName === "then" &&
459
+ args.length >= 2 &&
460
+ isNonNullishCallbackArg(args[1])
461
+ ) {
462
+ return true;
463
+ }
464
+ current = call;
465
+ }
466
+ return false;
467
+ }
468
+ function isNonNullishCallbackArg(arg) {
469
+ if (arg.type === "NullLiteral") {
470
+ return false;
471
+ }
472
+ if (arg.type === "Identifier" && arg.name === "undefined") {
473
+ return false;
474
+ }
475
+ return true;
476
+ }
425
477
  function getModuleNameFromCallArgs(path) {
426
478
  const args = path.get("arguments");
427
479
  if (!Array.isArray(args) || args.length !== 1) {
@@ -630,6 +630,15 @@ function isOptionalDependency(
630
630
  return false;
631
631
  }
632
632
 
633
+ // Treat dynamic imports as optional when a rejection handler is attached
634
+ // close to the import call, e.g.
635
+ // import('x').catch(handler)
636
+ // import('x').then(handler, onReject)
637
+ // import('x').then(...).catch(handler)
638
+ if (isInPromiseChainWithRejectionHandler(path)) {
639
+ return true;
640
+ }
641
+
633
642
  // Valid statement stack for single-level try-block: expressionStatement -> blockStatement -> tryStatement
634
643
  let sCount = 0;
635
644
  let p: ?(NodePath<> | NodePath<BabelNode>) = path;
@@ -652,6 +661,64 @@ function isOptionalDependency(
652
661
  return false;
653
662
  }
654
663
 
664
+ // Walk up a chain of `.then(...)` / `.catch(...)` member calls starting from
665
+ // `path` (typically an `import()` CallExpression) and return true if any
666
+ // chained call provides a rejection handler — either `.catch(handler)` or
667
+ // `.then(_, handler)`. The chain must be unbroken: as soon as the parent is
668
+ // not a member call applied to the previous expression, we stop. This keeps
669
+ // the heuristic local to the import, matching the behaviour of the
670
+ // try/catch heuristic above.
671
+ function isInPromiseChainWithRejectionHandler(path: NodePath<>): boolean {
672
+ let current: NodePath<> = path;
673
+ while (current.parentPath != null) {
674
+ const member = current.parentPath;
675
+ if (
676
+ member.node.type !== 'MemberExpression' ||
677
+ member.node.object !== current.node ||
678
+ member.node.computed ||
679
+ member.node.property.type !== 'Identifier' ||
680
+ member.parentPath == null
681
+ ) {
682
+ return false;
683
+ }
684
+ const call = member.parentPath;
685
+ if (
686
+ call.node.type !== 'CallExpression' ||
687
+ call.node.callee !== member.node
688
+ ) {
689
+ return false;
690
+ }
691
+ const propertyName = member.node.property.name;
692
+ const args = call.node.arguments;
693
+ if (
694
+ propertyName === 'catch' &&
695
+ args.length >= 1 &&
696
+ isNonNullishCallbackArg(args[0])
697
+ ) {
698
+ return true;
699
+ }
700
+ if (
701
+ propertyName === 'then' &&
702
+ args.length >= 2 &&
703
+ isNonNullishCallbackArg(args[1])
704
+ ) {
705
+ return true;
706
+ }
707
+ current = call;
708
+ }
709
+ return false;
710
+ }
711
+
712
+ function isNonNullishCallbackArg(arg: BabelNode): boolean {
713
+ if (arg.type === 'NullLiteral') {
714
+ return false;
715
+ }
716
+ if (arg.type === 'Identifier' && arg.name === 'undefined') {
717
+ return false;
718
+ }
719
+ return true;
720
+ }
721
+
655
722
  function getModuleNameFromCallArgs(path: NodePath<CallExpression>): ?string {
656
723
  const args = path.get('arguments');
657
724
  if (!Array.isArray(args) || args.length !== 1) {
package/src/Server.d.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * @noformat
8
8
  * @oncall react_native
9
- * @generated SignedSource<<161e77301d04ce6cc254f1dbf15ef06b>>
9
+ * @generated SignedSource<<03b526801403adb05b3b0f6c25b25ed5>>
10
10
  *
11
11
  * This file was translated from Flow by scripts/generateTypeScriptDefinitions.js
12
12
  * Original file: packages/metro/src/Server.js
@@ -225,6 +225,9 @@ declare class Server {
225
225
  _explodedSourceMapForBundleOptions(
226
226
  bundleOptions: BundleOptions,
227
227
  ): Promise<ExplodedSourceMap>;
228
+ _resolveWatchFolderPrefix(
229
+ filePath: string,
230
+ ): {rootDir: string; filePath: string} | null;
228
231
  _resolveRelativePath(
229
232
  filePath: string,
230
233
  $$PARAM_1$$: Readonly<{
package/src/Server.js CHANGED
@@ -1347,6 +1347,35 @@ class Server {
1347
1347
  },
1348
1348
  );
1349
1349
  }
1350
+ _resolveWatchFolderPrefix(filePath) {
1351
+ const watchFolderMatch = filePath.match(
1352
+ /^\.\/\[metro-watchFolders\]\/(\d+)\/(.*)/,
1353
+ );
1354
+ if (watchFolderMatch != null) {
1355
+ const index = parseInt(watchFolderMatch[1], 10);
1356
+ const watchFolder = this._config.watchFolders[index];
1357
+ if (watchFolder != null) {
1358
+ return {
1359
+ rootDir: _path.default.resolve(watchFolder),
1360
+ filePath:
1361
+ "." +
1362
+ _path.default.sep +
1363
+ watchFolderMatch[2].split("/").join(_path.default.sep),
1364
+ };
1365
+ }
1366
+ }
1367
+ const projectMatch = filePath.match(/^\.\/\[metro-project\]\/(.*)/);
1368
+ if (projectMatch != null) {
1369
+ return {
1370
+ rootDir: _path.default.resolve(this._config.projectRoot),
1371
+ filePath:
1372
+ "." +
1373
+ _path.default.sep +
1374
+ projectMatch[1].split("/").join(_path.default.sep),
1375
+ };
1376
+ }
1377
+ return null;
1378
+ }
1350
1379
  async _resolveRelativePath(
1351
1380
  filePath,
1352
1381
  { relativeTo, resolverOptions, transformOptions },
@@ -1356,14 +1385,18 @@ class Server {
1356
1385
  transformOptions.platform,
1357
1386
  resolverOptions,
1358
1387
  );
1388
+ const resolved = this._resolveWatchFolderPrefix(filePath);
1359
1389
  const rootDir =
1360
- relativeTo === "server"
1361
- ? this._getServerRootDir()
1362
- : this._config.projectRoot;
1390
+ resolved != null
1391
+ ? resolved.rootDir
1392
+ : relativeTo === "server"
1393
+ ? this._getServerRootDir()
1394
+ : this._config.projectRoot;
1395
+ const resolvedFilePath = resolved != null ? resolved.filePath : filePath;
1363
1396
  return resolutionFn(`${rootDir}/.`, {
1364
- name: filePath,
1397
+ name: resolvedFilePath,
1365
1398
  data: {
1366
- key: filePath,
1399
+ key: resolvedFilePath,
1367
1400
  locs: [],
1368
1401
  asyncType: null,
1369
1402
  isESMImport: false,
@@ -1403,6 +1436,10 @@ class Server {
1403
1436
  return this._config.server.unstable_serverRoot ?? this._config.projectRoot;
1404
1437
  }
1405
1438
  _getEntryPointAbsolutePath(entryFile) {
1439
+ const resolved = this._resolveWatchFolderPrefix(entryFile);
1440
+ if (resolved != null) {
1441
+ return _path.default.resolve(resolved.rootDir, resolved.filePath);
1442
+ }
1406
1443
  return _path.default.resolve(this._getServerRootDir(), entryFile);
1407
1444
  }
1408
1445
  async ready() {
@@ -1622,6 +1622,33 @@ export default class Server {
1622
1622
  );
1623
1623
  }
1624
1624
 
1625
+ _resolveWatchFolderPrefix(
1626
+ filePath: string,
1627
+ ): {rootDir: string, filePath: string} | null {
1628
+ const watchFolderMatch = filePath.match(
1629
+ /^\.\/\[metro-watchFolders\]\/(\d+)\/(.*)/,
1630
+ );
1631
+ if (watchFolderMatch != null) {
1632
+ const index = parseInt(watchFolderMatch[1], 10);
1633
+ const watchFolder = this._config.watchFolders[index];
1634
+ if (watchFolder != null) {
1635
+ return {
1636
+ rootDir: path.resolve(watchFolder),
1637
+ filePath:
1638
+ '.' + path.sep + watchFolderMatch[2].split('/').join(path.sep),
1639
+ };
1640
+ }
1641
+ }
1642
+ const projectMatch = filePath.match(/^\.\/\[metro-project\]\/(.*)/);
1643
+ if (projectMatch != null) {
1644
+ return {
1645
+ rootDir: path.resolve(this._config.projectRoot),
1646
+ filePath: '.' + path.sep + projectMatch[1].split('/').join(path.sep),
1647
+ };
1648
+ }
1649
+ return null;
1650
+ }
1651
+
1625
1652
  async _resolveRelativePath(
1626
1653
  filePath: string,
1627
1654
  {
@@ -1639,13 +1666,22 @@ export default class Server {
1639
1666
  transformOptions.platform,
1640
1667
  resolverOptions,
1641
1668
  );
1669
+ const resolved = this._resolveWatchFolderPrefix(filePath);
1642
1670
  const rootDir =
1643
- relativeTo === 'server'
1644
- ? this._getServerRootDir()
1645
- : this._config.projectRoot;
1671
+ resolved != null
1672
+ ? resolved.rootDir
1673
+ : relativeTo === 'server'
1674
+ ? this._getServerRootDir()
1675
+ : this._config.projectRoot;
1676
+ const resolvedFilePath = resolved != null ? resolved.filePath : filePath;
1646
1677
  return resolutionFn(`${rootDir}/.`, {
1647
- name: filePath,
1648
- data: {key: filePath, locs: [], asyncType: null, isESMImport: false},
1678
+ name: resolvedFilePath,
1679
+ data: {
1680
+ key: resolvedFilePath,
1681
+ locs: [],
1682
+ asyncType: null,
1683
+ isESMImport: false,
1684
+ },
1649
1685
  }).filePath;
1650
1686
  }
1651
1687
 
@@ -1706,6 +1742,10 @@ export default class Server {
1706
1742
  }
1707
1743
 
1708
1744
  _getEntryPointAbsolutePath(entryFile: string): string {
1745
+ const resolved = this._resolveWatchFolderPrefix(entryFile);
1746
+ if (resolved != null) {
1747
+ return path.resolve(resolved.rootDir, resolved.filePath);
1748
+ }
1709
1749
  return path.resolve(this._getServerRootDir(), entryFile);
1710
1750
  }
1711
1751
 
package/src/index.d.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * @noformat
8
8
  * @oncall react_native
9
- * @generated SignedSource<<6246e792b8fc5f3e524f443a31921cfc>>
9
+ * @generated SignedSource<<3c6460427c6760887187d6b16151c609>>
10
10
  *
11
11
  * This file was translated from Flow by scripts/generateTypeScriptDefinitions.js
12
12
  * Original file: packages/metro/src/index.flow.js
@@ -125,9 +125,28 @@ type DependenciesCommandOptions = Readonly<{
125
125
  }> | null;
126
126
  export {Terminal, JsonReporter, TerminalReporter};
127
127
  export type {AssetData} from './Assets';
128
- export type {Reporter, ReportableEvent} from './lib/reporting';
128
+ export type {
129
+ AsyncDependencyType,
130
+ DeltaResult,
131
+ Dependency,
132
+ MixedOutput,
133
+ Module,
134
+ ReadOnlyDependencies,
135
+ ReadOnlyGraph,
136
+ SerializerOptions,
137
+ TransformInputOptions,
138
+ TransformResult,
139
+ TransformResultDependency,
140
+ } from './DeltaBundler/types';
141
+ export type {default as DependencyGraph} from './node-haste/DependencyGraph';
142
+ export type {BundleDetails, Reporter, ReportableEvent} from './lib/reporting';
129
143
  export type {TerminalReportableEvent} from './lib/TerminalReporter';
130
- export type {MetroConfig};
144
+ export type {
145
+ ContextMode,
146
+ RequireContextParams,
147
+ } from './ModuleGraph/worker/collectDependencies';
148
+ export type {ServerOptions} from './Server';
149
+ export type {MetroConfig, MetroServer};
131
150
  export declare function runMetro(
132
151
  config: InputConfigT,
133
152
  options?: RunMetroOptions,
package/src/index.flow.js CHANGED
@@ -63,7 +63,6 @@ var _TerminalReporter = _interopRequireDefault(
63
63
  );
64
64
  var _Server = _interopRequireDefault(require("./Server"));
65
65
  var outputBundle = _interopRequireWildcard(require("./shared/output/bundle"));
66
- var _chalk = _interopRequireDefault(require("chalk"));
67
66
  var _fs = _interopRequireDefault(require("fs"));
68
67
  var _http = _interopRequireDefault(require("http"));
69
68
  var _https = _interopRequireDefault(require("https"));
@@ -71,6 +70,7 @@ var _metroConfig = require("metro-config");
71
70
  var _metroCore = require("metro-core");
72
71
  var _net = _interopRequireDefault(require("net"));
73
72
  var _nullthrows = _interopRequireDefault(require("nullthrows"));
73
+ var _util = _interopRequireDefault(require("util"));
74
74
  function _interopRequireWildcard(e, t) {
75
75
  if ("function" == typeof WeakMap)
76
76
  var r = new WeakMap(),
@@ -201,7 +201,7 @@ const runServer = async (
201
201
  await earlyPortCheck(host, config.server.port);
202
202
  if (secure != null || secureCert != null || secureKey != null) {
203
203
  console.warn(
204
- _chalk.default.inverse.yellow.bold(" DEPRECATED "),
204
+ _util.default.styleText(["inverse", "yellow", "bold"], " DEPRECATED "),
205
205
  "The `secure`, `secureCert`, and `secureKey` options are now deprecated. " +
206
206
  "Please use the `secureServerOptions` object instead to pass options to " +
207
207
  "Metro's https development server, or `config.server.tls` in Metro's configuration",
@@ -41,7 +41,6 @@ import JsonReporter from './lib/JsonReporter';
41
41
  import TerminalReporter from './lib/TerminalReporter';
42
42
  import MetroServer from './Server';
43
43
  import * as outputBundle from './shared/output/bundle';
44
- import chalk from 'chalk';
45
44
  import fs from 'fs';
46
45
  import http from 'http';
47
46
  import https from 'https';
@@ -54,6 +53,7 @@ import {
54
53
  import {Terminal} from 'metro-core';
55
54
  import net from 'net';
56
55
  import nullthrows from 'nullthrows';
56
+ import util from 'util';
57
57
 
58
58
  const DEFAULTS = MetroServer.DEFAULT_BUNDLE_OPTIONS;
59
59
 
@@ -156,9 +156,28 @@ type DependenciesCommandOptions = Readonly<{[string]: unknown}> | null;
156
156
  export {Terminal, JsonReporter, TerminalReporter};
157
157
 
158
158
  export type {AssetData} from './Assets';
159
- export type {Reporter, ReportableEvent} from './lib/reporting';
159
+ export type {
160
+ AsyncDependencyType,
161
+ DeltaResult,
162
+ Dependency,
163
+ MixedOutput,
164
+ Module,
165
+ ReadOnlyDependencies,
166
+ ReadOnlyGraph,
167
+ SerializerOptions,
168
+ TransformInputOptions,
169
+ TransformResult,
170
+ TransformResultDependency,
171
+ } from './DeltaBundler/types';
172
+ export type {default as DependencyGraph} from './node-haste/DependencyGraph';
173
+ export type {BundleDetails, Reporter, ReportableEvent} from './lib/reporting';
160
174
  export type {TerminalReportableEvent} from './lib/TerminalReporter';
161
- export type {MetroConfig};
175
+ export type {
176
+ ContextMode,
177
+ RequireContextParams,
178
+ } from './ModuleGraph/worker/collectDependencies';
179
+ export type {ServerOptions} from './Server';
180
+ export type {MetroConfig, MetroServer};
162
181
 
163
182
  async function getConfig(config: InputConfigT): Promise<ConfigT> {
164
183
  const defaultConfig = await getDefaultConfig(config.projectRoot);
@@ -277,7 +296,7 @@ export const runServer = async (
277
296
  if (secure != null || secureCert != null || secureKey != null) {
278
297
  // eslint-disable-next-line no-console
279
298
  console.warn(
280
- chalk.inverse.yellow.bold(' DEPRECATED '),
299
+ util.styleText(['inverse', 'yellow', 'bold'], ' DEPRECATED '),
281
300
  'The `secure`, `secureCert`, and `secureKey` options are now deprecated. ' +
282
301
  'Please use the `secureServerOptions` object instead to pass options to ' +
283
302
  "Metro's https development server, or `config.server.tls` in Metro's configuration",
@@ -19,7 +19,7 @@ export type SerializedError = {
19
19
  ...
20
20
  };
21
21
 
22
- export type SerializedEvent<TEvent: {+[string]: unknown, ...}> =
22
+ export type SerializedEvent<TEvent extends {+[string]: unknown, ...}> =
23
23
  TEvent extends {
24
24
  error: Error,
25
25
  ...
@@ -31,7 +31,7 @@ export type SerializedEvent<TEvent: {+[string]: unknown, ...}> =
31
31
  }
32
32
  : TEvent;
33
33
 
34
- export default class JsonReporter<TEvent: {+[string]: unknown, ...}> {
34
+ export default class JsonReporter<TEvent extends {+[string]: unknown, ...}> {
35
35
  _stream: Writable;
36
36
 
37
37
  constructor(stream: Writable) {
@@ -7,10 +7,10 @@ exports.default = void 0;
7
7
  var _bundleProgressUtils = require("./bundleProgressUtils");
8
8
  var _logToConsole = _interopRequireDefault(require("./logToConsole"));
9
9
  var reporting = _interopRequireWildcard(require("./reporting"));
10
- var _chalk = _interopRequireDefault(require("chalk"));
11
10
  var _lodash = _interopRequireDefault(require("lodash.throttle"));
12
11
  var _metroCore = require("metro-core");
13
12
  var _path = _interopRequireDefault(require("path"));
13
+ var _util = _interopRequireDefault(require("util"));
14
14
  function _interopRequireWildcard(e, t) {
15
15
  if ("function" == typeof WeakMap)
16
16
  var r = new WeakMap(),
@@ -41,6 +41,7 @@ function _interopRequireWildcard(e, t) {
41
41
  function _interopRequireDefault(e) {
42
42
  return e && e.__esModule ? e : { default: e };
43
43
  }
44
+ const style = (format, text) => _util.default.styleText(format, text);
44
45
  const DARK_BLOCK_CHAR = "\u2593";
45
46
  const LIGHT_BLOCK_CHAR = "\u2591";
46
47
  const MAX_PROGRESS_BAR_CHAR_WIDTH = 16;
@@ -68,26 +69,24 @@ class TerminalReporter {
68
69
  const localPath = _path.default.relative(".", entryFile);
69
70
  const filledBar = Math.floor(ratio * MAX_PROGRESS_BAR_CHAR_WIDTH);
70
71
  const bundleTypeColor =
71
- phase === "done"
72
- ? _chalk.default.green
73
- : phase === "failed"
74
- ? _chalk.default.red
75
- : _chalk.default.yellow;
72
+ phase === "done" ? ["green"] : phase === "failed" ? ["red"] : ["yellow"];
76
73
  const progress =
77
74
  phase === "in_progress"
78
- ? _chalk.default.green.bgGreen(DARK_BLOCK_CHAR.repeat(filledBar)) +
79
- _chalk.default.bgWhite.white(
75
+ ? style(["green", "bgGreen"], DARK_BLOCK_CHAR.repeat(filledBar)) +
76
+ style(
77
+ ["bgWhite", "white"],
80
78
  LIGHT_BLOCK_CHAR.repeat(MAX_PROGRESS_BAR_CHAR_WIDTH - filledBar),
81
79
  ) +
82
- _chalk.default.bold(` ${Math.floor(100 * ratio)}% `) +
83
- _chalk.default.dim(`(${transformedFileCount}/${totalFileCount})`)
80
+ style(["bold"], ` ${Math.floor(100 * ratio)}% `) +
81
+ style(["dim"], `(${transformedFileCount}/${totalFileCount})`)
84
82
  : "";
85
83
  return (
86
- bundleTypeColor.inverse.bold(
84
+ style(
85
+ [...bundleTypeColor, "inverse", "bold"],
87
86
  ` ${isPrefetch === true ? "PREBUNDLE" : bundleType.toUpperCase()} `,
88
87
  ) +
89
- _chalk.default.reset.dim(` ${_path.default.dirname(localPath)}/`) +
90
- _chalk.default.bold(_path.default.basename(localPath)) +
88
+ style(["reset", "dim"], ` ${_path.default.dirname(localPath)}/`) +
89
+ style(["bold"], _path.default.basename(localPath)) +
91
90
  " " +
92
91
  progress
93
92
  );
@@ -132,38 +131,36 @@ class TerminalReporter {
132
131
  "",
133
132
  "",
134
133
  ];
135
- const color = hasReducedPerformance
136
- ? _chalk.default.red
137
- : _chalk.default.blue;
138
- this.terminal.log(color(logo.join("\n")));
134
+ const color = hasReducedPerformance ? ["red"] : ["blue"];
135
+ this.terminal.log(style(color, logo.join("\n")));
139
136
  }
140
137
  _logInitializingFailed(port, error) {
141
138
  if (error.code === "EADDRINUSE") {
142
139
  this.terminal.log(
143
- _chalk.default.bgRed.bold(" ERROR "),
144
- _chalk.default.red(
145
- "Metro can't listen on port",
146
- _chalk.default.bold(String(port)),
140
+ style(["bgRed", "bold"], " ERROR "),
141
+ style(
142
+ ["red"],
143
+ `Metro can't listen on port ${style(["bold"], String(port))}`,
147
144
  ),
148
145
  );
149
146
  this.terminal.log(
150
147
  "Most likely another process is already using this port",
151
148
  );
152
149
  this.terminal.log("Run the following command to find out which process:");
153
- this.terminal.log("\n ", _chalk.default.bold("lsof -i :" + port), "\n");
150
+ this.terminal.log("\n ", style(["bold"], "lsof -i :" + port), "\n");
154
151
  this.terminal.log("Then, you can either shut down the other process:");
155
- this.terminal.log("\n ", _chalk.default.bold("kill -9 <PID>"), "\n");
152
+ this.terminal.log("\n ", style(["bold"], "kill -9 <PID>"), "\n");
156
153
  this.terminal.log("or run Metro on different port.");
157
154
  } else {
158
155
  this.terminal.log(
159
- _chalk.default.bgRed.bold(" ERROR "),
160
- _chalk.default.red(error.message),
156
+ style(["bgRed", "bold"], " ERROR "),
157
+ style(["red"], error.message),
161
158
  );
162
159
  const errorAttributes = JSON.stringify(error);
163
160
  if (errorAttributes !== "{}") {
164
- this.terminal.log(_chalk.default.red(errorAttributes));
161
+ this.terminal.log(style(["red"], errorAttributes));
165
162
  }
166
- this.terminal.log(_chalk.default.red(error.stack));
163
+ this.terminal.log(style(["red"], String(error.stack)));
167
164
  }
168
165
  }
169
166
  _log(event) {
@@ -214,22 +211,21 @@ class TerminalReporter {
214
211
  logFn(this.terminal, String(format), ...args);
215
212
  break;
216
213
  case "dep_graph_loading":
217
- const color = event.hasReducedPerformance
218
- ? _chalk.default.red
219
- : _chalk.default.blue;
214
+ const color = event.hasReducedPerformance ? ["red"] : ["blue"];
220
215
  const version = "v" + require("../../package.json").version;
221
216
  this.terminal.log(
222
- color.bold(
223
- " ".repeat(19 - version.length / 2),
224
- "Welcome to Metro " + _chalk.default.white(version) + "\n",
225
- ) +
226
- _chalk.default.dim(
227
- " Fast - Scalable - Integrated\n\n",
228
- ),
217
+ style(
218
+ [...color, "bold"],
219
+ " ".repeat(19 - version.length / 2) +
220
+ " Welcome to Metro " +
221
+ style(["white"], version) +
222
+ "\n",
223
+ ) + style(["dim"], " Fast - Scalable - Integrated\n\n"),
229
224
  );
230
225
  if (event.hasReducedPerformance) {
231
226
  this.terminal.log(
232
- _chalk.default.red(
227
+ style(
228
+ ["red"],
233
229
  "Metro is operating with reduced performance.\n" +
234
230
  "Please fix the problem above and restart Metro.\n\n",
235
231
  ),
@@ -360,7 +356,7 @@ class TerminalReporter {
360
356
  case "success":
361
357
  if (this._prevHealthCheckResult) {
362
358
  this.terminal.log(
363
- _chalk.default.green(`Watcher ${watcherName} is now healthy.`),
359
+ style(["green"], `Watcher ${watcherName} is now healthy.`),
364
360
  );
365
361
  }
366
362
  break;
@@ -399,14 +395,16 @@ class TerminalReporter {
399
395
  break;
400
396
  case "watchman_slow_command":
401
397
  this.terminal.log(
402
- _chalk.default.dim(
398
+ style(
399
+ ["dim"],
403
400
  `Waiting for Watchman \`${status.command}\` (${Math.round(status.timeElapsed / 1000)}s)...`,
404
401
  ),
405
402
  );
406
403
  break;
407
404
  case "watchman_slow_command_complete":
408
405
  this.terminal.log(
409
- _chalk.default.green(
406
+ style(
407
+ ["green"],
410
408
  `Watchman \`${status.command}\` finished after ${(status.timeElapsed / 1000).toFixed(1)}s.`,
411
409
  ),
412
410
  );