Skip to content

Commit

Permalink
remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Aug 9, 2023
1 parent 32ae724 commit 0a3f7e0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 102 deletions.
2 changes: 1 addition & 1 deletion incubator/@react-native-webapis/battery-status/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")],
],
},
],
Expand Down
12 changes: 4 additions & 8 deletions incubator/polyfills/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
53 changes: 0 additions & 53 deletions incubator/polyfills/babel-plugin.js

This file was deleted.

1 change: 0 additions & 1 deletion incubator/polyfills/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"email": "microsoftopensource@users.noreply.github.com"
},
"files": [
"babel-plugin.js",
"lib/*"
],
"main": "lib/index.js",
Expand Down
86 changes: 47 additions & 39 deletions incubator/polyfills/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 ?? "<unnamed module>";

const polyfills = getDependencyPolyfills({ projectRoot: context.cwd });
const importPolyfill = babelTemplate(`import %%source%%;`);

for (const polyfill of polyfills) {
path.unshiftContainer(
"body",
importPolyfill({ source: t.stringLiteral(polyfill) })
);
}
},
},
};
});

0 comments on commit 0a3f7e0

Please sign in to comment.