Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4299,6 +4299,7 @@ namespace ts {
getProjectReferenceRedirect: fileName => host.getProjectReferenceRedirect(fileName),
isSourceOfProjectReferenceRedirect: fileName => host.isSourceOfProjectReferenceRedirect(fileName),
fileExists: fileName => host.fileExists(fileName),
getCompilerOptions: () => host.getCompilerOptions()
} : undefined },
encounteredError: false,
visitedTypes: undefined,
Expand Down Expand Up @@ -5933,7 +5934,8 @@ namespace ts {
const resolverHost = {
getCanonicalFileName,
getCurrentDirectory: () => context.tracker.moduleResolverHost!.getCurrentDirectory(),
getCommonSourceDirectory: () => context.tracker.moduleResolverHost!.getCommonSourceDirectory()
getCommonSourceDirectory: () => context.tracker.moduleResolverHost!.getCommonSourceDirectory(),
getCompilerOptions: () => context.tracker.moduleResolverHost!.getCompilerOptions()
};
const newName = getResolvedExternalModuleName(resolverHost, targetFile);
return factory.createStringLiteral(newName);
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,14 @@ namespace ts {
category: Diagnostics.Advanced_Options,
description: Diagnostics.Emit_class_fields_with_Define_instead_of_Set,
},
{
name: "bundledPackageName",
type: "string",
affectsEmit: true,
category: Diagnostics.Advanced_Options,
description: Diagnostics.Provides_a_root_package_name_when_using_outFile_with_declarations,
},

{
name: "keyofStringsOnly",
type: "boolean",
Expand Down
9 changes: 8 additions & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,14 @@
"category": "Error",
"code": 1389
},

"Provides a root package name when using outFile with declarations.": {
"category": "Message",
"code": 1390
},
"The `bundledPackageName` option must be provided when using outFile and node module resolution with declaration emit.": {
"category": "Error",
"code": 1391
},
"The types of '{0}' are incompatible between these types.": {
"category": "Error",
"code": 2200
Expand Down
9 changes: 5 additions & 4 deletions src/compiler/moduleSpecifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ namespace ts.moduleSpecifiers {
}

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

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

const importRelativeToBaseUrl = removeExtensionAndIndexPostFix(relativeToBaseUrl, ending, compilerOptions);
const fromPaths = paths && tryGetModuleNameFromPaths(removeFileExtension(relativeToBaseUrl), importRelativeToBaseUrl, paths);
const bundledPkgReference = bundledPackageName ? combinePaths(bundledPackageName, relativeToBaseUrl) : relativeToBaseUrl;
const importRelativeToBaseUrl = removeExtensionAndIndexPostFix(bundledPkgReference, ending, compilerOptions);
const fromPaths = paths && tryGetModuleNameFromPaths(removeFileExtension(bundledPkgReference), importRelativeToBaseUrl, paths);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm late to the party here, but I don't think it's right to pass the bundled package reference to tryGetModuleNameFromPaths. These values get tested for equality with the values in the paths value arrays (with * substitutions applied), which are absolute paths. bundledPkgReference will presumably not be an absolute path, so this will prevent paths from ever being used to generate a module specifier in tandem with bundledPackageName. Or, maybe that's the intent? I can't totally wrap my mind around how/why these options would ever be used together.

const nonRelative = fromPaths === undefined ? importRelativeToBaseUrl : fromPaths;

if (relativePreference === RelativePreference.NonRelative) {
Expand Down Expand Up @@ -278,7 +279,7 @@ namespace ts.moduleSpecifiers {
const isInNodeModules = pathContainsNodeModules(path);
allFileNames.set(path, { path: getCanonicalFileName(path), isRedirect, isInNodeModules });
importedFileFromNodeModules = importedFileFromNodeModules || isInNodeModules;
// dont return value, so we collect everything
// don't return value, so we collect everything
}
);

Expand Down
5 changes: 5 additions & 0 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3139,6 +3139,11 @@ namespace ts {
}
}

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

if (options.resolveJsonModule) {
if (getEmitModuleResolutionKind(options) !== ModuleResolutionKind.NodeJs) {
createDiagnosticForOptionName(Diagnostics.Option_resolveJsonModule_cannot_be_specified_without_node_module_resolution_strategy, "resolveJsonModule");
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5723,6 +5723,7 @@ namespace ts {
/** An error if set - this should only go through the -b pipeline and not actually be observed */
/*@internal*/
build?: boolean;
bundledPackageName?: string;
charset?: string;
checkJs?: boolean;
/* @internal */ configFilePath?: string;
Expand Down Expand Up @@ -7843,6 +7844,7 @@ namespace ts {
readonly redirectTargetsMap: RedirectTargetsMap;
getProjectReferenceRedirect(fileName: string): string | undefined;
isSourceOfProjectReferenceRedirect(fileName: string): boolean;
getCompilerOptions(): CompilerOptions;
}

// Note: this used to be deprecated in our public API, but is still used internally
Expand Down
12 changes: 11 additions & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4021,6 +4021,7 @@ namespace ts {
getCanonicalFileName(p: string): string;
getCommonSourceDirectory(): string;
getCurrentDirectory(): string;
getCompilerOptions(): CompilerOptions;
}

export function getResolvedExternalModuleName(host: ResolveModuleNameResolutionHost, file: SourceFile, referenceFile?: SourceFile): string {
Expand All @@ -4044,7 +4045,16 @@ namespace ts {
const filePath = getNormalizedAbsolutePath(fileName, host.getCurrentDirectory());
const relativePath = getRelativePathToDirectoryOrUrl(dir, filePath, dir, getCanonicalFileName, /*isAbsolutePathAnUrl*/ false);
const extensionless = removeFileExtension(relativePath);
return referencePath ? ensurePathIsNonModuleName(extensionless) : extensionless;
if (referencePath) {
return ensurePathIsNonModuleName(extensionless);
}
const options = host.getCompilerOptions();
const rootPkgName = options.bundledPackageName || "";
const newPath = combinePaths(rootPkgName, extensionless);
if (rootPkgName && getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeJs && endsWith(newPath, "/index")) {
return newPath.slice(0, newPath.length - "/index".length);
}
return newPath;
}

export function getOwnEmitOutputFilePath(fileName: string, host: EmitHost, extension: string) {
Expand Down
1 change: 1 addition & 0 deletions src/harness/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "../tsconfig-base",
"compilerOptions": {
"outFile": "../../built/local/harness.js",
"moduleResolution": "node",
"types": [
"node", "mocha", "chai"
],
Expand Down
1 change: 1 addition & 0 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,7 @@ namespace ts {
redirectTargetsMap: program.redirectTargetsMap,
getProjectReferenceRedirect: fileName => program.getProjectReferenceRedirect(fileName),
isSourceOfProjectReferenceRedirect: fileName => program.isSourceOfProjectReferenceRedirect(fileName),
getCompilerOptions: () => program.getCompilerOptions()
};
}

Expand Down
1 change: 1 addition & 0 deletions src/testRunner/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "../tsconfig-noncomposite-base",
"compilerOptions": {
"outFile": "../../built/local/run.js",
"moduleResolution": "node",
"composite": false,
"declaration": false,
"declarationMap": false,
Expand Down
9 changes: 6 additions & 3 deletions src/testRunner/unittests/tsbuild/javascriptProjectEmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ namespace ts {
"extends": "../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"outFile": "common.js"
"outFile": "common.js",
"bundledPackageName": "common"
},
"include": ["nominal.js"]
}`,
Expand All @@ -119,7 +120,8 @@ namespace ts {
"extends": "../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"outFile": "sub-project.js"
"outFile": "sub-project.js",
"bundledPackageName": "sub"
},
"references": [
{ "path": "../common", "prepend": true }
Expand All @@ -143,7 +145,8 @@ namespace ts {
"extends": "../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"outFile": "sub-project-2.js"
"outFile": "sub-project-2.js",
"bundledPackageName": "sub-2"
},
"references": [
{ "path": "../sub-project", "prepend": true }
Expand Down
6 changes: 4 additions & 2 deletions src/testRunner/unittests/tsbuild/outFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,8 @@ ${internal} enum internalEnum { a, b, c }`);
declarationMap: true,
skipDefaultLibCheck: true,
sourceMap: true,
outFile: "./bin/first-output.js"
outFile: "./bin/first-output.js",
bundledPackageName: "first"
},
files: [sources[Project.first][Source.ts][Part.one]]
}));
Expand All @@ -669,7 +670,8 @@ ${internal} enum internalEnum { a, b, c }`);
declarationMap: false,
stripInternal: true,
sourceMap: true,
outFile: "./thirdjs/output/third-output.js"
outFile: "./thirdjs/output/third-output.js",
bundledPackageName: "third"
},
references: [{ path: "../first", prepend: true }],
files: [sources[Project.third][Source.ts][Part.one]]
Expand Down
4 changes: 2 additions & 2 deletions src/testRunner/unittests/tsbuild/watchMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,13 @@ export class someClass2 { }`),
const coreTsConfig: File = {
path: core[0].path,
content: JSON.stringify({
compilerOptions: { composite: true, declaration: true, outFile: "index.js" }
compilerOptions: { composite: true, declaration: true, outFile: "index.js", bundledPackageName: "core" }
})
};
const logicTsConfig: File = {
path: logic[0].path,
content: JSON.stringify({
compilerOptions: { composite: true, declaration: true, outFile: "index.js" },
compilerOptions: { composite: true, declaration: true, outFile: "index.js", bundledPackageName: "logic" },
references: [{ path: "../core", prepend: true }]
})
};
Expand Down
4 changes: 2 additions & 2 deletions src/testRunner/unittests/tsserver/projectReferenceErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ let x: string = 10;`
};
const dependencyConfig: File = {
path: `${dependecyLocation}/tsconfig.json`,
content: JSON.stringify({ compilerOptions: { composite: true, outFile: "../dependency.js" } })
content: JSON.stringify({ compilerOptions: { composite: true, outFile: "../dependency.js", bundledPackageName: "dep" } })
};
const usageTs: File = {
path: `${usageLocation}/usage.ts`,
Expand All @@ -205,7 +205,7 @@ fnErr();
const usageConfig: File = {
path: `${usageLocation}/tsconfig.json`,
content: JSON.stringify({
compilerOptions: { composite: true, outFile: "../usage.js" },
compilerOptions: { composite: true, outFile: "../usage.js", bundledPackageName: "usage" },
references: [{ path: "../dependency" }]
})
};
Expand Down
1 change: 1 addition & 0 deletions src/tsconfig-base.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"pretty": true,
"lib": ["es2015.iterable", "es2015.generator", "es5"],
"target": "es5",
"moduleResolution": "classic",
"rootDir": ".",

"declaration": true,
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2795,6 +2795,7 @@ declare namespace ts {
allowUnusedLabels?: boolean;
alwaysStrict?: boolean;
baseUrl?: string;
bundledPackageName?: string;
charset?: string;
checkJs?: boolean;
declaration?: boolean;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2795,6 +2795,7 @@ declare namespace ts {
allowUnusedLabels?: boolean;
alwaysStrict?: boolean;
baseUrl?: string;
bundledPackageName?: string;
charset?: string;
checkJs?: boolean;
declaration?: boolean;
Expand Down
30 changes: 30 additions & 0 deletions tests/baselines/reference/bundledDtsLateExportRenaming.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
error TS1391: The `bundledPackageName` option must be provided when using outFile and node module resolution with declaration emit.


!!! error TS1391: The `bundledPackageName` option must be provided when using outFile and node module resolution with declaration emit.
==== tests/cases/compiler/index.ts (0 errors) ====
export * from "./nested";

==== tests/cases/compiler/nested/base.ts (0 errors) ====
import { B } from "./shared";

export function f() {
return new B();
}

==== tests/cases/compiler/nested/derived.ts (0 errors) ====
import { f } from "./base";

export function g() {
return f();
}

==== tests/cases/compiler/nested/index.ts (0 errors) ====
export * from "./base";

export * from "./derived";
export * from "./shared";

==== tests/cases/compiler/nested/shared.ts (0 errors) ====
export class B {}

51 changes: 51 additions & 0 deletions tests/baselines/reference/bundledDtsLateExportRenaming.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//// [tests/cases/compiler/bundledDtsLateExportRenaming.ts] ////

//// [index.ts]
export * from "./nested";

//// [base.ts]
import { B } from "./shared";

export function f() {
return new B();
}

//// [derived.ts]
import { f } from "./base";

export function g() {
return f();
}

//// [index.ts]
export * from "./base";

export * from "./derived";
export * from "./shared";

//// [shared.ts]
export class B {}




//// [out.d.ts]
declare module "nested/shared" {
export class B {
}
}
declare module "nested/base" {
import { B } from "nested/shared";
export function f(): B;
}
declare module "nested/derived" {
export function g(): import("nested").B;
}
declare module "nested/index" {
export * from "nested/base";
export * from "nested/derived";
export * from "nested/shared";
}
declare module "index" {
export * from "nested/index";
}
35 changes: 35 additions & 0 deletions tests/baselines/reference/bundledDtsLateExportRenaming.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
=== tests/cases/compiler/index.ts ===
export * from "./nested";
No type information for this code.
No type information for this code.=== tests/cases/compiler/nested/base.ts ===
import { B } from "./shared";
>B : Symbol(B, Decl(base.ts, 0, 8))

export function f() {
>f : Symbol(f, Decl(base.ts, 0, 29))

return new B();
>B : Symbol(B, Decl(base.ts, 0, 8))
}

=== tests/cases/compiler/nested/derived.ts ===
import { f } from "./base";
>f : Symbol(f, Decl(derived.ts, 0, 8))

export function g() {
>g : Symbol(g, Decl(derived.ts, 0, 27))

return f();
>f : Symbol(f, Decl(derived.ts, 0, 8))
}

=== tests/cases/compiler/nested/index.ts ===
export * from "./base";
No type information for this code.
No type information for this code.export * from "./derived";
No type information for this code.export * from "./shared";
No type information for this code.
No type information for this code.=== tests/cases/compiler/nested/shared.ts ===
export class B {}
>B : Symbol(B, Decl(shared.ts, 0, 0))

Loading