From 306edff62c5af8997a9760c2b7b7f600ced5510e Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Mon, 4 Jan 2021 07:37:56 -0800 Subject: [PATCH] Pass Systrace and Refresh as globals Summary: The `Systrace` and `Refresh` dependencies are injected into the `metroRequire` implementation by assigning the values to e.g. `require.Systrace = ...`. The issue with this approach is that some `require` implementations might not support extending the `require` object or doing so results in a degraded performance. An example where this is the case is Hermes where changing the `require` object forces Hermes to opt out of the static require optimization. This diff extends Metro so that the `Systrace` and `Refresh` implementation can either be injected by assigning to `require.Systrace` or by exposing the implementation in the global scope. It further changes the `Systrace` and `Refresh` modules to inject the instances using the global scope instead of extending `require`. Changelog: [Internal][Changed] - Expose Systrace and ReactRefresh as globals instead of extending require. Reviewed By: motiz88 Differential Revision: D25693381 fbshipit-source-id: 254d66d43e7a56d3310cf1a17d5146b8d1307562 --- Libraries/Core/setUpReactRefresh.js | 4 +++- Libraries/Performance/Systrace.js | 8 +++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Libraries/Core/setUpReactRefresh.js b/Libraries/Core/setUpReactRefresh.js index 4ee10acf086..ccc67ca2239 100644 --- a/Libraries/Core/setUpReactRefresh.js +++ b/Libraries/Core/setUpReactRefresh.js @@ -45,5 +45,7 @@ if (__DEV__) { }, }; - (require: any).Refresh = Refresh; + // The metro require polyfill can not have dependencies (applies for all polyfills). + // Expose `Refresh` by assigning it to global to make it available in the polyfill. + global[(global.__METRO_GLOBAL_PREFIX__ || '') + '__ReactRefresh'] = Refresh; } diff --git a/Libraries/Performance/Systrace.js b/Libraries/Performance/Systrace.js index 048c3d40427..6089016521b 100644 --- a/Libraries/Performance/Systrace.js +++ b/Libraries/Performance/Systrace.js @@ -209,11 +209,9 @@ const Systrace = { }; if (__DEV__) { - // This is needed, because require callis in polyfills are not processed as - // other files. Therefore, calls to `require('moduleId')` are not replaced - // with numeric IDs - // TODO(davidaurelio) Scan polyfills for dependencies, too (t9759686) - (require: $FlowFixMe).Systrace = Systrace; + // The metro require polyfill can not have dependencies (true for all polyfills). + // Ensure that `Systrace` is available in polyfill by exposing it globally. + global[(global.__METRO_GLOBAL_PREFIX__ || '') + '__SYSTRACE'] = Systrace; } module.exports = Systrace;