xref: /expo/packages/expo/types/metro-require.d.ts (revision a2441f5f)
1// Based on https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/webpack-env/index.d.ts
2// Adds support for the runtime `require.context` method.
3// https://github.com/facebook/metro/pull/822/
4
5declare var module: NodeModule;
6
7declare namespace __MetroModuleApi {
8  interface RequireContext {
9    /** Return the keys that can be resolved. */
10    keys(): string[];
11    (id: string): any;
12    <T>(id: string): T;
13    /** **Unimplemented:** Return the module identifier for a user request. */
14    resolve(id: string): string;
15    /** **Unimplemented:** Readable identifier for the context module. */
16    id: string;
17  }
18
19  interface RequireFunction {
20    /**
21     * Returns the exports from a dependency. The call is sync. No request to the server is fired. The compiler ensures that the dependency is available.
22     */
23    (path: string): any;
24    <T>(path: string): T;
25
26    /**
27     * **Experimental:** Import all modules in a given directory. This module dynamically updates when the files in a directory are added or removed.
28     *
29     * **Enabling:** This feature can be enabled by setting the `transformer.unstable_allowRequireContext` property to `true` in your Metro configuration.
30     *
31     * @param path File path pointing to the directory to require.
32     * @param recursive Should search for files recursively. Optional, default `true` when `require.context` is used.
33     * @param filter Filename filter pattern for use in `require.context`. Optional, default `.*` (any file) when `require.context` is used.
34     * @param mode Mode for resolving dynamic dependencies. Defaults to `sync`.
35     */
36    context(
37      path: string,
38      recursive?: boolean,
39      filter?: RegExp,
40      mode?: 'sync' | 'eager' | 'weak' | 'lazy' | 'lazy-once'
41    ): RequireContext;
42  }
43}
44
45/**
46 * Declare process variable
47 */
48declare namespace NodeJS {
49  interface Require extends __MetroModuleApi.RequireFunction {}
50}
51
52declare var require: NodeRequire;
53