Skip to content

Commit b8da895

Browse files
committed
Add platform logic
1 parent 8890dcb commit b8da895

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

src/compiler/commandLineParser.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,17 @@ namespace ts {
701701
category: Diagnostics.Module_Resolution_Options,
702702
description: Diagnostics.Do_not_resolve_the_real_path_of_symlinks,
703703
},
704+
{
705+
name: "resolutionPlatforms",
706+
type: "list",
707+
element: {
708+
name: "types",
709+
type: "string"
710+
},
711+
showInSimplifiedHelpView: true,
712+
category: Diagnostics.Module_Resolution_Options,
713+
description: Diagnostics.List_of_platform_extensions_to_fallback_on
714+
},
704715
{
705716
name: "allowUmdGlobalAccess",
706717
type: "boolean",

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5268,6 +5268,10 @@
52685268
"category": "Message",
52695269
"code": 90029
52705270
},
5271+
"List of platform extensions to fallback on": {
5272+
"category": "Message",
5273+
"code": 910044
5274+
},
52715275
"Replace 'infer {0}' with 'unknown'": {
52725276
"category": "Message",
52735277
"code": 90030

src/compiler/moduleNameResolver.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,22 +1109,44 @@ namespace ts {
11091109
}
11101110

11111111
/** Return the file if it exists. */
1112-
function tryFile(fileName: string, onlyRecordFailures: boolean, state: ModuleResolutionState): string | undefined {
1113-
if (!onlyRecordFailures) {
1114-
if (state.host.fileExists(fileName)) {
1115-
if (state.traceEnabled) {
1116-
trace(state.host, Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName);
1112+
function tryFile(file: string, onlyRecordFailures: boolean, state: ModuleResolutionState): string | undefined {
1113+
1114+
if (state.compilerOptions.resolutionPlatforms){
1115+
for(let platform of state.compilerOptions.resolutionPlatforms) {
1116+
let result = tryFileInner(platform);
1117+
if (result) {
1118+
return result;
11171119
}
1118-
return fileName;
11191120
}
1120-
else {
1121-
if (state.traceEnabled) {
1122-
trace(state.host, Diagnostics.File_0_does_not_exist, fileName);
1121+
}
1122+
1123+
return tryFileInner(null);
1124+
1125+
function tryFileInner(platform: string | null): string | undefined {
1126+
1127+
let fileName = file;
1128+
if (platform) {
1129+
let lastDot = file.lastIndexOf('.');
1130+
if (lastDot === -1) {
1131+
return undefined;
1132+
}
1133+
fileName = file.slice(0, lastDot + 1) + platform + file.slice(lastDot);
1134+
} if (!onlyRecordFailures) {
1135+
if (state.host.fileExists(fileName)) {
1136+
if (state.traceEnabled) {
1137+
trace(state.host, Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName);
1138+
}
1139+
return fileName;
1140+
}
1141+
else {
1142+
if (state.traceEnabled) {
1143+
trace(state.host, Diagnostics.File_0_does_not_exist, fileName);
1144+
}
11231145
}
11241146
}
1147+
state.failedLookupLocations.push(fileName);
1148+
return undefined;
11251149
}
1126-
state.failedLookupLocations.push(fileName);
1127-
return undefined;
11281150
}
11291151

11301152
function loadNodeModuleFromDirectory(extensions: Extensions, candidate: 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
@@ -5728,6 +5728,7 @@ namespace ts {
57285728
incremental?: boolean;
57295729
tsBuildInfoFile?: string;
57305730
removeComments?: boolean;
5731+
resolutionPlatforms?: string[]; // Use react-native lookup logic for these platforms
57315732
rootDir?: string;
57325733
rootDirs?: string[];
57335734
skipLibCheck?: boolean;

0 commit comments

Comments
 (0)