Define "exports" field on main package (#50320)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/50320

Define `"exports"` field on the main `react-native` package.

**Notes**

Module resolution should be equivalent to the previous implicit `"main"` field (backwards compatible).

- Exports all module subpaths to JavaScript (Flow) source files, with and without `.js` suffix (unchanged ✅)
    - These are restricted to the `flow/` and `Libraries/` subdirectories (ℹ️ this should be unchanged, matching any JS imports apps may have today)
        - Still includes 3P integration scripts such as `./jest-preset.js` and `./rn-get-polyfills.js` (unchanged ✅)
- Exports `./package.json` (unchanged ✅)
- TypeScript should:
    - fall back to the `"types"` field (unchanged ✅)
    - OR to `"."`,`"./*"` when Package Exports support is enabled via `compilerOptions`, and use the *adjacent `.d.ts` file* (unchanged ✅)

Changelog:
[General][Breaking] - The `react-native` package now defines package.json `"exports"`.
- While these expose existing JavaScript and TypeScript modules, this change may affect deep imports of non-JS files via Node in third party tools.
- Imports from `src/` and `src/private/` directories are disallowed.

Reviewed By: robhogan

Differential Revision: D71968187

fbshipit-source-id: 28ff941692d1822a916457c4fb891e200e3bca61
This commit is contained in:
Alex Hunt
2025-03-31 02:37:13 -07:00
committed by Facebook GitHub Bot
parent 398ac1f716
commit 9fc2a9b9e6
+18 -5
View File
@@ -26,7 +26,25 @@
"bin": {
"react-native": "cli.js"
},
"main": "./index.js",
"types": "types",
"exports": {
".": "./index.js",
"./*": "./*.js",
"./*.js": "./*.js",
"./Libraries/*.d.ts": "./Libraries/*.d.ts",
"./types/*.d.ts": "./types/*.d.ts",
"./gradle/*": null,
"./React/*": null,
"./ReactAndroid/*": null,
"./ReactApple/*": null,
"./ReactCommon/*": null,
"./sdks/*": null,
"./src/*": null,
"./third-party-podspecs/*": null,
"./types/*": null,
"./package.json": "./package.json"
},
"jest-junit": {
"outputDirectory": "reports/junit",
"outputName": "js-test-results.xml"
@@ -84,11 +102,6 @@
"sdks/hermesc",
"settings.gradle.kts",
"src",
"template.config.js",
"template",
"!template/node_modules",
"!template/package-lock.json",
"!template/yarn.lock",
"third-party-podspecs",
"types"
],