From 3519dc9f2bd8a22922b661f28309f2ade665bca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Sat, 11 Nov 2023 15:21:03 -0500 Subject: [PATCH 1/2] Update types for `require` and `require.context` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's been 1½ years since the merge of https://github.com/facebook/metro/pull/822 and my hope is that this feature is ready to be documented and made available through types. --- packages/react-native/types/index.d.ts | 31 ++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/react-native/types/index.d.ts b/packages/react-native/types/index.d.ts index b23e2e0a2ff2..138507b80979 100644 --- a/packages/react-native/types/index.d.ts +++ b/packages/react-native/types/index.d.ts @@ -161,11 +161,38 @@ export * from './public/ReactNativeTypes'; import type {ErrorUtils} from '../Libraries/vendor/core/ErrorUtils'; declare global { - interface NodeRequire { + interface ReactNativeRequire { (id: string): any; + /** + * Instructs Metro to generate a virtual module in the dependency graph, + * with dependencies on every file in the file map that matches the context params. + * This can be used to perform dynamic imports from files on disk at build time. + * @note Metro parses for `require.context()` calls in the code while building. + * @see https://github.com/facebook/metro/pull/822 + * @see https://webpack.js.org/guides/dependency-management/#requirecontext + */ + context( + directory: string, + useSubdirectories?: boolean, + regExp?: RegExp, + mode?: 'sync' | 'lazy', + ): MetroRequireContext; + } + + /** + * Returned from `require.context` provided by Metro. + * @see https://github.com/facebook/metro/pull/822 + * @see https://webpack.js.org/guides/dependency-management/#requirecontext + */ + interface MetroRequireContext { + (id: string): any; + /** + * @return an array of all possible requests that the context module can handle. + */ + keys(): string[]; } - var require: NodeRequire; + var require: ReactNativeRequire; /** * Console polyfill From 34f02c88d8f0a6b73e754113e2c72cfbc5fd15f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Mon, 13 Nov 2023 13:54:18 -0500 Subject: [PATCH 2/2] Update index.d.ts --- packages/react-native/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-native/types/index.d.ts b/packages/react-native/types/index.d.ts index 138507b80979..284ac991f23d 100644 --- a/packages/react-native/types/index.d.ts +++ b/packages/react-native/types/index.d.ts @@ -171,7 +171,7 @@ declare global { * @see https://github.com/facebook/metro/pull/822 * @see https://webpack.js.org/guides/dependency-management/#requirecontext */ - context( + context?( directory: string, useSubdirectories?: boolean, regExp?: RegExp,