cli
cli copied to clipboard
feat: support ESM in `react-native.config`
Summary:
Closes https://github.com/react-native-community/cli/issues/2167
Add support for ESM inside react-native.config file.
Test Plan:
- Clone the repository and do all the required steps from the Contributing guide
- Create a new project and add
type: "module"inside package ✅ - Create a
react-native.config.mjswith following content
export default {
commands: [
{
name: 'hello',
func: () => {
console.log('hello esm');
},
},
],
};
- Run
npx react-native helloand you should seehello esmlog.
Checklist
- [x] Documentation is up to date to reflect these changes.
- [x] Follows commit message convention described in CONTRIBUTING.md
Re: test plan, test these scenarios:
- Add
type: 'module'topackage.jsonand userequireinreact-native.config.js- should fail unless usingcreateRequirehelper (import { createRequire } from 'node:module'; const require = createRequire(import.meta.url);) - Add
type: 'module'topackage.jsonand userequireinreact-native.config.cjs - Add
type: 'module'topackage.jsonand useimport/exportinreact-native.config.js - Remove
type: 'modulefrompackage.json/addtype: 'commonjs'and useimport/exportinreact-native.config.mjs
You don’t need both type: 'module' and .mjs. either is enough to signal ES modules.
Changing the signature of loadConfig would be breaking e.g. for rnx-kit. We could consider creating loadConfigAsync or so to avoid it. cc @tido64
Changing the signature of
loadConfigwould be breaking e.g. for rnx-kit. We could consider creatingloadConfigAsyncor so to avoid it. cc @tido64
Thanks, this would indeed break us. I'm also curious why everything needs be async just because of ESM? IMHO, async makes sense if you're doing a lot of things at once and then awaiting them all. But it looks like we're just async/awaiting in sequence, so I'm not really seeing any benefits.
everything needs be async just because of ESM
Because it's loading ES module dynamically, which needs to be async.
Nice, thanks to all of the contributors and reviewers here, great to have ESM in react-native.config.js files!
I'm not super aware of the upstreaming / integration process here in React Native projects - will this work right away out of the box with @react-native-community/[email protected]?
Eg:
- I guess new React Native projects which start without a framework will receive this feature right away now?
- Does this affect Expo projects at all? (I guess not)
Yes, this should work with v15.1. You'll need to update the template (or your project files) to reference the newer version of the CLI. It doesn't affect Expo projects