From 5c6897eba2ac6d5524f554f11f5baaa5d3156e43 Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Tue, 17 Jan 2023 04:11:06 -0800 Subject: [PATCH] Add experimental package exports config options Summary: Add new experimental/unstable config options which will be used by incoming package exports support. Changelog: **[Experimental]** Add `resolver.unstable_enablePackageExports` config option Reviewed By: motiz88 Differential Revision: D42531005 fbshipit-source-id: a73500523a96106d1551d8796996551f32321cdd --- .../__snapshots__/loadConfig-test.js.snap | 40 +++++++++++++++++++ packages/metro-config/src/configTypes.flow.js | 5 +++ packages/metro-config/src/defaults/index.js | 5 +++ .../src/__tests__/index-test.js | 3 ++ packages/metro-resolver/src/types.js | 5 +++ .../metro/src/node-haste/DependencyGraph.js | 5 +++ .../DependencyGraph/ModuleResolution.js | 7 ++++ 7 files changed, 70 insertions(+) diff --git a/packages/metro-config/src/__tests__/__snapshots__/loadConfig-test.js.snap b/packages/metro-config/src/__tests__/__snapshots__/loadConfig-test.js.snap index fcc25d9a9e..b765ecc1a0 100644 --- a/packages/metro-config/src/__tests__/__snapshots__/loadConfig-test.js.snap +++ b/packages/metro-config/src/__tests__/__snapshots__/loadConfig-test.js.snap @@ -73,6 +73,16 @@ Object { "ts", "tsx", ], + "unstable_conditionNames": Array [ + "import", + "require", + ], + "unstable_conditionsByPlatform": Object { + "web": Array [ + "browser", + ], + }, + "unstable_enablePackageExports": false, "useWatchman": true, }, "serializer": Object { @@ -239,6 +249,16 @@ Object { "ts", "tsx", ], + "unstable_conditionNames": Array [ + "import", + "require", + ], + "unstable_conditionsByPlatform": Object { + "web": Array [ + "browser", + ], + }, + "unstable_enablePackageExports": false, "useWatchman": true, }, "serializer": Object { @@ -405,6 +425,16 @@ Object { "ts", "tsx", ], + "unstable_conditionNames": Array [ + "import", + "require", + ], + "unstable_conditionsByPlatform": Object { + "web": Array [ + "browser", + ], + }, + "unstable_enablePackageExports": false, "useWatchman": true, }, "serializer": Object { @@ -571,6 +601,16 @@ Object { "ts", "tsx", ], + "unstable_conditionNames": Array [ + "import", + "require", + ], + "unstable_conditionsByPlatform": Object { + "web": Array [ + "browser", + ], + }, + "unstable_enablePackageExports": false, "useWatchman": true, }, "serializer": Object { diff --git a/packages/metro-config/src/configTypes.flow.js b/packages/metro-config/src/configTypes.flow.js index 96ac6767dd..c4b15e1925 100644 --- a/packages/metro-config/src/configTypes.flow.js +++ b/packages/metro-config/src/configTypes.flow.js @@ -109,6 +109,11 @@ type ResolverConfigT = { resolveRequest: ?CustomResolver, resolverMainFields: $ReadOnlyArray, sourceExts: $ReadOnlyArray, + unstable_conditionNames: $ReadOnlyArray, + unstable_conditionsByPlatform: $ReadOnly<{ + [platform: string]: $ReadOnlyArray, + }>, + unstable_enablePackageExports: boolean, useWatchman: boolean, requireCycleIgnorePatterns: $ReadOnlyArray, }; diff --git a/packages/metro-config/src/defaults/index.js b/packages/metro-config/src/defaults/index.js index 54d7cb3617..26d75330a0 100644 --- a/packages/metro-config/src/defaults/index.js +++ b/packages/metro-config/src/defaults/index.js @@ -47,6 +47,11 @@ const getDefaultValues = (projectRoot: ?string): ConfigT => ({ nodeModulesPaths: [], resolveRequest: null, resolverMainFields: ['browser', 'main'], + unstable_conditionNames: ['import', 'require'], + unstable_conditionsByPlatform: { + web: ['browser'], + }, + unstable_enablePackageExports: false, useWatchman: true, requireCycleIgnorePatterns: [/(^|\/|\\)node_modules($|\/|\\)/], }, diff --git a/packages/metro-resolver/src/__tests__/index-test.js b/packages/metro-resolver/src/__tests__/index-test.js index d1b812e685..4e484bb571 100644 --- a/packages/metro-resolver/src/__tests__/index-test.js +++ b/packages/metro-resolver/src/__tests__/index-test.js @@ -120,6 +120,9 @@ const CONTEXT: ResolutionContext = (() => { return null; }, sourceExts: ['js'], + unstable_conditionNames: [], + unstable_conditionsByPlatform: {}, + unstable_enablePackageExports: false, }; })(); diff --git a/packages/metro-resolver/src/types.js b/packages/metro-resolver/src/types.js index 1833e3c868..f2196a4862 100644 --- a/packages/metro-resolver/src/types.js +++ b/packages/metro-resolver/src/types.js @@ -120,6 +120,11 @@ export type ResolutionContext = $ReadOnly<{ disableHierarchicalLookup: boolean, extraNodeModules: ?{[string]: string, ...}, originModulePath: string, + unstable_conditionNames: $ReadOnlyArray, + unstable_conditionsByPlatform: $ReadOnly<{ + [platform: string]: $ReadOnlyArray, + }>, + unstable_enablePackageExports: boolean, resolveRequest?: ?CustomResolver, customResolverOptions: CustomResolverOptions, ... diff --git a/packages/metro/src/node-haste/DependencyGraph.js b/packages/metro/src/node-haste/DependencyGraph.js index 1d5d02c77a..b763bcc629 100644 --- a/packages/metro/src/node-haste/DependencyGraph.js +++ b/packages/metro/src/node-haste/DependencyGraph.js @@ -209,6 +209,11 @@ class DependencyGraph extends EventEmitter { }, resolveRequest: this._config.resolver.resolveRequest, sourceExts: this._config.resolver.sourceExts, + unstable_conditionNames: this._config.resolver.unstable_conditionNames, + unstable_conditionsByPlatform: + this._config.resolver.unstable_conditionsByPlatform, + unstable_enablePackageExports: + this._config.resolver.unstable_enablePackageExports, }); } diff --git a/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js b/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js index a2c2b2a8a6..24641a4321 100644 --- a/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js +++ b/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js @@ -71,6 +71,11 @@ type Options = $ReadOnly<{ resolveAsset: ResolveAsset, resolveRequest: ?CustomResolver, sourceExts: $ReadOnlyArray, + unstable_conditionNames: $ReadOnlyArray, + unstable_conditionsByPlatform: $ReadOnly<{ + [platform: string]: $ReadOnlyArray, + }>, + unstable_enablePackageExports: boolean, }>; class ModuleResolver { @@ -187,6 +192,8 @@ class ModuleResolver { resolveHastePackage: (name: string) => this._options.getHastePackagePath(name, platform), getPackageMainPath: this._getPackageMainPath, + unstable_enablePackageExports: + this._options.unstable_enablePackageExports, }, moduleName, platform,