Skip to content

Commit 62c687b

Browse files
committed
Store hash of text in the bundle info so it can be verified before manipulating text for fast updates during prepend
This helps when text changes during incremental build toggling and we determine we can just manipulate text
1 parent 5bccee8 commit 62c687b

File tree

55 files changed

+2039
-1204
lines changed

Some content is hidden

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

55 files changed

+2039
-1204
lines changed

Diff for: src/compiler/emitter.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -558,14 +558,17 @@ namespace ts {
558558
if (sourceMapFilePath) {
559559
const sourceMap = sourceMapGenerator.toString();
560560
writeFile(host, emitterDiagnostics, sourceMapFilePath, sourceMap, /*writeByteOrderMark*/ false, sourceFiles);
561+
if (printer.bundleFileInfo) printer.bundleFileInfo.mapHash = BuilderState.computeSignature(sourceMap, maybeBind(host, host.createHash));
561562
}
562563
}
563564
else {
564565
writer.writeLine();
565566
}
566567

567568
// Write the output file
568-
writeFile(host, emitterDiagnostics, jsFilePath, writer.getText(), !!compilerOptions.emitBOM, sourceFiles, { sourceMapUrlPos });
569+
const text = writer.getText();
570+
writeFile(host, emitterDiagnostics, jsFilePath, text, !!compilerOptions.emitBOM, sourceFiles, { sourceMapUrlPos });
571+
if (printer.bundleFileInfo) printer.bundleFileInfo.hash = BuilderState.computeSignature(text, maybeBind(host, host.createHash));
569572

570573
// Reset state
571574
writer.clear();
@@ -712,6 +715,7 @@ namespace ts {
712715
getNewLine(): string;
713716
createHash?(data: string): string;
714717
now?(): Date;
718+
getBuildInfo?(fileName: string, configFilePath: string | undefined): BuildInfo | undefined;
715719
}
716720

717721
function createSourceFilesFromBundleBuildInfo(bundle: BundleBuildInfo, buildInfoDirectory: string, host: EmitUsingBuildInfoHost): readonly SourceFile[] {
@@ -748,23 +752,38 @@ namespace ts {
748752
getCommandLine: (ref: ProjectReference) => ParsedCommandLine | undefined,
749753
customTransformers?: CustomTransformers
750754
): EmitUsingBuildInfoResult {
755+
const createHash = maybeBind(host, host.createHash);
751756
const { buildInfoPath, jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath } = getOutputPathsForBundle(config.options, /*forceDtsPaths*/ false);
752-
const buildInfoText = host.readFile(Debug.checkDefined(buildInfoPath));
753-
if (!buildInfoText) return buildInfoPath!;
757+
let buildInfo: BuildInfo;
758+
if (host.getBuildInfo) {
759+
const hostBuildInfo = host.getBuildInfo(buildInfoPath!, config.options.configFilePath);
760+
if (!hostBuildInfo) return buildInfoPath!;
761+
buildInfo = hostBuildInfo;
762+
}
763+
else {
764+
const buildInfoText = host.readFile(buildInfoPath!);
765+
if (!buildInfoText) return buildInfoPath!;
766+
buildInfo = getBuildInfo(buildInfoText);
767+
}
768+
if (!buildInfo.bundle || !buildInfo.bundle.js || (declarationFilePath && !buildInfo.bundle.dts)) return buildInfoPath!;
769+
754770
const jsFileText = host.readFile(Debug.checkDefined(jsFilePath));
755771
if (!jsFileText) return jsFilePath!;
772+
if (BuilderState.computeSignature(jsFileText, createHash) !== buildInfo.bundle.js.hash) return jsFilePath!;
756773
const sourceMapText = sourceMapFilePath && host.readFile(sourceMapFilePath);
757774
// error if no source map or for now if inline sourcemap
758775
if ((sourceMapFilePath && !sourceMapText) || config.options.inlineSourceMap) return sourceMapFilePath || "inline sourcemap decoding";
776+
if (sourceMapFilePath && BuilderState.computeSignature(sourceMapText!, createHash) !== buildInfo.bundle.js.mapHash) return sourceMapFilePath;
777+
759778
// read declaration text
760779
const declarationText = declarationFilePath && host.readFile(declarationFilePath);
761780
if (declarationFilePath && !declarationText) return declarationFilePath;
781+
if (declarationFilePath && BuilderState.computeSignature(declarationText!, createHash) !== buildInfo.bundle.dts!.hash) return declarationFilePath;
762782
const declarationMapText = declarationMapPath && host.readFile(declarationMapPath);
763783
// error if no source map or for now if inline sourcemap
764784
if ((declarationMapPath && !declarationMapText) || config.options.inlineSourceMap) return declarationMapPath || "inline sourcemap decoding";
785+
if (declarationMapPath && BuilderState.computeSignature(declarationMapText!, createHash) !== buildInfo.bundle.dts!.mapHash) return declarationMapPath;
765786

766-
const buildInfo = getBuildInfo(buildInfoText);
767-
if (!buildInfo.bundle || !buildInfo.bundle.js || (declarationText && !buildInfo.bundle.dts)) return buildInfoPath!;
768787
const buildInfoDirectory = getDirectoryPath(getNormalizedAbsolutePath(buildInfoPath!, host.getCurrentDirectory()));
769788
const ownPrependInput = createInputFiles(
770789
jsFileText,
@@ -812,7 +831,7 @@ namespace ts {
812831
newBuildInfo.program = buildInfo.program;
813832
if (newBuildInfo.program && changedDtsText !== undefined && config.options.composite) {
814833
// Update the output signature
815-
newBuildInfo.program.outSignature = computeSignature(changedDtsText, changedDtsData, maybeBind(host, host.createHash));
834+
newBuildInfo.program.outSignature = computeSignature(changedDtsText, changedDtsData, createHash);
816835
newBuildInfo.program.dtsChangeTime = getCurrentTime(host).getTime();
817836
}
818837
// Update sourceFileInfo
@@ -845,6 +864,7 @@ namespace ts {
845864
getSourceFileFromReference: returnUndefined,
846865
redirectTargetsMap: createMultiMap(),
847866
getFileIncludeReasons: notImplemented,
867+
createHash,
848868
};
849869
emitFiles(
850870
notImplementedResolver,

Diff for: src/compiler/program.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1867,6 +1867,7 @@ namespace ts {
18671867
getSourceFileFromReference: (file, ref) => program.getSourceFileFromReference(file, ref),
18681868
redirectTargetsMap,
18691869
getFileIncludeReasons: program.getFileIncludeReasons,
1870+
createHash: maybeBind(host, host.createHash),
18701871
};
18711872
}
18721873

Diff for: src/compiler/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -7095,6 +7095,7 @@ namespace ts {
70957095
getProgramBuildInfo(): ProgramBuildInfo | undefined;
70967096
getSourceFileFromReference: Program["getSourceFileFromReference"];
70977097
readonly redirectTargetsMap: RedirectTargetsMap;
7098+
createHash?(data: string): string;
70987099
}
70997100

71007101
/* @internal */
@@ -8242,6 +8243,8 @@ namespace ts {
82428243
/*@internal*/
82438244
export interface BundleFileInfo {
82448245
sections: BundleFileSection[];
8246+
hash?: string;
8247+
mapHash?: string;
82458248
sources?: SourceFileInfo;
82468249
}
82478250

Diff for: src/testRunner/unittests/tsbuild/helpers.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,22 @@ interface Symbol {
247247
};
248248
const version = buildInfo.version === ts.version ? fakes.version : buildInfo.version;
249249
const result: ReadableBuildInfo = {
250-
bundle: buildInfo.bundle,
250+
// Baseline fixed order for bundle
251+
bundle: buildInfo.bundle && {
252+
...buildInfo.bundle,
253+
js: buildInfo.bundle.js && {
254+
sections: buildInfo.bundle.js.sections,
255+
hash: buildInfo.bundle.js.hash,
256+
mapHash: buildInfo.bundle.js.mapHash,
257+
sources: buildInfo.bundle.js.sources,
258+
},
259+
dts: buildInfo.bundle.dts && {
260+
sections: buildInfo.bundle.dts.sections,
261+
hash: buildInfo.bundle.dts.hash,
262+
mapHash: buildInfo.bundle.dts.mapHash,
263+
sources: buildInfo.bundle.dts.sources,
264+
},
265+
},
251266
program,
252267
version,
253268
size: getBuildInfoText({ ...buildInfo, version }).length,

0 commit comments

Comments
 (0)