Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(polyfills): introduce @rnx-kit/polyfills #2580

Merged
merged 4 commits into from
Aug 8, 2023
Merged

Conversation

tido64
Copy link
Member

@tido64 tido64 commented Aug 3, 2023

Description

This is a polyfills "autolinker" for Metro. It works like native module autolinking, but gathers polyfills from dependencies instead.

Note

This package is temporary. Ideally, this should be upstreamed to @react-native-community/cli.

Test plan

Cherry-pick the BatteryStatus module:

git cherry-pick tido/webapis/battery-manager

Apply the following changes to the test app:

diff --git a/packages/test-app/metro.config.js b/packages/test-app/metro.config.js
index 7c0dcfc2..df0f8b0d 100644
--- a/packages/test-app/metro.config.js
+++ b/packages/test-app/metro.config.js
@@ -33,4 +33,7 @@ module.exports = makeMetroConfig({
     blacklistRE: blockList,
     blockList,
   },
+  serializer: {
+    getModulesRunBeforeMainModule: require("@rnx-kit/polyfills").default,
+  },
 });
diff --git a/packages/test-app/src/App.native.tsx b/packages/test-app/src/App.native.tsx
index 599634a9..b465f0fe 100644
--- a/packages/test-app/src/App.native.tsx
+++ b/packages/test-app/src/App.native.tsx
@@ -1,3 +1,5 @@
+// Temporary until we figure out how to magically inject WebAPIs
+import "@react-native-webapis/battery-status";
 import { acquireTokenWithScopes } from "@rnx-kit/react-native-auth";
 // Both `internal` imports are used to verify that `metro-resolver-symlinks`
 // resolves them correctly when `experimental_retryResolvingFromDisk` is
@@ -7,7 +9,7 @@ import {
   getRemoteDebuggingAvailability,
 } from "internal";
 import { getHermesVersion } from "internal/hermes";
-import React, { useCallback, useMemo, useState } from "react";
+import React, { useCallback, useEffect, useMemo, useState } from "react";
 import type { LayoutChangeEvent } from "react-native";
 import {
   NativeModules,
@@ -186,6 +188,14 @@ function App({ concurrentRoot }: { concurrentRoot?: boolean }) {
     [setFabric]
   );

+  const [batteryLevel, setBatteryLevel] = useState(-1);
+  useEffect(() => {
+    // @ts-expect-error FIXME
+    global.navigator.getBattery().then((status) => {
+      setBatteryLevel(status.level);
+    });
+  }, []);
+
   return (
     <SafeAreaView style={styles.body}>
       <StatusBar barStyle={isDarkMode ? "light-content" : "dark-content"} />
@@ -195,6 +205,9 @@ function App({ concurrentRoot }: { concurrentRoot?: boolean }) {
         style={styles.body}
       >
         <Header />
+        <View style={styles.group}>
+          <Feature value={batteryLevel.toFixed(2)}>Battery Level</Feature>
+        </View>
         <View style={styles.group}>
           <Button onPress={startAcquireToken}>Acquire Token</Button>
         </View>

Build and run:

yarn
cd packages/test-app
yarn build --dependencies
yarn bundle:ios
pod install --project-directory=ios
yarn ios

Note: When running in the simulator, the battery level will always be '-1'.

@github-actions github-actions bot added the chore Improvements that don't directly affect features label Aug 3, 2023
@tido64 tido64 force-pushed the tido/webapis/polyfills branch 4 times, most recently from c70ed71 to a53da8e Compare August 4, 2023 08:11
@github-actions github-actions bot removed the chore Improvements that don't directly affect features label Aug 4, 2023
@tido64 tido64 changed the title feat(webapis/polyfills): introduce @react-native-webapis/polyfills feat(polyfills): introduce @rnx-kit/polyfills Aug 4, 2023
@tido64 tido64 force-pushed the tido/webapis/polyfills branch 5 times, most recently from e4d0250 to c466918 Compare August 8, 2023 09:56
@tido64 tido64 mentioned this pull request Aug 8, 2023
@tido64 tido64 marked this pull request as ready for review August 8, 2023 10:33
Copy link
Contributor

@kelset kelset left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@tido64 tido64 enabled auto-merge (squash) August 8, 2023 14:20
@tido64 tido64 merged commit 7609d4a into main Aug 8, 2023
12 checks passed
@tido64 tido64 deleted the tido/webapis/polyfills branch August 8, 2023 14:36
@kelset kelset added the feature: webapis This is related to the experimental effort around WebAPIs for RN label Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature: webapis This is related to the experimental effort around WebAPIs for RN
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants