Skip to content

Commit 49bbdea

Browse files
alexeaglegregmagolan
authored andcommitted
microsoft#48190 on top of v4.3.5
1 parent 1294ee8 commit 49bbdea

File tree

6 files changed

+599
-455
lines changed

6 files changed

+599
-455
lines changed

package-lock.json

Lines changed: 522 additions & 451 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"@types/mkdirp": "latest",
4848
"@types/mocha": "latest",
4949
"@types/ms": "latest",
50-
"@types/node": "latest",
50+
"@types/node": "14.x.x",
5151
"@types/node-fetch": "^2.3.4",
5252
"@types/q": "latest",
5353
"@types/source-map-support": "latest",
@@ -61,7 +61,7 @@
6161
"browser-resolve": "^1.11.2",
6262
"browserify": "latest",
6363
"chai": "latest",
64-
"chalk": "latest",
64+
"chalk": "4.1.1",
6565
"convert-source-map": "latest",
6666
"del": "5.1.0",
6767
"diff": "^4.0.2",
@@ -94,7 +94,7 @@
9494
"remove-internal": "^2.9.2",
9595
"source-map-support": "latest",
9696
"through2": "latest",
97-
"typescript": "^4.2.3",
97+
"typescript": "4.3.5",
9898
"vinyl": "latest",
9999
"vinyl-sourcemaps-apply": "latest",
100100
"xml2js": "^0.4.19"

src/compiler/commandLineParser.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,13 @@ namespace ts {
750750
category: Diagnostics.Module_Resolution_Options,
751751
description: Diagnostics.List_of_folders_to_include_type_definitions_from
752752
},
753+
{
754+
name: "resolveFromOutDir",
755+
type: "boolean",
756+
affectsModuleResolution: true,
757+
category: Diagnostics.Module_Resolution_Options,
758+
description: Diagnostics.Allow_resolving_files_relative_to_the_output_directory
759+
},
753760
{
754761
name: "types",
755762
type: "list",

src/compiler/diagnosticMessages.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4331,6 +4331,10 @@
43314331
"category": "Message",
43324332
"code": 6107
43334333
},
4334+
"'resolveFromOutDir' option is set, using it to resolve relative module name '{0}'.": {
4335+
"category": "Message",
4336+
"code": 16107
4337+
},
43344338
"Longest matching prefix for '{0}' is '{1}'.": {
43354339
"category": "Message",
43364340
"code": 6108
@@ -4339,6 +4343,10 @@
43394343
"category": "Message",
43404344
"code": 6109
43414345
},
4346+
"Loading '{0}' from the out dir '{1}', candidate location '{2}'.": {
4347+
"category": "Message",
4348+
"code": 16109
4349+
},
43424350
"Trying other entries in 'rootDirs'.": {
43434351
"category": "Message",
43444352
"code": 6110
@@ -4347,6 +4355,10 @@
43474355
"category": "Message",
43484356
"code": 6111
43494357
},
4358+
"Module resolution using 'outDir' has failed.": {
4359+
"category": "Message",
4360+
"code": 16111
4361+
},
43504362
"Do not emit 'use strict' directives in module output.": {
43514363
"category": "Message",
43524364
"code": 6112
@@ -5086,6 +5098,10 @@
50865098
"category": "Message",
50875099
"code": 6505
50885100
},
5101+
"Allow resolving files relative to the output directory.": {
5102+
"category": "Message",
5103+
"code": 6719
5104+
},
50895105

50905106
"Include 'undefined' in index signature results": {
50915107
"category": "Message",

src/compiler/moduleNameResolver.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,12 +878,13 @@ namespace ts {
878878
type ResolutionKindSpecificLoader = (extensions: Extensions, candidate: string, onlyRecordFailures: boolean, state: ModuleResolutionState) => Resolved | undefined;
879879

880880
/**
881-
* Any module resolution kind can be augmented with optional settings: 'baseUrl', 'paths' and 'rootDirs' - they are used to
881+
* Any module resolution kind can be augmented with optional settings: 'baseUrl', 'resolveFromOutDir', 'paths' and 'rootDirs' - they are used to
882882
* mitigate differences between design time structure of the project and its runtime counterpart so the same import name
883883
* can be resolved successfully by TypeScript compiler and runtime module loader.
884884
* If these settings are set then loading procedure will try to use them to resolve module name and it can of failure it will
885885
* fallback to standard resolution routine.
886886
*
887+
* 'resolveFromOutDir': TODO document the semantics
887888
* - baseUrl - this setting controls how non-relative module names are resolved. If this setting is specified then non-relative
888889
* names will be resolved relative to baseUrl: i.e. if baseUrl is '/a/b' then candidate location to resolve module name 'c/d' will
889890
* be '/a/b/c/d'
@@ -940,6 +941,9 @@ namespace ts {
940941
function tryLoadModuleUsingOptionalResolutionSettings(extensions: Extensions, moduleName: string, containingDirectory: string, loader: ResolutionKindSpecificLoader,
941942
state: ModuleResolutionState): Resolved | undefined {
942943

944+
const resolvedFromOutDir = tryLoadModuleUsingOutDirIfEligible(extensions, moduleName, containingDirectory, loader, state);
945+
if (resolvedFromOutDir) return resolvedFromOutDir;
946+
943947
const resolved = tryLoadModuleUsingPathsIfEligible(extensions, moduleName, loader, state);
944948
if (resolved) return resolved.value;
945949

@@ -965,6 +969,51 @@ namespace ts {
965969
}
966970
}
967971

972+
function tryLoadModuleUsingOutDirIfEligible(extensions: Extensions, moduleName: string, containingDirectory: string, loader: ResolutionKindSpecificLoader, state: ModuleResolutionState): Resolved | undefined {
973+
const { baseUrl, resolveFromOutDir, outDir, rootDir } = state.compilerOptions;
974+
if (!resolveFromOutDir) {
975+
return undefined;
976+
}
977+
if (!outDir) {
978+
return undefined;
979+
}
980+
if (state.traceEnabled) {
981+
trace(state.host, Diagnostics.resolveFromOutDir_option_is_set_using_it_to_resolve_relative_module_name_0, moduleName);
982+
}
983+
984+
// COMMENT FOR REVIEWER: Is there a more robust way to determine the base directory here?
985+
var baseDirectory = baseUrl;
986+
if (!baseDirectory && state.host.getCurrentDirectory) {
987+
baseDirectory = state.host.getCurrentDirectory();
988+
}
989+
if (!baseDirectory) {
990+
return undefined;
991+
}
992+
993+
// COMMENT FOR REVIEWER: I've seen rootDir be relative path and and absolute path so
994+
// handling both cases here to come up with an absolute normalizedPrefix path
995+
var normalizedPrefix = rootDir && ts.startsWith(rootDir, ts.directorySeparator) ?
996+
ts.normalizePath(rootDir) :
997+
ts.normalizePath(ts.combinePaths(baseDirectory, rootDir));
998+
var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName));
999+
1000+
// COMMENT FOR REVIEWER: No ts.relativePath() function that I could find. Is there one
1001+
// somewhere that I'm not aware of?
1002+
var suffix = require("path").relative(normalizedPrefix, candidate);
1003+
candidate = ts.normalizePath(ts.combinePaths(baseDirectory, outDir, suffix))
1004+
if (state.traceEnabled) {
1005+
trace(state.host, Diagnostics.Loading_0_from_the_out_dir_1_candidate_location_2, suffix, outDir, candidate);
1006+
}
1007+
const resolvedFileName = loader(extensions, candidate, !directoryProbablyExists(containingDirectory, state.host), state);
1008+
if (resolvedFileName) {
1009+
return resolvedFileName;
1010+
}
1011+
if (state.traceEnabled) {
1012+
trace(state.host, Diagnostics.Module_resolution_using_outDir_has_failed);
1013+
}
1014+
return undefined;
1015+
}
1016+
9681017
function tryLoadModuleUsingRootDirs(extensions: Extensions, moduleName: string, containingDirectory: string, loader: ResolutionKindSpecificLoader,
9691018
state: ModuleResolutionState): Resolved | undefined {
9701019

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5982,6 +5982,7 @@ namespace ts {
59825982
project?: string;
59835983
/* @internal */ pretty?: boolean;
59845984
reactNamespace?: string;
5985+
resolveFromOutDir?: boolean;
59855986
jsxFactory?: string;
59865987
jsxFragmentFactory?: string;
59875988
jsxImportSource?: string;

0 commit comments

Comments
 (0)