Skip to content

Commit f023961

Browse files
author
Sverre Johansen
committed
Add resolutionPlatforms for better React Native support
1 parent 0951a94 commit f023961

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

src/compiler/commandLineParser.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,17 @@ namespace ts {
415415
category: Diagnostics.Module_Resolution_Options,
416416
description: Diagnostics.Do_not_resolve_the_real_path_of_symlinks,
417417
},
418-
418+
{
419+
name: "resolutionPlatforms",
420+
type: "list",
421+
element: {
422+
name: "types",
423+
type: "string"
424+
},
425+
showInSimplifiedHelpView: true,
426+
category: Diagnostics.Module_Resolution_Options,
427+
description: Diagnostics.List_of_platform_extensions_to_fallback_on
428+
},
419429
// Source Maps
420430
{
421431
name: "sourceRoot",

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3889,6 +3889,10 @@
38893889
"category": "Message",
38903890
"code": 90029
38913891
},
3892+
"List of platform extensions to fallback on": {
3893+
"category": "Message",
3894+
"code": 910044
3895+
},
38923896
"Convert function to an ES2015 class": {
38933897
"category": "Message",
38943898
"code": 95001

src/compiler/moduleNameResolver.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -925,22 +925,46 @@ namespace ts {
925925
}
926926

927927
/** Return the file if it exists. */
928-
function tryFile(fileName: string, failedLookupLocations: Push<string>, onlyRecordFailures: boolean, state: ModuleResolutionState): string | undefined {
929-
if (!onlyRecordFailures) {
930-
if (state.host.fileExists(fileName)) {
931-
if (state.traceEnabled) {
932-
trace(state.host, Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName);
928+
function tryFile(file: string, failedLookupLocations: Push<string>, onlyRecordFailures: boolean, state: ModuleResolutionState): string | undefined {
929+
930+
if (state.compilerOptions.resolutionPlatforms){
931+
for(let platform of state.compilerOptions.resolutionPlatforms) {
932+
let result = tryFileInner(platform);
933+
if (result) {
934+
return result;
933935
}
934-
return fileName;
935936
}
936-
else {
937-
if (state.traceEnabled) {
938-
trace(state.host, Diagnostics.File_0_does_not_exist, fileName);
937+
}
938+
939+
return tryFileInner(null);
940+
941+
function tryFileInner(platform: string): string | undefined {
942+
943+
let fileName = file;
944+
if (platform) {
945+
let lastDot = file.lastIndexOf('.');
946+
if (lastDot === -1) {
947+
return undefined;
948+
}
949+
fileName = file.slice(0, lastDot + 1) + platform + file.slice(lastDot);
950+
}
951+
952+
if (!onlyRecordFailures) {
953+
if (state.host.fileExists(fileName)) {
954+
if (state.traceEnabled) {
955+
trace(state.host, Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName);
956+
}
957+
return fileName;
958+
}
959+
else {
960+
if (state.traceEnabled) {
961+
trace(state.host, Diagnostics.File_0_does_not_exist, fileName);
962+
}
939963
}
940964
}
965+
failedLookupLocations.push(fileName);
966+
return undefined;
941967
}
942-
failedLookupLocations.push(fileName);
943-
return undefined;
944968
}
945969

946970
function loadNodeModuleFromDirectory(extensions: Extensions, candidate: string, failedLookupLocations: Push<string>, onlyRecordFailures: boolean, state: ModuleResolutionState, considerPackageJson = true) {

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4019,6 +4019,7 @@ namespace ts {
40194019
reactNamespace?: string;
40204020
jsxFactory?: string;
40214021
removeComments?: boolean;
4022+
resolutionPlatforms?: string[]; // Use react-native lookup logic for these platforms
40224023
rootDir?: string;
40234024
rootDirs?: string[];
40244025
skipLibCheck?: boolean;

0 commit comments

Comments
 (0)