Skip to content

Commit ca7e7a8

Browse files
committed
feat: update package.json entry point for React Native compatibility
1 parent a76ad16 commit ca7e7a8

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -949,8 +949,8 @@ Because of compatibility issues with the original packages
949949
They are included in the source code of this project.
950950

951951
### React-Native
952-
React native uses *commonjs* versions by default and its Hermes engine does not support `TextDecoder`.
953-
That is why we have `./src/compatibility` that includes a `TextDecoder` polyfill.
952+
React native uses *commonjs* versions by default and its Hermes engine does not support `TextDecoder` as of (React Native 77) yet.
953+
That is why we have `./src/index-react-native.ts` that includes a `TextDecoder` polyfill.
954954

955955
## Contributing
956956

package.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@
1313
"dist",
1414
"index.html"
1515
],
16-
"sideEffects": [
17-
"./dist/esm/compatibility.js",
18-
"./dist/commonjs/compatibility.js"
19-
],
2016
"scripts": {
2117
"clean": "rimraf dist && rimraf tsconfig.tsbuildinfo",
22-
"build": "yarn clean && yarn compile",
18+
"build": "yarn clean && yarn compile && node ./scripts/post.build.js",
2319
"compile": "tshy && rollup -c && \\cp -f ./src/wrappers/cbor2Wrapper.ts ./dist/commonjs/wrappers/cbor2Wrapper.d.ts",
2420
"test": "node --experimental-vm-modules node_modules/.bin/jest",
2521
"prepublishOnly": "yarn build",
@@ -78,6 +74,10 @@
7874
"exports": {
7975
"./package.json": "./package.json",
8076
".": {
77+
"react-native": {
78+
"types": "./dist/commonjs/index-react-native.d.ts",
79+
"default": "./dist/commonjs/index-react-native.js"
80+
},
8181
"import": {
8282
"types": "./dist/esm/index.d.ts",
8383
"default": "./dist/esm/index.js"
@@ -89,5 +89,6 @@
8989
}
9090
},
9191
"main": "./dist/commonjs/index.js",
92+
"react-native": "./dist/commonjs/index-react-native.js",
9293
"module": "./dist/esm/index.js"
93-
}
94+
}

scripts/post.build.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Edit package.json here to inject react-native exports
2+
3+
import fs from "fs";
4+
import path from "path";
5+
import { fileURLToPath } from "url";
6+
7+
// Get the package.json file
8+
const __filename = fileURLToPath(import.meta.url);
9+
const __dirname = path.dirname(__filename);
10+
const packageJsonPath = path.resolve(__dirname, "../package.json");
11+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
12+
13+
// Add react-native export as the first entry
14+
const exports = packageJson.exports['.'];
15+
packageJson.exports['.'] = {
16+
"react-native": {
17+
"types": "./dist/commonjs/index-react-native.d.ts",
18+
"default": "./dist/commonjs/index-react-native.js"
19+
},
20+
...exports
21+
};
22+
23+
// Write the updated package.json back to its path
24+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));

src/compatibility.ts renamed to src/index-react-native.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@
55
*
66
* So lets inject TextDecoder polyfill for React Native if it is not supported
77
*/
8-
import "@bacons/text-decoder/install";
8+
import "@bacons/text-decoder/install";
9+
10+
// Export everything from the main index file
11+
//@ts-ignore
12+
export * from "./index.js";

src/index.ts

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// Before everyhing else, import the compatibility layer for react-native
2-
import "./compatibility.js";
3-
41
import { Ur } from "./classes/Ur.js"
52
import { UrRegistry } from "./registry.js"
63
import { registryItemFactory, RegistryItemBase } from "./classes/RegistryItem.js"

0 commit comments

Comments
 (0)