1declare module 'metro/src/shared/output/bundle' {
2  export function build(
3    arg0: Server,
4    arg1: RequestOptions
5  ): Promise<{
6    code: string;
7    map: string;
8  }>;
9  export function save(
10    arg0: {
11      code: string;
12      map: string;
13    },
14    arg1: OutputOptions,
15    arg2: (...args: Array<string>) => void
16  ): Promise<unknown>;
17}
18
19declare module 'metro/src/HmrServer' {
20  export class MetroHmrServer {
21    constructor(...args: any[]);
22  }
23
24  module.exports = MetroHmrServer;
25}
26
27declare module 'metro/src/ModuleGraph/worker/collectDependencies' {
28  export type AllowOptionalDependenciesWithOptions = {
29    exclude: string[];
30  };
31
32  export type DynamicRequiresBehavior = 'throwAtRuntime' | 'reject';
33
34  export type AllowOptionalDependencies = boolean | AllowOptionalDependenciesWithOptions;
35}
36
37declare module 'metro/src/DeltaBundler/types.flow' {
38  export type AllowOptionalDependenciesWithOptions = {
39    exclude: string[];
40  };
41
42  export type AllowOptionalDependencies = boolean | AllowOptionalDependenciesWithOptions;
43}
44declare module 'metro/src/DeltaBundler' {
45  export type AsyncDependencyType = 'async' | 'prefetch';
46  export type TransformResultDependency = {
47    /**
48     * The literal name provided to a require or import call. For example 'foo' in
49     * case of `require('foo')`.
50     */
51    name: string;
52
53    /**
54     * Extra data returned by the dependency extractor.
55     */
56    data: {
57      /**
58       * A locally unique key for this dependency within the current module.
59       */
60      key: string;
61      /**
62       * If not null, this dependency is due to a dynamic `import()` or `__prefetchImport()` call.
63       */
64      asyncType: AsyncDependencyType | null;
65      /**
66       * The condition for splitting on this dependency edge.
67       */
68      splitCondition?: {
69        mobileConfigName: string;
70      };
71      /**
72       * The dependency is enclosed in a try/catch block.
73       */
74      isOptional?: boolean;
75
76      locs: $ReadOnlyArray<BabelSourceLocation>;
77
78      /** Context for requiring a collection of modules. */
79      contextParams?: RequireContextParams;
80    };
81  };
82}
83
84declare module 'metro/src/lib/countLines' {
85  const countLines = (string: string) => number;
86
87  module.exports = countLines;
88  export default countLines;
89}
90
91declare module 'metro/src/lib/createWebsocketServer' {
92  export function createWebsocketServer<TClient extends object>({
93    websocketServer,
94  }: HMROptions<TClient>): typeof import('ws').Server;
95
96  module.exports = createWebsocketServer;
97}
98
99declare module 'metro/src/lib/splitBundleOptions' {
100  import { ConfigT } from 'metro-config';
101
102  type SplitBundleOptions = {
103    entryFile: string;
104    resolverOptions: unknown;
105    transformOptions: { platform: string };
106    serializerOptions: unknown;
107    graphOptions: unknown;
108    onProgress?: (numProcessed: number, total: number) => any;
109  };
110
111  type BundleOptions = any;
112
113  function splitBundleOptions(options: BundleOptions): SplitBundleOptions;
114
115  export default splitBundleOptions;
116}
117
118declare module 'metro/src/DeltaBundler/Serializers/helpers/js' {
119  import type { JsOutput } from 'metro-transform-worker';
120  import type { MixedOutput, Module } from 'metro';
121
122  export function getJsOutput(
123    module: readonly {
124      output: readonly MixedOutput[];
125      path?: string;
126    }
127  ): JsOutput;
128
129  export function isJsModule(module: Module<unknown>): boolean;
130}
131
132declare module 'metro/src/Assets' {
133  export type AssetInfo = {
134    files: string[];
135    hash: string;
136    name: string;
137    scales: number[];
138    type: string;
139  };
140
141  export type AssetDataWithoutFiles = {
142    __packager_asset: boolean;
143    fileSystemLocation: string;
144    hash: string;
145    height: number | null;
146    httpServerLocation: string;
147    name: string;
148    scales: number[];
149    type: string;
150    width: number | null;
151  };
152
153  export type AssetDataFiltered = {
154    __packager_asset: boolean;
155    hash: string;
156    height: number | null;
157    httpServerLocation: string;
158    name: string;
159    scales: number[];
160    type: string;
161    width: number | null;
162  };
163
164  export type AssetData = AssetDataWithoutFiles & { files: Array<string> };
165
166  export type AssetDataPlugin = (assetData: AssetData) => AssetData | Promise<AssetData>;
167
168  export async function getAsset(
169    relativePath: string,
170    projectRoot: string,
171    watchFolders: readonly string[],
172    platform: string | null | undefined,
173    assetExts: readonly string[]
174  ): Promise<Buffer>;
175
176  async function getAssetData(
177    assetPath: string,
178    localPath: string,
179    assetDataPlugins: readonly string[],
180    platform: string | null | undefined,
181    publicPath: string
182  ): Promise<AssetData>;
183}
184
185declare module 'metro' {
186  //#region metro/src/Assets.js
187
188  type AssetDataWithoutFiles = {
189    readonly __packager_asset: true;
190    readonly fileSystemLocation: string;
191    readonly hash: string;
192    readonly height: number | null | undefined;
193    readonly httpServerLocation: string;
194    readonly name: string;
195    readonly scales: Array<number>;
196    readonly type: string;
197    readonly width: number | null | undefined;
198  };
199
200  export type AssetData = AssetDataWithoutFiles & { readonly files: Array<string> };
201
202  //#endregion
203  //#region metro/src/DeltaBundler/types.flow.js
204
205  export interface MixedOutput {
206    readonly data: any;
207    readonly type: string;
208  }
209
210  interface BabelSourceLocation {
211    start: { line: number; column: number };
212    end: { line: number; column: number };
213    identifierName?: string;
214  }
215
216  interface TransformResultDependency {
217    /**
218     * The literal name provided to a require or import call. For example 'foo' in
219     * case of `require('foo')`.
220     */
221    readonly name: string;
222
223    /**
224     * Extra data returned by the dependency extractor. Whatever is added here is
225     * blindly piped by Metro to the serializers.
226     */
227    readonly data: {
228      /**
229       * If `true` this dependency is due to a dynamic `import()` call. If `false`,
230       * this dependency was pulled using a synchronous `require()` call.
231       */
232      readonly isAsync: boolean;
233
234      /**
235       * The dependency is actually a `__prefetchImport()` call.
236       */
237      readonly isPrefetchOnly?: true;
238
239      /**
240       * The condition for splitting on this dependency edge.
241       */
242      readonly splitCondition?: {
243        readonly mobileConfigName: string;
244      };
245
246      /**
247       * The dependency is enclosed in a try/catch block.
248       */
249      readonly isOptional?: boolean;
250
251      readonly locs: ReadonlyArray<BabelSourceLocation>;
252    };
253  }
254
255  interface Dependency {
256    readonly absolutePath: string;
257    readonly data: TransformResultDependency;
258  }
259
260  export interface Module<T = MixedOutput> {
261    readonly dependencies: Map<string, Dependency>;
262    readonly inverseDependencies: Set<string>;
263    readonly output: ReadonlyArray<T>;
264    readonly path: string;
265    readonly getSource: () => Buffer;
266  }
267
268  export interface Graph<T = MixedOutput> {
269    dependencies: Map<string, Module<T>>;
270    importBundleNames: Set<string>;
271    readonly entryPoints: ReadonlyArray<string>;
272  }
273
274  export type TransformResult<T = MixedOutput> = Readonly<{
275    dependencies: ReadonlyArray<TransformResultDependency>;
276    output: ReadonlyArray<T>;
277  }>;
278
279  interface AllowOptionalDependenciesWithOptions {
280    readonly exclude: Array<string>;
281  }
282  type AllowOptionalDependencies = boolean | AllowOptionalDependenciesWithOptions;
283
284  export interface DeltaResult<T = MixedOutput> {
285    readonly added: Map<string, Module<T>>;
286    readonly modified: Map<string, Module<T>>;
287    readonly deleted: Set<string>;
288    readonly reset: boolean;
289  }
290
291  export interface SerializerOptions {
292    readonly asyncRequireModulePath: string;
293    readonly createModuleId: (arg0: string) => number;
294    readonly dev: boolean;
295    readonly getRunModuleStatement: (arg0: number | string) => string;
296    readonly inlineSourceMap: boolean | null | undefined;
297    readonly modulesOnly: boolean;
298    readonly processModuleFilter: (module: Module) => boolean;
299    readonly projectRoot: string;
300    readonly runBeforeMainModule: ReadonlyArray<string>;
301    readonly runModule: boolean;
302    readonly sourceMapUrl: string | null | undefined;
303    readonly sourceUrl: string | null | undefined;
304  }
305
306  //#endregion
307  //#region metro/src/DeltaBundler/Serializers/getRamBundleInfo.js
308
309  interface RamBundleInfo {
310    getDependencies: (filePath: string) => Set<string>;
311    startupModules: ReadonlyArray<ModuleTransportLike>;
312    lazyModules: ReadonlyArray<ModuleTransportLike>;
313    groups: Map<number, Set<number>>;
314  }
315
316  //#endregion
317  //#region metro/src/index.js
318
319  import { Server as HttpServer } from 'http';
320  import { Server as HttpsServer } from 'https';
321  import { loadConfig, ConfigT, InputConfigT, Middleware, ConfigT } from 'metro-config';
322
323  type MetroMiddleWare = {
324    attachHmrServer: (httpServer: HttpServer | HttpsServer) => void;
325    end: () => void;
326    metroServer: Server;
327    middleware: Middleware;
328  };
329
330  export type RunServerOptions = {
331    hasReducedPerformance?: boolean;
332    hmrEnabled?: boolean;
333    host?: string;
334    onError?: (arg0: Error & { code?: string }) => void;
335    onReady?: (server: HttpServer | HttpsServer) => void;
336    runInspectorProxy?: boolean;
337    /** @deprecated */
338    secure?: boolean;
339    /** @deprecated */
340    secureCert?: string;
341    /** @deprecated */
342    secureKey?: string;
343    websocketEndpoints?: Record<string, import('ws').Server>;
344    hasReducedPerformance?: boolean;
345    host?: string;
346    secureServerOptions?: any;
347    waitForBundler?: boolean;
348    watch?: boolean;
349  };
350
351  type BuildGraphOptions = {
352    entries: ReadonlyArray<string>;
353    customTransformOptions?: CustomTransformOptions;
354    dev?: boolean;
355    minify?: boolean;
356    onProgress?: (transformedFileCount: number, totalFileCount: number) => void;
357    platform?: string;
358    type?: 'module' | 'script';
359  };
360
361  type RunBuildOptions = {
362    entry: string;
363    dev?: boolean;
364    out?: string;
365    onBegin?: () => void;
366    onComplete?: () => void;
367    onProgress?: (transformedFileCount: number, totalFileCount: number) => void;
368    minify?: boolean;
369    output?: {
370      build: (
371        arg0: Server,
372        arg1: RequestOptions
373      ) => Promise<{
374        code: string;
375        map: string;
376      }>;
377      save: (
378        arg0: {
379          code: string;
380          map: string;
381        },
382        arg1: OutputOptions,
383        arg2: (...args: Array<string>) => void
384      ) => Promise<unknown>;
385    };
386    platform?: string;
387    sourceMap?: boolean;
388    sourceMapUrl?: string;
389  };
390
391  export function runMetro(config: InputConfigT, options?: ServerOptions): Promise<Server>;
392
393  export { loadConfig };
394
395  export function createConnectMiddleware(
396    config: ConfigT,
397    options?: ServerOptions
398  ): Promise<MetroMiddleWare>;
399
400  export function runServer(
401    config: ConfigT,
402    options: RunServerOptions
403  ): Promise<HttpServer | HttpsServer>;
404
405  export function runBuild(
406    config: ConfigT,
407    options: RunBuildOptions
408  ): Promise<{
409    code: string;
410    map: string;
411  }>;
412
413  export function buildGraph(config: InputConfigT, options: BuildGraphOptions): Promise<Graph>;
414  //#endregion
415  //#region metro/src/JSTransformer/worker.js
416
417  type CustomTransformOptions = {
418    [key: string]: unknown;
419  };
420
421  export type JsTransformerConfig = Readonly<{
422    assetPlugins: ReadonlyArray<string>;
423    assetRegistryPath: string;
424    asyncRequireModulePath: string;
425    babelTransformerPath: string;
426    dynamicDepsInPackages: DynamicRequiresBehavior;
427    enableBabelRCLookup: boolean;
428    enableBabelRuntime: boolean;
429    experimentalImportBundleSupport: boolean;
430    minifierConfig: MinifierConfig;
431    minifierPath: string;
432    optimizationSizeLimit: number;
433    publicPath: string;
434    allowOptionalDependencies: AllowOptionalDependencies;
435    unstable_allowRequireContext?: boolean;
436  }>;
437
438  //#endregion
439  //#region metro/src/lib/reporting.js
440
441  /**
442   * A tagged union of all the actions that may happen and we may want to
443   * report to the tool user.
444   */
445  export type ReportableEvent =
446    | {
447        port: number;
448        hasReducedPerformance: boolean;
449        type: 'initialize_started';
450      }
451    | {
452        type: 'initialize_failed';
453        port: number;
454        error: Error;
455      }
456    | {
457        buildID: string;
458        type: 'bundle_build_done';
459      }
460    | {
461        buildID: string;
462        type: 'bundle_build_failed';
463      }
464    | {
465        buildID: string;
466        bundleDetails: BundleDetails;
467        type: 'bundle_build_started';
468      }
469    | {
470        error: Error;
471        type: 'bundling_error';
472      }
473    | {
474        type: 'dep_graph_loading';
475        hasReducedPerformance: boolean;
476      }
477    | { type: 'dep_graph_loaded' }
478    | {
479        buildID: string;
480        type: 'bundle_transform_progressed';
481        transformedFileCount: number;
482        totalFileCount: number;
483      }
484    | {
485        type: 'global_cache_error';
486        error: Error;
487      }
488    | {
489        type: 'global_cache_disabled';
490        reason: GlobalCacheDisabledReason;
491      }
492    | { type: 'transform_cache_reset' }
493    | {
494        type: 'worker_stdout_chunk';
495        chunk: string;
496      }
497    | {
498        type: 'worker_stderr_chunk';
499        chunk: string;
500      }
501    | {
502        type: 'hmr_client_error';
503        error: Error;
504      }
505    | {
506        type: 'client_log';
507        level:
508          | 'trace'
509          | 'info'
510          | 'warn'
511          | 'log'
512          | 'group'
513          | 'groupCollapsed'
514          | 'groupEnd'
515          | 'debug';
516        data: unknown[];
517      };
518
519  /**
520   * Code across the application takes a reporter as an option and calls the
521   * update whenever one of the ReportableEvent happens. Code does not directly
522   * write to the standard output, because a build would be:
523   *
524   *   1. ad-hoc, embedded into another tool, in which case we do not want to
525   *   pollute that tool's own output. The tool is free to present the
526   *   warnings/progress we generate any way they want, by specifing a custom
527   *   reporter.
528   *   2. run as a background process from another tool, in which case we want
529   *   to expose updates in a way that is easily machine-readable, for example
530   *   a JSON-stream. We don't want to pollute it with textual messages.
531   *
532   * We centralize terminal reporting into a single place because we want the
533   * output to be robust and consistent. The most common reporter is
534   * TerminalReporter, that should be the only place in the application should
535   * access the `terminal` module (nor the `console`).
536   */
537  export interface Reporter {
538    update(event: ReportableEvent): void;
539  }
540
541  //#endregion
542  //#region metro/src/ModuleGraph/types.flow.js
543
544  export type TransformVariants = {
545    readonly [name: string]: {};
546  };
547
548  //#endregion
549  //#region metro/src/Server.js
550
551  type ServerOptions = Readonly<{
552    watch?: boolean;
553  }>;
554
555  //#endregion
556  //#region metro/src/Server/index.js
557
558  import { IncomingMessage, ServerResponse } from 'http';
559
560  class Bundler {
561    getWatcher(): import('events').EventEmitter;
562  }
563
564  class IncrementalBundler {
565    // TODO: type declaration
566    getBundler(): Bundler;
567  }
568
569  export class Server {
570    constructor(config: ConfigT, options?: ServerOptions);
571
572    end(): void;
573
574    getBundler(): IncrementalBundler;
575
576    getCreateModuleId(): (path: string) => number;
577
578    build(options: BundleOptions): Promise<{
579      code: string;
580      map: string;
581    }>;
582
583    getRamBundleInfo(options: BundleOptions): Promise<RamBundleInfo>;
584
585    getAssets(options: BundleOptions): Promise<ReadonlyArray<AssetData>>;
586
587    getOrderedDependencyPaths(options: {
588      readonly dev: boolean;
589      readonly entryFile: string;
590      readonly minify: boolean;
591      readonly platform: string;
592    }): Promise<Array<string>>;
593
594    processRequest(
595      req: IncomingMessage,
596      res: ServerResponse,
597      next: (arg0: Error | null | undefined) => unknown
598    ): void;
599
600    getNewBuildID(): string;
601
602    getPlatforms(): ReadonlyArray<string>;
603
604    getWatchFolders(): ReadonlyArray<string>;
605
606    static DEFAULT_GRAPH_OPTIONS: {
607      customTransformOptions: any;
608      dev: boolean;
609      hot: boolean;
610      minify: boolean;
611    };
612
613    static DEFAULT_BUNDLE_OPTIONS: typeof Server.DEFAULT_GRAPH_OPTIONS & {
614      excludeSource: false;
615      inlineSourceMap: false;
616      modulesOnly: false;
617      onProgress: null;
618      runModule: true;
619      shallow: false;
620      sourceMapUrl: null;
621      sourceUrl: null;
622    };
623  }
624
625  //#endregion
626  //#region metro/src/shared/types.flow.js
627
628  type BundleType = 'bundle' | 'delta' | 'meta' | 'map' | 'ram' | 'cli' | 'hmr' | 'todo' | 'graph';
629
630  type MetroSourceMapOrMappings = MixedSourceMap | Array<MetroSourceMapSegmentTuple>;
631
632  export interface BundleOptions {
633    bundleType: BundleType;
634    customTransformOptions: CustomTransformOptions;
635    dev: boolean;
636    entryFile: string;
637    readonly excludeSource: boolean;
638    readonly hot: boolean;
639    readonly inlineSourceMap: boolean;
640    minify: boolean;
641    readonly modulesOnly: boolean;
642    onProgress: (doneCont: number, totalCount: number) => unknown | null | undefined;
643    readonly platform: string | null | undefined;
644    readonly runModule: boolean;
645    readonly shallow: boolean;
646    sourceMapUrl: string | null | undefined;
647    sourceUrl: string | null | undefined;
648    createModuleIdFactory?: () => (path: string) => number;
649  }
650
651  type ModuleTransportLike = {
652    readonly code: string;
653    readonly id: number;
654    readonly map: MetroSourceMapOrMappings | null | undefined;
655    readonly name?: string;
656    readonly sourcePath: string;
657  };
658
659  export interface OutputOptions {
660    bundleOutput: string;
661    bundleEncoding?: 'utf8' | 'utf16le' | 'ascii';
662    dev?: boolean;
663    indexedRamBundle?: boolean;
664    platform: string;
665    sourcemapOutput?: string;
666    sourcemapSourcesRoot?: string;
667    sourcemapUseAbsolutePath?: boolean;
668  }
669
670  export interface RequestOptions {
671    entryFile: string;
672    inlineSourceMap?: boolean;
673    sourceMapUrl?: string;
674    dev?: boolean;
675    minify: boolean;
676    platform: string;
677    createModuleIdFactory?: () => (path: string) => number;
678    onProgress?: (transformedFileCount: number, totalFileCount: number) => void;
679  }
680
681  //#endregion
682}
683
684declare module 'metro/src/DeltaBundler/Serializers/baseJSBundle' {
685  import { Module, Graph, SerializerOptions } from 'metro';
686
687  type ModuleMap = readonly [number, string][];
688
689  type Bundle = {
690    readonly modules: ModuleMap;
691    readonly post: string;
692    readonly pre: string;
693  };
694
695  export default function baseJSBundle(
696    entryPoint: string,
697    preModules: readonly Module[],
698    graph: Graph,
699    options: SerializerOptions
700  ): Bundle;
701}
702
703declare module 'metro/src/lib/bundleToString' {
704  import { Module, ReadOnlyGraph, SerializerOptions } from 'metro';
705
706  type ModuleMap = readonly [number, string][];
707
708  type Bundle = {
709    readonly modules: ModuleMap;
710    readonly post: string;
711    readonly pre: string;
712  };
713
714  type BundleMetadata = {
715    readonly pre: number;
716    readonly post: number;
717    readonly modules: readonly [number, number][];
718  };
719
720  export default function bundleToString(bundle: Bundle): {
721    readonly code: string;
722    readonly metadata: BundleMetadata;
723  };
724}
725