From 0a3f7e080c05964a82de4ee0d29c39c929f7217a Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Wed, 9 Aug 2023 14:52:02 +0200 Subject: [PATCH] remove unused code --- .../battery-status/README.md | 2 +- incubator/polyfills/README.md | 12 +-- incubator/polyfills/babel-plugin.js | 53 ------------ incubator/polyfills/package.json | 1 - incubator/polyfills/src/index.ts | 86 ++++++++++--------- 5 files changed, 52 insertions(+), 102 deletions(-) delete mode 100644 incubator/polyfills/babel-plugin.js diff --git a/incubator/@react-native-webapis/battery-status/README.md b/incubator/@react-native-webapis/battery-status/README.md index f4b1a4d927..68228f24c8 100644 --- a/incubator/@react-native-webapis/battery-status/README.md +++ b/incubator/@react-native-webapis/battery-status/README.md @@ -42,7 +42,7 @@ index 69ebd557..a012b7f5 100644 { runtime: "automatic" }, ], [require("@babel/plugin-transform-react-jsx-source")], -+ [require("@rnx-kit/polyfills/babel-plugin")], ++ [require("@rnx-kit/polyfills")], ], }, ], diff --git a/incubator/polyfills/README.md b/incubator/polyfills/README.md index 208f362979..f525bb5dfb 100644 --- a/incubator/polyfills/README.md +++ b/incubator/polyfills/README.md @@ -9,14 +9,9 @@ 🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧 -This is a polyfills "autolinker" for Metro. It works like native module +This is a polyfills "autolinker" for React Native. 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`. - ## Motivation Please read the @@ -45,11 +40,12 @@ npm add --save-dev @rnx-kit/polyfills // babel.config.js module.exports = { presets: ["module:@react-native/babel-preset"], - + plugins: [require("@rnx-kit/polyfills/babel-plugin")], + + plugins: [require("@rnx-kit/polyfills")], }; ``` -2. In your `index.js` (or `index.ts`), add the following comment: +2. In your `index.ts` (or `index.js`), add the following comment at the top of + the file: ``` // @react-native-webapis diff --git a/incubator/polyfills/babel-plugin.js b/incubator/polyfills/babel-plugin.js deleted file mode 100644 index 694ff4832d..0000000000 --- a/incubator/polyfills/babel-plugin.js +++ /dev/null @@ -1,53 +0,0 @@ -/* jshint esversion: 8, node: true */ -// @ts-check -"use strict"; - -const { types: t } = require("@babel/core"); -const { declare } = require("@babel/helper-plugin-utils"); -const { default: babelTemplate } = require("@babel/template"); - -module.exports = declare((api) => { - api.assertVersion(7); - - const pluginName = "@react-native-webapis/polyfills"; - - /** @type {string | null} */ - let isPolyfilled = null; - - return { - name: pluginName, - visitor: { - Program: (path, context) => { - const leadingComments = path.node.body[0]?.leadingComments; - const codegen = leadingComments?.some((comment) => { - const normalizedComment = comment.value.trim().split(" ")[0].trim(); - return normalizedComment.startsWith("@react-native-webapis"); - }); - - if (!codegen) { - return; - } - - if (isPolyfilled != null) { - throw new Error( - `'${pluginName}' is already applied to ${isPolyfilled}` - ); - } - - isPolyfilled = context.file.opts.filename ?? ""; - - const { getDependencyPolyfills } = require("./lib/dependency"); - const polyfills = getDependencyPolyfills({ projectRoot: context.cwd }); - - const importPolyfill = babelTemplate(`import %%source%%;`); - - for (const polyfill of polyfills) { - path.unshiftContainer( - "body", - importPolyfill({ source: t.stringLiteral(polyfill) }) - ); - } - }, - }, - }; -}); diff --git a/incubator/polyfills/package.json b/incubator/polyfills/package.json index 68a05c44b1..1cf82a2759 100644 --- a/incubator/polyfills/package.json +++ b/incubator/polyfills/package.json @@ -10,7 +10,6 @@ "email": "microsoftopensource@users.noreply.github.com" }, "files": [ - "babel-plugin.js", "lib/*" ], "main": "lib/index.js", diff --git a/incubator/polyfills/src/index.ts b/incubator/polyfills/src/index.ts index 4d106d1b83..a61a267d42 100644 --- a/incubator/polyfills/src/index.ts +++ b/incubator/polyfills/src/index.ts @@ -1,40 +1,48 @@ +import type { ConfigAPI } from "@babel/core"; +import { types as t } from "@babel/core"; +import { declare } from "@babel/helper-plugin-utils"; +import babelTemplate from "@babel/template"; import { getDependencyPolyfills } from "./dependency"; -import type { Context, GetPreludeModules } from "./types"; - -/** - * Ideally, we'd need something between `serializer.getPolyfills` and - * `serializer.getModulesRunBeforeMainModule`. The former does not have access - * to `require`, while the latter requires that the listed modules are - * explicitly used in the bundle itself (see - * https://github.com/facebook/metro/issues/850). For now, we will use this fact - * to simply list all prelude modules. - */ -function defaultModules({ projectRoot }: Context): string[] { - const platforms = [ - "react-native", - "react-native-macos", - "react-native-windows", - ]; - const options = { paths: [projectRoot] }; - - const modules = []; - for (const platform of platforms) { - const core = `${platform}/Libraries/Core/InitializeCore`; - try { - modules.push(require.resolve(core, options)); - } catch (_) { - // ignore - } - } - - return modules; -} - -export const getPreludeModules: GetPreludeModules = () => { - const context = { projectRoot: process.cwd() }; - const modules = defaultModules(context); - const dependencyPolyfills = getDependencyPolyfills(context); - return modules.concat(dependencyPolyfills); -}; - -export default getPreludeModules; + +module.exports = declare((api: ConfigAPI) => { + api.assertVersion(7); + + const pluginName = "@react-native-webapis/polyfills"; + + let isPolyfilled: string | null = null; + + return { + name: pluginName, + visitor: { + Program: (path, context) => { + const leadingComments = path.node.body[0]?.leadingComments; + const codegen = leadingComments?.some((comment) => { + const normalizedComment = comment.value.trim().split(" ")[0].trim(); + return normalizedComment.startsWith("@react-native-webapis"); + }); + + if (!codegen) { + return; + } + + if (isPolyfilled != null) { + throw new Error( + `'${pluginName}' is already applied to ${isPolyfilled}` + ); + } + + isPolyfilled = context.file.opts.filename ?? ""; + + const polyfills = getDependencyPolyfills({ projectRoot: context.cwd }); + const importPolyfill = babelTemplate(`import %%source%%;`); + + for (const polyfill of polyfills) { + path.unshiftContainer( + "body", + importPolyfill({ source: t.stringLiteral(polyfill) }) + ); + } + }, + }, + }; +});