Skip to content

Enables passing --declaration, --emitDeclarationOnly, --declarationMap, --soureMap and --inlineSourceMap to tsc --build #51241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Copy js or dts bundle from old build if emitting only js or dts files
  • Loading branch information
sheetalkamat committed Nov 3, 2022
commit fdba2d3a727b1d259a016abfa8ab9a4a218f7f51
31 changes: 21 additions & 10 deletions src/compiler/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ namespace ts {
* Name of the file whose dts was the latest to change
*/
latestChangedDtsFile: string | undefined;
bundle?: BundleBuildInfo;
}

export const enum BuilderFileEmit {
Expand Down Expand Up @@ -312,9 +313,10 @@ namespace ts {
}
}
}
// If this program has prepend references, always emit since we wont know if files on disk are correct unless we check file hash for correctness
if (outFilePath && !state.changedFilesSet.size && some(newProgram.getProjectReferences(), ref => !!ref.prepend)) {
state.programEmitPending = getBuilderFileEmit(compilerOptions);
if (outFilePath && !state.changedFilesSet.size) {
if (useOldState) state.bundle = oldState!.bundle;
// If this program has prepend references, always emit since we wont know if files on disk are correct unless we check file hash for correctness
if (some(newProgram.getProjectReferences(), ref => !!ref.prepend)) state.programEmitPending = getBuilderFileEmit(compilerOptions);
}
return state;
}
Expand Down Expand Up @@ -914,7 +916,7 @@ namespace ts {
/**
* Gets the program information to be emitted in buildInfo so that we can use it to create new program
*/
function getProgramBuildInfo(state: BuilderProgramState, getCanonicalFileName: GetCanonicalFileName): ProgramBuildInfo | undefined {
function getBuildInfo(state: BuilderProgramState, getCanonicalFileName: GetCanonicalFileName, bundle: BundleBuildInfo | undefined): BuildInfo {
const currentDirectory = Debug.checkDefined(state.program).getCurrentDirectory();
const buildInfoDirectory = getDirectoryPath(getNormalizedAbsolutePath(getTsBuildInfoEmitOutputFilePath(state.compilerOptions)!, currentDirectory));
// Convert the file name to Path here if we set the fileName instead to optimize multiple d.ts file emits and having to compute Canonical path
Expand All @@ -929,7 +931,7 @@ namespace ts {
{ version: value.version, impliedFormat: value.impliedFormat, signature: undefined, affectsGlobalScope: undefined } :
value.version;
});
const result: ProgramBundleEmitBuildInfo = {
const program: ProgramBundleEmitBuildInfo = {
fileNames,
fileInfos,
options: convertToProgramBuildInfoCompilerOptions(state.compilerOptions),
Expand All @@ -942,7 +944,14 @@ namespace ts {
false :
state.programEmitPending,
};
return result;
const { js, dts, commonSourceDirectory, sourceFiles } = bundle!;
state.bundle = bundle = {
commonSourceDirectory,
sourceFiles,
js: js || (!state.compilerOptions.emitDeclarationOnly ? state.bundle?.js : undefined),
dts: dts || (getEmitDeclarations(state.compilerOptions) ? state.bundle?.dts : undefined),
};
return createBuildInfo(program, bundle);
}

let fileIdsList: (readonly ProgramBuildInfoFileId[])[] | undefined;
Expand Down Expand Up @@ -1042,7 +1051,7 @@ namespace ts {
}
}

const result: ProgramMultiFileEmitBuildInfo = {
const program: ProgramMultiFileEmitBuildInfo = {
fileNames,
fileInfos,
options: convertToProgramBuildInfoCompilerOptions(state.compilerOptions),
Expand All @@ -1056,7 +1065,7 @@ namespace ts {
emitSignatureDtsMapDiffers,
latestChangedDtsFile,
};
return result;
return createBuildInfo(program, bundle);

function relativeToBuildInfoEnsuringAbsolutePath(path: string) {
return relativeToBuildInfo(getNormalizedAbsolutePath(path, currentDirectory));
Expand Down Expand Up @@ -1252,7 +1261,7 @@ namespace ts {
*/
const computeHash = maybeBind(host, host.createHash);
const state = createBuilderProgramState(newProgram, getCanonicalFileName, oldState, host.disableUseFileVersionAsSignature);
newProgram.getProgramBuildInfo = () => getProgramBuildInfo(state, getCanonicalFileName);
newProgram.getBuildInfo = bundle => getBuildInfo(state, getCanonicalFileName, bundle);

// To ensure that we arent storing any references to old program or new program without state
newProgram = undefined!; // TODO: GH#18217
Expand Down Expand Up @@ -1587,7 +1596,8 @@ namespace ts {
return !value ? getBuilderFileEmit(options || {}) : value;
}

export function createBuilderProgramUsingProgramBuildInfo(program: ProgramBuildInfo, buildInfoPath: string, host: ReadBuildProgramHost): EmitAndSemanticDiagnosticsBuilderProgram {
export function createBuilderProgramUsingProgramBuildInfo(buildInfo: BuildInfo, buildInfoPath: string, host: ReadBuildProgramHost): EmitAndSemanticDiagnosticsBuilderProgram {
const program = buildInfo.program!;
const buildInfoDirectory = getDirectoryPath(getNormalizedAbsolutePath(buildInfoPath, host.getCurrentDirectory()));
const getCanonicalFileName = createGetCanonicalFileName(host.useCaseSensitiveFileNames());

Expand All @@ -1608,6 +1618,7 @@ namespace ts {
outSignature: program.outSignature,
outSignatureDtsMapDiffers: program.outSignatureDtsMapDiffers,
programEmitPending: program.pendingEmit === undefined ? undefined : toProgramEmitPending(program.pendingEmit, program.options),
bundle: buildInfo.bundle,
};
}
else {
Expand Down
44 changes: 24 additions & 20 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,11 @@ namespace ts {
function emitBuildInfo(bundle: BundleBuildInfo | undefined, buildInfoPath: string | undefined) {
// Write build information if applicable
if (!buildInfoPath || targetSourceFile || emitSkipped) return;
const program = host.getProgramBuildInfo();
if (host.isEmitBlocked(buildInfoPath)) {
emitSkipped = true;
return;
}
const version = ts.version; // Extracted into a const so the form is stable between namespace and module
const buildInfo: BuildInfo = { bundle, program, version };
const buildInfo = host.getBuildInfo(bundle) || createBuildInfo(/*program*/ undefined, bundle);
// Pass buildinfo as additional data to avoid having to reparse
writeFile(host, emitterDiagnostics, buildInfoPath, getBuildInfoText(buildInfo), /*writeByteOrderMark*/ false, /*sourceFiles*/ undefined, { buildInfo });
}
Expand Down Expand Up @@ -646,6 +644,12 @@ namespace ts {
}
}

/*@internal*/
export function createBuildInfo(program: ProgramBuildInfo | undefined, bundle: BundleBuildInfo | undefined): BuildInfo {
const version = ts.version; // Extracted into a const so the form is stable between namespace and module
return { bundle, program, version };
}

/*@internal*/
export function getBuildInfoText(buildInfo: BuildInfo) {
return JSON.stringify(buildInfo);
Expand Down Expand Up @@ -825,21 +829,7 @@ namespace ts {
if (sourceMapText === text) return;
break;
case buildInfoPath:
const newBuildInfo = data!.buildInfo!;
newBuildInfo.program = buildInfo!.program;
if (newBuildInfo.program && changedDtsText !== undefined && config.options.composite) {
// Update the output signature
(newBuildInfo.program as ProgramBundleEmitBuildInfo).outSignature = computeSignature(changedDtsText, createHash, changedDtsData);
}
// Update sourceFileInfo
const { js, dts, sourceFiles } = buildInfo!.bundle!;
newBuildInfo.bundle!.js!.sources = js!.sources;
if (dts) {
newBuildInfo.bundle!.dts!.sources = dts.sources;
}
newBuildInfo.bundle!.sourceFiles = sourceFiles;
outputFiles.push({ name, text: getBuildInfoText(newBuildInfo), writeByteOrderMark, data: { buildInfo: newBuildInfo } });
return;
break;
case declarationFilePath:
if (declarationText === text) return;
changedDtsText = text;
Expand All @@ -851,13 +841,27 @@ namespace ts {
default:
Debug.fail(`Unexpected path: ${name}`);
}
outputFiles.push({ name, text, writeByteOrderMark });
outputFiles.push({ name, text, writeByteOrderMark, data });
},
isEmitBlocked: returnFalse,
readFile: f => host.readFile(f),
fileExists: f => host.fileExists(f),
useCaseSensitiveFileNames: () => host.useCaseSensitiveFileNames(),
getProgramBuildInfo: returnUndefined,
getBuildInfo: bundle => {
const program = buildInfo!.program;
if (program && changedDtsText !== undefined && config.options.composite) {
// Update the output signature
(program as ProgramBundleEmitBuildInfo).outSignature = computeSignature(changedDtsText, createHash, changedDtsData);
}
// Update sourceFileInfo
const { js, dts, sourceFiles } = buildInfo!.bundle!;
bundle!.js!.sources = js!.sources;
if (dts) {
bundle!.dts!.sources = dts.sources;
}
bundle!.sourceFiles = sourceFiles;
return createBuildInfo(program, bundle);
},
getSourceFileFromReference: returnUndefined,
redirectTargetsMap: createMultiMap(),
getFileIncludeReasons: notImplemented,
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1948,7 +1948,7 @@ namespace ts {
return host.fileExists(f);
},
useCaseSensitiveFileNames: () => host.useCaseSensitiveFileNames(),
getProgramBuildInfo: () => program.getProgramBuildInfo && program.getProgramBuildInfo(),
getBuildInfo: bundle => program.getBuildInfo?.(bundle),
getSourceFileFromReference: (file, ref) => program.getSourceFileFromReference(file, ref),
redirectTargetsMap,
getFileIncludeReasons: program.getFileIncludeReasons,
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4469,7 +4469,7 @@ namespace ts {
/*@internal*/ forEachResolvedProjectReference<T>(cb: (resolvedProjectReference: ResolvedProjectReference) => T | undefined): T | undefined;
/*@internal*/ getResolvedProjectReferenceByPath(projectReferencePath: Path): ResolvedProjectReference | undefined;
/*@internal*/ isSourceOfProjectReferenceRedirect(fileName: string): boolean;
/*@internal*/ getProgramBuildInfo?(): ProgramBuildInfo | undefined;
/*@internal*/ getBuildInfo?(bundle: BundleBuildInfo | undefined): BuildInfo;
/*@internal*/ emitBuildInfo(writeFile?: WriteFileCallback, cancellationToken?: CancellationToken): EmitResult;
/**
* This implementation handles file exists to be true if file is source of project reference redirect when program is created using useSourceOfProjectReferenceRedirect
Expand Down Expand Up @@ -7533,7 +7533,7 @@ namespace ts {
getPrependNodes(): readonly (InputFiles | UnparsedSource)[];

writeFile: WriteFileCallback;
getProgramBuildInfo(): ProgramBuildInfo | undefined;
getBuildInfo(bundle: BundleBuildInfo | undefined): BuildInfo | undefined;
getSourceFileFromReference: Program["getSourceFileFromReference"];
readonly redirectTargetsMap: RedirectTargetsMap;
createHash?(data: string): string;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/watchPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace ts {
buildInfo = getBuildInfo(buildInfoPath, content);
}
if (!buildInfo || buildInfo.version !== version || !buildInfo.program) return undefined;
return createBuilderProgramUsingProgramBuildInfo(buildInfo.program, buildInfoPath, host);
return createBuilderProgramUsingProgramBuildInfo(buildInfo, buildInfoPath, host);
}

export function createIncrementalCompilerHost(options: CompilerOptions, system = sys): CompilerHost {
Expand Down
Loading