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}
18declare module 'metro' {
19  //#region metro/src/Assets.js
20
21  type AssetDataWithoutFiles = {
22    readonly __packager_asset: true;
23    readonly fileSystemLocation: string;
24    readonly hash: string;
25    readonly height: number | null | undefined;
26    readonly httpServerLocation: string;
27    readonly name: string;
28    readonly scales: Array<number>;
29    readonly type: string;
30    readonly width: number | null | undefined;
31  };
32
33  export type AssetData = AssetDataWithoutFiles & { readonly files: Array<string> };
34
35  //#endregion
36  //#region metro/src/DeltaBundler/types.flow.js
37
38  interface MixedOutput {
39    readonly data: unknown;
40    readonly type: string;
41  }
42
43  interface BabelSourceLocation {
44    start: { line: number; column: number };
45    end: { line: number; column: number };
46    identifierName?: string;
47  }
48
49  interface TransformResultDependency {
50    /**
51     * The literal name provided to a require or import call. For example 'foo' in
52     * case of `require('foo')`.
53     */
54    readonly name: string;
55
56    /**
57     * Extra data returned by the dependency extractor. Whatever is added here is
58     * blindly piped by Metro to the serializers.
59     */
60    readonly data: {
61      /**
62       * If `true` this dependency is due to a dynamic `import()` call. If `false`,
63       * this dependency was pulled using a synchronous `require()` call.
64       */
65      readonly isAsync: boolean;
66
67      /**
68       * The dependency is actually a `__prefetchImport()` call.
69       */
70      readonly isPrefetchOnly?: true;
71
72      /**
73       * The condition for splitting on this dependency edge.
74       */
75      readonly splitCondition?: {
76        readonly mobileConfigName: string;
77      };
78
79      /**
80       * The dependency is enclosed in a try/catch block.
81       */
82      readonly isOptional?: boolean;
83
84      readonly locs: ReadonlyArray<BabelSourceLocation>;
85    };
86  }
87
88  interface Dependency {
89    readonly absolutePath: string;
90    readonly data: TransformResultDependency;
91  }
92
93  export interface Module<T = MixedOutput> {
94    readonly dependencies: Map<string, Dependency>;
95    readonly inverseDependencies: Set<string>;
96    readonly output: ReadonlyArray<T>;
97    readonly path: string;
98    readonly getSource: () => Buffer;
99  }
100
101  export interface Graph<T = MixedOutput> {
102    dependencies: Map<string, Module<T>>;
103    importBundleNames: Set<string>;
104    readonly entryPoints: ReadonlyArray<string>;
105  }
106
107  export type TransformResult<T = MixedOutput> = Readonly<{
108    dependencies: ReadonlyArray<TransformResultDependency>;
109    output: ReadonlyArray<T>;
110  }>;
111
112  interface AllowOptionalDependenciesWithOptions {
113    readonly exclude: Array<string>;
114  }
115  type AllowOptionalDependencies = boolean | AllowOptionalDependenciesWithOptions;
116
117  export interface DeltaResult<T = MixedOutput> {
118    readonly added: Map<string, Module<T>>;
119    readonly modified: Map<string, Module<T>>;
120    readonly deleted: Set<string>;
121    readonly reset: boolean;
122  }
123
124  export interface SerializerOptions {
125    readonly asyncRequireModulePath: string;
126    readonly createModuleId: (arg0: string) => number;
127    readonly dev: boolean;
128    readonly getRunModuleStatement: (arg0: number | string) => string;
129    readonly inlineSourceMap: boolean | null | undefined;
130    readonly modulesOnly: boolean;
131    readonly processModuleFilter: (module: Module) => boolean;
132    readonly projectRoot: string;
133    readonly runBeforeMainModule: ReadonlyArray<string>;
134    readonly runModule: boolean;
135    readonly sourceMapUrl: string | null | undefined;
136    readonly sourceUrl: string | null | undefined;
137  }
138
139  //#endregion
140  //#region metro/src/DeltaBundler/Serializers/getRamBundleInfo.js
141
142  interface RamBundleInfo {
143    getDependencies: (filePath: string) => Set<string>;
144    startupModules: ReadonlyArray<ModuleTransportLike>;
145    lazyModules: ReadonlyArray<ModuleTransportLike>;
146    groups: Map<number, Set<number>>;
147  }
148
149  //#endregion
150  //#region metro/src/index.js
151
152  import { Server as HttpServer } from 'http';
153  import { Server as HttpsServer } from 'https';
154  import { loadConfig, ConfigT, InputConfigT, Middleware } from 'metro-config';
155
156  type MetroMiddleWare = {
157    attachHmrServer: (httpServer: HttpServer | HttpsServer) => void;
158    end: () => void;
159    metroServer: Server;
160    middleware: Middleware;
161  };
162
163  type RunServerOptions = {
164    hasReducedPerformance?: boolean;
165    hmrEnabled?: boolean;
166    host?: string;
167    onError?: (arg0: Error & { code?: string }) => void;
168    onReady?: (server: HttpServer | HttpsServer) => void;
169    runInspectorProxy?: boolean;
170    secure?: boolean;
171    secureCert?: string;
172    secureKey?: string;
173    websocketEndpoints?: object;
174  };
175
176  type BuildGraphOptions = {
177    entries: ReadonlyArray<string>;
178    customTransformOptions?: CustomTransformOptions;
179    dev?: boolean;
180    minify?: boolean;
181    onProgress?: (transformedFileCount: number, totalFileCount: number) => void;
182    platform?: string;
183    type?: 'module' | 'script';
184  };
185
186  type RunBuildOptions = {
187    entry: string;
188    dev?: boolean;
189    out?: string;
190    onBegin?: () => void;
191    onComplete?: () => void;
192    onProgress?: (transformedFileCount: number, totalFileCount: number) => void;
193    minify?: boolean;
194    output?: {
195      build: (
196        arg0: Server,
197        arg1: RequestOptions
198      ) => Promise<{
199        code: string;
200        map: string;
201      }>;
202      save: (
203        arg0: {
204          code: string;
205          map: string;
206        },
207        arg1: OutputOptions,
208        arg2: (...args: Array<string>) => void
209      ) => Promise<unknown>;
210    };
211    platform?: string;
212    sourceMap?: boolean;
213    sourceMapUrl?: string;
214  };
215
216  export function runMetro(config: InputConfigT, options?: ServerOptions): Promise<Server>;
217
218  export { loadConfig };
219
220  export function createConnectMiddleware(
221    config: ConfigT,
222    options?: ServerOptions
223  ): Promise<MetroMiddleWare>;
224
225  export function runServer(
226    config: ConfigT,
227    options: RunServerOptions
228  ): Promise<HttpServer | HttpsServer>;
229
230  export function runBuild(
231    config: ConfigT,
232    options: RunBuildOptions
233  ): Promise<{
234    code: string;
235    map: string;
236  }>;
237
238  export function buildGraph(config: InputConfigT, options: BuildGraphOptions): Promise<Graph>;
239  //#endregion
240  //#region metro/src/JSTransformer/worker.js
241
242  type CustomTransformOptions = {
243    [key: string]: unknown;
244  };
245
246  export type JsTransformerConfig = Readonly<{
247    assetPlugins: ReadonlyArray<string>;
248    assetRegistryPath: string;
249    asyncRequireModulePath: string;
250    babelTransformerPath: string;
251    dynamicDepsInPackages: DynamicRequiresBehavior;
252    enableBabelRCLookup: boolean;
253    enableBabelRuntime: boolean;
254    experimentalImportBundleSupport: boolean;
255    minifierConfig: MinifierConfig;
256    minifierPath: string;
257    optimizationSizeLimit: number;
258    publicPath: string;
259    allowOptionalDependencies: AllowOptionalDependencies;
260    unstable_allowRequireContext?: boolean;
261  }>;
262
263  //#endregion
264  //#region metro/src/lib/reporting.js
265
266  /**
267   * A tagged union of all the actions that may happen and we may want to
268   * report to the tool user.
269   */
270  export type ReportableEvent =
271    | {
272        port: number;
273        hasReducedPerformance: boolean;
274        type: 'initialize_started';
275      }
276    | {
277        type: 'initialize_failed';
278        port: number;
279        error: Error;
280      }
281    | {
282        buildID: string;
283        type: 'bundle_build_done';
284      }
285    | {
286        buildID: string;
287        type: 'bundle_build_failed';
288      }
289    | {
290        buildID: string;
291        bundleDetails: BundleDetails;
292        type: 'bundle_build_started';
293      }
294    | {
295        error: Error;
296        type: 'bundling_error';
297      }
298    | {
299        type: 'dep_graph_loading';
300        hasReducedPerformance: boolean;
301      }
302    | { type: 'dep_graph_loaded' }
303    | {
304        buildID: string;
305        type: 'bundle_transform_progressed';
306        transformedFileCount: number;
307        totalFileCount: number;
308      }
309    | {
310        type: 'global_cache_error';
311        error: Error;
312      }
313    | {
314        type: 'global_cache_disabled';
315        reason: GlobalCacheDisabledReason;
316      }
317    | { type: 'transform_cache_reset' }
318    | {
319        type: 'worker_stdout_chunk';
320        chunk: string;
321      }
322    | {
323        type: 'worker_stderr_chunk';
324        chunk: string;
325      }
326    | {
327        type: 'hmr_client_error';
328        error: Error;
329      }
330    | {
331        type: 'client_log';
332        level:
333          | 'trace'
334          | 'info'
335          | 'warn'
336          | 'log'
337          | 'group'
338          | 'groupCollapsed'
339          | 'groupEnd'
340          | 'debug';
341        data: unknown[];
342      };
343
344  /**
345   * Code across the application takes a reporter as an option and calls the
346   * update whenever one of the ReportableEvent happens. Code does not directly
347   * write to the standard output, because a build would be:
348   *
349   *   1. ad-hoc, embedded into another tool, in which case we do not want to
350   *   pollute that tool's own output. The tool is free to present the
351   *   warnings/progress we generate any way they want, by specifing a custom
352   *   reporter.
353   *   2. run as a background process from another tool, in which case we want
354   *   to expose updates in a way that is easily machine-readable, for example
355   *   a JSON-stream. We don't want to pollute it with textual messages.
356   *
357   * We centralize terminal reporting into a single place because we want the
358   * output to be robust and consistent. The most common reporter is
359   * TerminalReporter, that should be the only place in the application should
360   * access the `terminal` module (nor the `console`).
361   */
362  export interface Reporter {
363    update(event: ReportableEvent): void;
364  }
365
366  //#endregion
367  //#region metro/src/ModuleGraph/types.flow.js
368
369  export type TransformVariants = {
370    readonly [name: string]: {};
371  };
372
373  //#endregion
374  //#region metro/src/Server.js
375
376  type ServerOptions = Readonly<{
377    watch?: boolean;
378  }>;
379
380  //#endregion
381  //#region metro/src/Server/index.js
382
383  import { IncomingMessage, ServerResponse } from 'http';
384
385  // TODO: type declaration
386  type IncrementalBundler = unknown;
387
388  export class Server {
389    constructor(config: ConfigT, options?: ServerOptions);
390
391    end(): void;
392
393    getBundler(): IncrementalBundler;
394
395    getCreateModuleId(): (path: string) => number;
396
397    build(options: BundleOptions): Promise<{
398      code: string;
399      map: string;
400    }>;
401
402    getRamBundleInfo(options: BundleOptions): Promise<RamBundleInfo>;
403
404    getAssets(options: BundleOptions): Promise<ReadonlyArray<AssetData>>;
405
406    getOrderedDependencyPaths(options: {
407      readonly dev: boolean;
408      readonly entryFile: string;
409      readonly minify: boolean;
410      readonly platform: string;
411    }): Promise<Array<string>>;
412
413    processRequest(
414      req: IncomingMessage,
415      res: ServerResponse,
416      next: (arg0: Error | null | undefined) => unknown
417    ): void;
418
419    getNewBuildID(): string;
420
421    getPlatforms(): ReadonlyArray<string>;
422
423    getWatchFolders(): ReadonlyArray<string>;
424
425    static DEFAULT_GRAPH_OPTIONS: {
426      customTransformOptions: any;
427      dev: boolean;
428      hot: boolean;
429      minify: boolean;
430    };
431
432    static DEFAULT_BUNDLE_OPTIONS: typeof Server.DEFAULT_GRAPH_OPTIONS & {
433      excludeSource: false;
434      inlineSourceMap: false;
435      modulesOnly: false;
436      onProgress: null;
437      runModule: true;
438      shallow: false;
439      sourceMapUrl: null;
440      sourceUrl: null;
441    };
442  }
443
444  //#endregion
445  //#region metro/src/shared/types.flow.js
446
447  type BundleType = 'bundle' | 'delta' | 'meta' | 'map' | 'ram' | 'cli' | 'hmr' | 'todo' | 'graph';
448
449  type MetroSourceMapOrMappings = MixedSourceMap | Array<MetroSourceMapSegmentTuple>;
450
451  export interface BundleOptions {
452    bundleType: BundleType;
453    customTransformOptions: CustomTransformOptions;
454    dev: boolean;
455    entryFile: string;
456    readonly excludeSource: boolean;
457    readonly hot: boolean;
458    readonly inlineSourceMap: boolean;
459    minify: boolean;
460    readonly modulesOnly: boolean;
461    onProgress: (doneCont: number, totalCount: number) => unknown | null | undefined;
462    readonly platform: string | null | undefined;
463    readonly runModule: boolean;
464    readonly shallow: boolean;
465    sourceMapUrl: string | null | undefined;
466    sourceUrl: string | null | undefined;
467    createModuleIdFactory?: () => (path: string) => number;
468  }
469
470  type ModuleTransportLike = {
471    readonly code: string;
472    readonly id: number;
473    readonly map: MetroSourceMapOrMappings | null | undefined;
474    readonly name?: string;
475    readonly sourcePath: string;
476  };
477
478  export interface OutputOptions {
479    bundleOutput: string;
480    bundleEncoding?: 'utf8' | 'utf16le' | 'ascii';
481    dev?: boolean;
482    indexedRamBundle?: boolean;
483    platform: string;
484    sourcemapOutput?: string;
485    sourcemapSourcesRoot?: string;
486    sourcemapUseAbsolutePath?: boolean;
487  }
488
489  export interface RequestOptions {
490    entryFile: string;
491    inlineSourceMap?: boolean;
492    sourceMapUrl?: string;
493    dev?: boolean;
494    minify: boolean;
495    platform: string;
496    createModuleIdFactory?: () => (path: string) => number;
497    onProgress?: (transformedFileCount: number, totalFileCount: number) => void;
498  }
499
500  //#endregion
501}
502