Skip to content

Commit cdafb71

Browse files
Orta Theroxweswigham
andauthored
Replaces the default module index resolver with '/index' instead of '' when handling internal routing for dts bundles (#39277)
* Adds support for declaring the bundled name of a dts module export Co-authored-by: Wesley Wigham <[email protected]> * Adds baselines * Update the tests * Try to reduce the scope of the bundledPackageName error * Use the flag in more baselines * Get it green * More tests * Handle more feedback * More test cleanup * Set the moduleResolution for the tsconfigs Co-authored-by: Wesley Wigham <[email protected]>
1 parent bbf2133 commit cdafb71

File tree

142 files changed

+1175
-169
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+1175
-169
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4299,6 +4299,7 @@ namespace ts {
42994299
getProjectReferenceRedirect: fileName => host.getProjectReferenceRedirect(fileName),
43004300
isSourceOfProjectReferenceRedirect: fileName => host.isSourceOfProjectReferenceRedirect(fileName),
43014301
fileExists: fileName => host.fileExists(fileName),
4302+
getCompilerOptions: () => host.getCompilerOptions()
43024303
} : undefined },
43034304
encounteredError: false,
43044305
visitedTypes: undefined,
@@ -5933,7 +5934,8 @@ namespace ts {
59335934
const resolverHost = {
59345935
getCanonicalFileName,
59355936
getCurrentDirectory: () => context.tracker.moduleResolverHost!.getCurrentDirectory(),
5936-
getCommonSourceDirectory: () => context.tracker.moduleResolverHost!.getCommonSourceDirectory()
5937+
getCommonSourceDirectory: () => context.tracker.moduleResolverHost!.getCommonSourceDirectory(),
5938+
getCompilerOptions: () => context.tracker.moduleResolverHost!.getCompilerOptions()
59375939
};
59385940
const newName = getResolvedExternalModuleName(resolverHost, targetFile);
59395941
return factory.createStringLiteral(newName);

src/compiler/commandLineParser.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,14 @@ namespace ts {
10021002
category: Diagnostics.Advanced_Options,
10031003
description: Diagnostics.Emit_class_fields_with_Define_instead_of_Set,
10041004
},
1005+
{
1006+
name: "bundledPackageName",
1007+
type: "string",
1008+
affectsEmit: true,
1009+
category: Diagnostics.Advanced_Options,
1010+
description: Diagnostics.Provides_a_root_package_name_when_using_outFile_with_declarations,
1011+
},
1012+
10051013
{
10061014
name: "keyofStringsOnly",
10071015
type: "boolean",

src/compiler/diagnosticMessages.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,14 @@
11841184
"category": "Error",
11851185
"code": 1389
11861186
},
1187-
1187+
"Provides a root package name when using outFile with declarations.": {
1188+
"category": "Message",
1189+
"code": 1390
1190+
},
1191+
"The `bundledPackageName` option must be provided when using outFile and node module resolution with declaration emit.": {
1192+
"category": "Error",
1193+
"code": 1391
1194+
},
11881195
"The types of '{0}' are incompatible between these types.": {
11891196
"category": "Error",
11901197
"code": 2200

src/compiler/moduleSpecifiers.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ namespace ts.moduleSpecifiers {
157157
}
158158

159159
function getLocalModuleSpecifier(moduleFileName: string, { getCanonicalFileName, sourceDirectory }: Info, compilerOptions: CompilerOptions, { ending, relativePreference }: Preferences): string {
160-
const { baseUrl, paths, rootDirs } = compilerOptions;
160+
const { baseUrl, paths, rootDirs, bundledPackageName } = compilerOptions;
161161

162162
const relativePath = rootDirs && tryGetModuleNameFromRootDirs(rootDirs, moduleFileName, sourceDirectory, getCanonicalFileName, ending, compilerOptions) ||
163163
removeExtensionAndIndexPostFix(ensurePathIsNonModuleName(getRelativePathFromDirectory(sourceDirectory, moduleFileName, getCanonicalFileName)), ending, compilerOptions);
@@ -170,8 +170,9 @@ namespace ts.moduleSpecifiers {
170170
return relativePath;
171171
}
172172

173-
const importRelativeToBaseUrl = removeExtensionAndIndexPostFix(relativeToBaseUrl, ending, compilerOptions);
174-
const fromPaths = paths && tryGetModuleNameFromPaths(removeFileExtension(relativeToBaseUrl), importRelativeToBaseUrl, paths);
173+
const bundledPkgReference = bundledPackageName ? combinePaths(bundledPackageName, relativeToBaseUrl) : relativeToBaseUrl;
174+
const importRelativeToBaseUrl = removeExtensionAndIndexPostFix(bundledPkgReference, ending, compilerOptions);
175+
const fromPaths = paths && tryGetModuleNameFromPaths(removeFileExtension(bundledPkgReference), importRelativeToBaseUrl, paths);
175176
const nonRelative = fromPaths === undefined ? importRelativeToBaseUrl : fromPaths;
176177

177178
if (relativePreference === RelativePreference.NonRelative) {
@@ -278,7 +279,7 @@ namespace ts.moduleSpecifiers {
278279
const isInNodeModules = pathContainsNodeModules(path);
279280
allFileNames.set(path, { path: getCanonicalFileName(path), isRedirect, isInNodeModules });
280281
importedFileFromNodeModules = importedFileFromNodeModules || isInNodeModules;
281-
// dont return value, so we collect everything
282+
// don't return value, so we collect everything
282283
}
283284
);
284285

src/compiler/program.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3139,6 +3139,11 @@ namespace ts {
31393139
}
31403140
}
31413141

3142+
// Without a package name, for multiple files being bundled into a .d.ts file you basically get a file which doesn't wrk
3143+
if (outputFile && getEmitDeclarations(options) && getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeJs && !options.bundledPackageName) {
3144+
createDiagnosticForOptionName(Diagnostics.The_bundledPackageName_option_must_be_provided_when_using_outFile_and_node_module_resolution_with_declaration_emit, options.out ? "out" : "outFile");
3145+
}
3146+
31423147
if (options.resolveJsonModule) {
31433148
if (getEmitModuleResolutionKind(options) !== ModuleResolutionKind.NodeJs) {
31443149
createDiagnosticForOptionName(Diagnostics.Option_resolveJsonModule_cannot_be_specified_without_node_module_resolution_strategy, "resolveJsonModule");

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5723,6 +5723,7 @@ namespace ts {
57235723
/** An error if set - this should only go through the -b pipeline and not actually be observed */
57245724
/*@internal*/
57255725
build?: boolean;
5726+
bundledPackageName?: string;
57265727
charset?: string;
57275728
checkJs?: boolean;
57285729
/* @internal */ configFilePath?: string;
@@ -7843,6 +7844,7 @@ namespace ts {
78437844
readonly redirectTargetsMap: RedirectTargetsMap;
78447845
getProjectReferenceRedirect(fileName: string): string | undefined;
78457846
isSourceOfProjectReferenceRedirect(fileName: string): boolean;
7847+
getCompilerOptions(): CompilerOptions;
78467848
}
78477849

78487850
// Note: this used to be deprecated in our public API, but is still used internally

src/compiler/utilities.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4021,6 +4021,7 @@ namespace ts {
40214021
getCanonicalFileName(p: string): string;
40224022
getCommonSourceDirectory(): string;
40234023
getCurrentDirectory(): string;
4024+
getCompilerOptions(): CompilerOptions;
40244025
}
40254026

40264027
export function getResolvedExternalModuleName(host: ResolveModuleNameResolutionHost, file: SourceFile, referenceFile?: SourceFile): string {
@@ -4044,7 +4045,16 @@ namespace ts {
40444045
const filePath = getNormalizedAbsolutePath(fileName, host.getCurrentDirectory());
40454046
const relativePath = getRelativePathToDirectoryOrUrl(dir, filePath, dir, getCanonicalFileName, /*isAbsolutePathAnUrl*/ false);
40464047
const extensionless = removeFileExtension(relativePath);
4047-
return referencePath ? ensurePathIsNonModuleName(extensionless) : extensionless;
4048+
if (referencePath) {
4049+
return ensurePathIsNonModuleName(extensionless);
4050+
}
4051+
const options = host.getCompilerOptions();
4052+
const rootPkgName = options.bundledPackageName || "";
4053+
const newPath = combinePaths(rootPkgName, extensionless);
4054+
if (rootPkgName && getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeJs && endsWith(newPath, "/index")) {
4055+
return newPath.slice(0, newPath.length - "/index".length);
4056+
}
4057+
return newPath;
40484058
}
40494059

40504060
export function getOwnEmitOutputFilePath(fileName: string, host: EmitHost, extension: string) {

src/harness/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "../tsconfig-base",
33
"compilerOptions": {
44
"outFile": "../../built/local/harness.js",
5+
"moduleResolution": "node",
56
"types": [
67
"node", "mocha", "chai"
78
],

src/services/utilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,7 @@ namespace ts {
17731773
redirectTargetsMap: program.redirectTargetsMap,
17741774
getProjectReferenceRedirect: fileName => program.getProjectReferenceRedirect(fileName),
17751775
isSourceOfProjectReferenceRedirect: fileName => program.isSourceOfProjectReferenceRedirect(fileName),
1776+
getCompilerOptions: () => program.getCompilerOptions()
17761777
};
17771778
}
17781779

src/testRunner/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "../tsconfig-noncomposite-base",
33
"compilerOptions": {
44
"outFile": "../../built/local/run.js",
5+
"moduleResolution": "node",
56
"composite": false,
67
"declaration": false,
78
"declarationMap": false,

0 commit comments

Comments
 (0)