From 0026f73360c07e3f6289b16a08b960025743ea1d Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Thu, 23 Mar 2023 06:06:42 -0700 Subject: [PATCH] Remove 'import' from default Package Exports conditions (#955) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: X-link: https://github.com/facebook/metro/pull/955 Pull Request resolved: https://github.com/facebook/react-native/pull/36584 Changelog: [General][Changed] - Default condition set for experimental Package Exports is now `['require', 'react-native']` The [Exports RFC](https://github.com/react-native-community/discussions-and-proposals/blob/main/proposals/0534-metro-package-exports-support.md) had assumed that supporting the `"import"` condition was a syntax-only difference, given we are not in a Node.js environment — and so was worthwhile to support for maximal ecosystem compatibility. {F915841105} This assumption is similar to [`--moduleResolution bundler` in TypeScript 5.0](https://github.com/microsoft/TypeScript/pull/51669): > bundlers and runtimes that include a range of Node-like resolution features and ESM syntax, but do not enforce the strict resolution rules that accompany ES modules in Node or in the browser > -- https://github.com/microsoft/TypeScript/pull/51669#issue-1467004047 However, robhogan has rightly pointed out that **we should not do this!** - ESM (once transpiled) is **not** simply a stricter subset of in-scope features supported by CJS. For example, it supports top-level async, which would be breaking at runtime. - We recently made the same change for our Jest environment: - https://github.com/facebook/react-native/commit/681d7f8113d2b5e9d6966255ee6c72b50a7d488a As such, we are erring on the side of correctness and supporting only `['require', 'react-native']` in our defaults. At runtime, all code run by React Native is anticipated to be CommonJS. `"exports"` will instead allow React Native to correctly select the CommonJS versions of modules from all npm packages. Metro changelog: [Experimental] Package Exports `unstable_conditionNames` now defaults to `['require']` Reviewed By: robhogan Differential Revision: D44303559 fbshipit-source-id: 0077e547e7775e53d1e4e9c3a9d01347f4fb7d4a --- packages/metro-config/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/metro-config/index.js b/packages/metro-config/index.js index 07a16e217c3d2b..30a9e0c1d7c2f6 100644 --- a/packages/metro-config/index.js +++ b/packages/metro-config/index.js @@ -42,7 +42,7 @@ function getDefaultConfig( resolver: { resolverMainFields: ['react-native', 'browser', 'main'], platforms: ['android', 'ios'], - unstable_conditionNames: ['import', 'require', 'react-native'], + unstable_conditionNames: ['require', 'react-native'], }, serializer: { getPolyfills: () => require('@react-native/js-polyfills')(),