Files
react-native/scripts/build/prepack.js
T
Alex Hunt cc9164c337 Move JavaScript exports rewrites to publishConfig/prepack (#54857)
Summary:
### Motivation

Updates the shared JavaScript build setup to use the modern `publishConfig` convention.

This:

- Simplifies the build script.
- Makes the production values for `"exports"` more understandable in place (especially by separating from exports conditions).
- Prevents us from creating a dirty file state when running `yarn build`.

### Changes

- Add `publishConfig` to each `package.json` listing production `"exports"` targets.
- Add `scripts/build/prepack.js` script to action `publishConfig` (now on `npm pack`, `npm publish` exclusively).
- Remove `"exports"` rewriting (and un-rewriting safeguards) from build script.

**Note on `"prepack"`**

Slightly unfortunately, `publishConfig` doesn't work consistently between package managers currently, including npm — so this does not work implicitly (but may in future).

We're instead following `publishConfig` as a convention, and explicitly implementing a full copy (theoretically forking us towards pnpm and Yarn v4's approach).

However, I believe this is:

- Worthwhile, for the motivations above — and in particular being able to understand the final shape of `"exports"` (independent from the dimension of conditional exports, which may come into play later).
- Completely inspectable/maintainable as an explicit implementation (`scripts/build/prepack.js`).

Changelog: [Internal]

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

Test Plan:
### CI

✅ GitHub Actions

### End-to-end release test script

(Note: Rebased on `0.83-stable` when tested)

```
yarn test-release-local -t "RNTestProject" -p "iOS" -c $GITHUB_TOKEN
```

{F1984106139}

✅ Test script runs `npm publish` on packages to a local proxy.

{F1984106146}

✅ Installed packages have `publishConfig` `"exports"` values applied

NOTE: ⬆️ This is **exactly** the same output as before.

 {F1984106148}

✅ `/tmp/RNTestProject` runs using built + proxy-published + proxy-installed packages

Reviewed By: cipolleschi

Differential Revision: D88963450

Pulled By: huntie

fbshipit-source-id: f328252cf93a1f1039b79d7f369d1e6e7e5b4b52
2025-12-12 04:08:59 -08:00

46 lines
1.2 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
require('../shared/babelRegister').registerForScript();
const {promises: fs} = require('fs');
const path = require('path');
// "prepack" script to prepare JavaScript packages for publishing.
//
// We use this to copy over fields from "publishConfig" to the root of each
// package.json, which is not supported in Yarn v1.
async function prepack() {
const pkgJsonPath = path.join(process.cwd(), './package.json');
const contents = JSON.parse(await fs.readFile(pkgJsonPath, 'utf8'));
if (
path.dirname(pkgJsonPath).split(path.sep).slice(-2, -1)[0] !== 'packages'
) {
console.error('Error: prepack.js must be run from a package directory');
process.exitCode = 1;
return;
}
if (contents.publishConfig != null) {
for (const key of Object.keys(contents.publishConfig)) {
contents[key] = contents.publishConfig[key];
}
}
delete contents.publishConfig;
await fs.writeFile(pkgJsonPath, JSON.stringify(contents, null, 2) + '\n');
}
if (require.main === module) {
void prepack();
}