Skip to content
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

Incremental compilation using new API #635

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 13 additions & 16 deletions lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class ProjectCompiler implements ICompiler {
finalTransformers: FinalTransformers;
host: Host;
project: ProjectInfo;
program: ts.Program;
program: ts.BuilderProgram;
private hasSourceMap: boolean;

prepare(project: ProjectInfo, finalTransformers?: FinalTransformers) {
Expand Down Expand Up @@ -68,24 +68,21 @@ export class ProjectCompiler implements ICompiler {
rootFilenames.map(fileName => this.project.input.getFile(fileName).gulp.cwd)
);

this.host = new Host(
this.project.typescript,
currentDirectory,
this.project.input,
this.project.options
);

// Calling `createProgram` with an object is only supported in 3.0. Only call this overload
// if we have project references (also only supported in 3.0)
this.program = this.project.projectReferences
? this.project.typescript.createProgram({
if (this.program === undefined) {
this.host = new Host(
this.project.typescript,
currentDirectory,
this.project.input,
this.project.options
);
this.program = this.project.typescript.createIncrementalProgram({
rootNames: rootFilenames,
options: this.project.options,
projectReferences: this.project.projectReferences,
host: this.host,
oldProgram: this.program
})
: this.project.typescript.createProgram(rootFilenames, this.project.options, this.host, this.program);
createProgram: this.project.typescript.createAbstractBuilder
});
}

const result = emptyCompilationResult(this.project.options.noEmit);

Expand Down Expand Up @@ -178,7 +175,7 @@ export class ProjectCompiler implements ICompiler {
callback,
undefined,
false,
this.finalTransformers ? this.finalTransformers(this.program) : undefined,
this.finalTransformers ? this.finalTransformers(this.program.getProgram()) : undefined,
);
result.emitSkipped = emitOutput.emitSkipped;

Expand Down
9 changes: 7 additions & 2 deletions lib/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ export class Host implements ts.CompilerHost {
let sourceFile = this.input.getFile(fileName);
if (sourceFile) return sourceFile.ts;

return this.fallback.getSourceFile(fileName, languageVersion, onError);
const file = this.fallback.getSourceFile(fileName, languageVersion, onError);
if (file === undefined) return undefined;
(file as any).version = this.input.versionString;
return file;
}

realpath = (path: string) => this.fallback.realpath(path);
Expand All @@ -91,5 +94,7 @@ export class Host implements ts.CompilerHost {
directoryExists = (path: string) => this.fallback.directoryExists(path);

readDirectory = (rootDir: string, extensions: string[], excludes: string[], includes: string[], depth?: number) =>
this.fallback.readDirectory(rootDir, extensions, excludes, includes, depth)
this.fallback.readDirectory(rootDir, extensions, excludes, includes, depth);

createHash = (data: string) => this.fallback.createHash(data);
}
7 changes: 5 additions & 2 deletions lib/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ export class FileCache {
current: FileDictionary;
options: ts.CompilerOptions;
caseSensitive: boolean;
noParse: boolean = false; // true when using a file based compiler.
noParse = false; // true when using a file based compiler.

typescript: typeof ts;
version: number = 0;
version = 0;
versionString = "0";

constructor(typescript: typeof ts, options: ts.CompilerOptions, caseSensitive: boolean) {
this.typescript = typescript;
Expand All @@ -177,6 +178,7 @@ export class FileCache {

reset() {
this.version++;
this.versionString = this.version.toString();
this.previous = this.current;
this.createDictionary();
}
Expand All @@ -196,6 +198,7 @@ export class FileCache {
}
}
file.ts = this.typescript.createSourceFile(file.fileNameOriginal, file.content, this.options.target);
(file.ts as any).version = this.versionString;
}

getFile(name: string) {
Expand Down
2 changes: 1 addition & 1 deletion release/compiler.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export declare class ProjectCompiler implements ICompiler {
finalTransformers: FinalTransformers;
host: Host;
project: ProjectInfo;
program: ts.Program;
program: ts.BuilderProgram;
private hasSourceMap;
prepare(project: ProjectInfo, finalTransformers?: FinalTransformers): void;
inputFile(file: File): void;
Expand Down
30 changes: 15 additions & 15 deletions release/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,16 @@ class ProjectCompiler {
}
this.project.options.sourceMap = this.hasSourceMap;
const currentDirectory = utils.getCommonBasePathOfArray(rootFilenames.map(fileName => this.project.input.getFile(fileName).gulp.cwd));
this.host = new host_1.Host(this.project.typescript, currentDirectory, this.project.input, this.project.options);
// Calling `createProgram` with an object is only supported in 3.0. Only call this overload
// if we have project references (also only supported in 3.0)
this.program = this.project.projectReferences
? this.project.typescript.createProgram({
if (this.program === undefined) {
this.host = new host_1.Host(this.project.typescript, currentDirectory, this.project.input, this.project.options);
this.program = this.project.typescript.createIncrementalProgram({
rootNames: rootFilenames,
options: this.project.options,
projectReferences: this.project.projectReferences,
host: this.host,
oldProgram: this.program
})
: this.project.typescript.createProgram(rootFilenames, this.project.options, this.host, this.program);
createProgram: this.project.typescript.createAbstractBuilder
});
}
const result = reporter_1.emptyCompilationResult(this.project.options.noEmit);
const optionErrors = this.program.getOptionsDiagnostics();
const syntaxErrors = this.program.getSyntacticDiagnostics();
Expand Down Expand Up @@ -73,22 +71,24 @@ class ProjectCompiler {
const output = {};
const input = this.host.input.getFileNames(true);
for (let i = 0; i < input.length; i++) {
const fileName = utils.normalizePath(input[i]);
const fileName = this.host.getCanonicalFileName(input[i]);
const file = this.project.input.getFile(fileName);
output[fileName] = { file };
}
this.emit(result, preEmitDiagnostics, (fileName, content, writeByteOrderMark, onError, sourceFiles) => {
if (sourceFiles === undefined)
return; // .tsbuildinfo file, ignore
if (sourceFiles.length !== 1) {
throw new Error("Failure: sourceFiles in WriteFileCallback should have length 1, got " + sourceFiles.length);
}
const fileNameOriginal = utils.normalizePath(sourceFiles[0].fileName);
const fileNameOriginal = this.host.getCanonicalFileName(sourceFiles[0].fileName);
const file = output[fileNameOriginal];
if (!file)
return;
this.attachContentToFile(file, fileName, content);
});
for (let i = 0; i < input.length; i++) {
const fileName = utils.normalizePath(input[i]);
const fileName = this.host.getCanonicalFileName(input[i]);
this.emitFile(output[fileName], currentDirectory);
}
}
Expand Down Expand Up @@ -116,7 +116,7 @@ class ProjectCompiler {
}
}
emit(result, preEmitDiagnostics, callback) {
const emitOutput = this.program.emit(undefined, callback, undefined, false, this.finalTransformers ? this.finalTransformers(this.program) : undefined);
const emitOutput = this.program.emit(undefined, callback, undefined, false, this.finalTransformers ? this.finalTransformers(this.program.getProgram()) : undefined);
result.emitSkipped = emitOutput.emitSkipped;
// `emitOutput.diagnostics` might contain diagnostics that were already part of `preEmitDiagnostics`.
// See https://github.com/Microsoft/TypeScript/issues/20876
Expand All @@ -130,8 +130,6 @@ class ProjectCompiler {
}
}
emitFile({ file, jsFileName, dtsFileName, dtsMapFileName, jsContent, dtsContent, dtsMapContent, jsMapContent }, currentDirectory) {
if (!jsFileName)
return;
let base;
let baseDeclarations;
if (file) {
Expand All @@ -153,7 +151,9 @@ class ProjectCompiler {
else {
base = this.project.directory;
baseDeclarations = base;
jsFileName = path.resolve(base, jsFileName);
if (jsFileName !== undefined) {
jsFileName = path.resolve(base, jsFileName);
}
if (dtsFileName !== undefined) {
dtsFileName = path.resolve(base, dtsFileName);
}
Expand Down
1 change: 1 addition & 0 deletions release/host.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export declare class Host implements ts.CompilerHost {
getDirectories: (path: string) => string[];
directoryExists: (path: string) => boolean;
readDirectory: (rootDir: string, extensions: string[], excludes: string[], includes: string[], depth?: number) => string[];
createHash: (data: string) => string;
}
11 changes: 8 additions & 3 deletions release/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ class Host {
let sourceFile = this.input.getFile(fileName);
if (sourceFile)
return sourceFile.ts;
return this.fallback.getSourceFile(fileName, languageVersion, onError);
const file = this.fallback.getSourceFile(fileName, languageVersion, onError);
if (file === undefined)
return undefined;
file.version = this.input.versionString;
return file;
};
this.realpath = (path) => this.fallback.realpath(path);
this.getDirectories = (path) => this.fallback.getDirectories(path);
this.directoryExists = (path) => this.fallback.directoryExists(path);
this.readDirectory = (rootDir, extensions, excludes, includes, depth) => this.fallback.readDirectory(rootDir, extensions, excludes, includes, depth);
this.createHash = (data) => this.fallback.createHash(data);
this.typescript = typescript;
this.fallback = typescript.createCompilerHost(options);
this.currentDirectory = currentDirectory;
Expand All @@ -39,10 +44,10 @@ class Host {
return '\n';
}
useCaseSensitiveFileNames() {
return false;
return this.fallback.useCaseSensitiveFileNames();
}
getCanonicalFileName(filename) {
return utils.normalizePath(filename);
return utils.normalizePath(this.useCaseSensitiveFileNames(), filename);
}
getDefaultLibFileName(options) {
return this.fallback.getDefaultLibFileName(options);
Expand Down
11 changes: 7 additions & 4 deletions release/input.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ export interface File {
ts?: ts.SourceFile;
}
export declare module File {
function fromContent(fileName: string, content: string): File;
function fromGulp(file: VinylFile): File;
function fromContent(caseSensitive: boolean, fileName: string, content: string): File;
function fromGulp(caseSensitive: boolean, file: VinylFile): File;
function equal(a: File, b: File): boolean;
function getChangeState(previous: File, current: File): FileChangeState;
}
export declare class FileDictionary {
files: utils.Map<File>;
firstSourceFile: File;
caseSensitive: boolean;
typescript: typeof ts;
constructor(typescript: typeof ts);
constructor(caseSensitive: boolean, typescript: typeof ts);
addGulp(gFile: VinylFile): File;
addContent(fileName: string, content: string): File;
private addFile;
Expand All @@ -50,10 +51,12 @@ export declare class FileCache {
previous: FileDictionary;
current: FileDictionary;
options: ts.CompilerOptions;
caseSensitive: boolean;
noParse: boolean;
typescript: typeof ts;
version: number;
constructor(typescript: typeof ts, options: ts.CompilerOptions);
versionString: string;
constructor(typescript: typeof ts, options: ts.CompilerOptions, caseSensitive: boolean);
addGulp(gFile: VinylFile): File;
addContent(fileName: string, content: string): File;
reset(): void;
Expand Down
29 changes: 17 additions & 12 deletions release/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ var FileKind;
})(FileKind = exports.FileKind || (exports.FileKind = {}));
var File;
(function (File) {
function fromContent(fileName, content) {
function fromContent(caseSensitive, fileName, content) {
let kind = FileKind.Source;
if (path.extname(fileName).toLowerCase() === 'json')
kind = FileKind.Config;
return {
fileNameNormalized: utils.normalizePath(fileName),
fileNameNormalized: utils.normalizePath(caseSensitive, fileName),
fileNameOriginal: fileName,
content,
kind
};
}
File.fromContent = fromContent;
function fromGulp(file) {
function fromGulp(caseSensitive, file) {
let str = file.contents.toString('utf8');
let data = fromContent(file.path, str);
let data = fromContent(caseSensitive, file.path, str);
data.gulp = file;
return data;
}
Expand All @@ -58,16 +58,17 @@ var File;
File.getChangeState = getChangeState;
})(File = exports.File || (exports.File = {}));
class FileDictionary {
constructor(typescript) {
constructor(caseSensitive, typescript) {
this.files = {};
this.firstSourceFile = undefined;
this.caseSensitive = caseSensitive;
this.typescript = typescript;
}
addGulp(gFile) {
return this.addFile(File.fromGulp(gFile));
return this.addFile(File.fromGulp(this.caseSensitive, gFile));
}
addContent(fileName, content) {
return this.addFile(File.fromContent(fileName, content));
return this.addFile(File.fromContent(this.caseSensitive, fileName, content));
}
addFile(file) {
if (file.kind === FileKind.Source) {
Expand All @@ -79,7 +80,7 @@ class FileDictionary {
return file;
}
getFile(name) {
return this.files[utils.normalizePath(name)];
return this.files[utils.normalizePath(this.caseSensitive, name)];
}
getFileNames(onlyGulp = false) {
const fileNames = [];
Expand Down Expand Up @@ -107,7 +108,7 @@ class FileDictionary {
get commonBasePath() {
const fileNames = this.getSourceFileNames(true);
return utils.getCommonBasePathOfArray(fileNames.map(fileName => {
const file = this.files[utils.normalizePath(fileName)];
const file = this.files[utils.normalizePath(this.caseSensitive, fileName)];
return path.resolve(process.cwd(), file.gulp.base);
}));
}
Expand All @@ -117,7 +118,7 @@ class FileDictionary {
get commonSourceDirectory() {
const fileNames = this.getSourceFileNames();
return utils.getCommonBasePathOfArray(fileNames.map(fileName => {
const file = this.files[utils.normalizePath(fileName)];
const file = this.files[utils.normalizePath(this.caseSensitive, fileName)];
return path.dirname(file.fileNameNormalized);
}));
}
Expand All @@ -127,12 +128,14 @@ class FileDictionary {
}
exports.FileDictionary = FileDictionary;
class FileCache {
constructor(typescript, options) {
constructor(typescript, options, caseSensitive) {
this.previous = undefined;
this.noParse = false; // true when using a file based compiler.
this.version = 0;
this.versionString = "0";
this.typescript = typescript;
this.options = options;
this.caseSensitive = caseSensitive;
this.createDictionary();
}
addGulp(gFile) {
Expand All @@ -143,11 +146,12 @@ class FileCache {
}
reset() {
this.version++;
this.versionString = this.version.toString();
this.previous = this.current;
this.createDictionary();
}
createDictionary() {
this.current = new FileDictionary(this.typescript);
this.current = new FileDictionary(this.caseSensitive, this.typescript);
this.current.initTypeScriptSourceFile = (file) => this.initTypeScriptSourceFile(file);
}
initTypeScriptSourceFile(file) {
Expand All @@ -161,6 +165,7 @@ class FileCache {
}
}
file.ts = this.typescript.createSourceFile(file.fileNameOriginal, file.content, this.options.target);
file.ts.version = this.versionString;
}
getFile(name) {
return this.current.getFile(name);
Expand Down
6 changes: 4 additions & 2 deletions release/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ var __rest = (this && this.__rest) || function (s, e) {
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
const path = require("path");
Expand Down
Loading