Skip to content

Commit

Permalink
Merge pull request #579 from rbuckton/projectReferences
Browse files Browse the repository at this point in the history
Add support for TS3.0 projectReferences
  • Loading branch information
ivogabe authored Jun 18, 2018
2 parents 36ff1f5 + 80ffc3d commit 95e9c2b
Show file tree
Hide file tree
Showing 36 changed files with 1,049 additions and 880 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@
[submodule "typescript/2.9"]
path = typescript/2.9
url = https://github.com/Microsoft/TypeScript
[submodule "typescript/2.7"]
path = typescript/2.7
url = https://github.com/Microsoft/TypeScript.git
branch = release-2.7
8 changes: 6 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const diff = require('gulp-diff');
const tsVersions = {
dev: './typescript/dev',
release23: './typescript/2.3',
release27: './typescript/2.7',
release29: './typescript/2.9'
};

Expand Down Expand Up @@ -74,7 +75,7 @@ const typecheck = gulp.parallel(typecheckDev, typecheck2_3);

// We run every test on multiple typescript versions:
const libs = [
['2.7', undefined],
['2.7', require(tsVersions.release27)],
['2.3', require(tsVersions.release23)],
['2.9', require(tsVersions.release29)],
['dev', require(tsVersions.dev)]
Expand All @@ -97,8 +98,11 @@ async function runTest(name) {
const newGulpTs = require('./release-2/main');
const testTask = require(`./${path.posix.join(testDir, 'gulptask.js')}`);

const matchingLibs = testTask.match ? libs.filter(([, tsLib]) => testTask.match(tsLib)) : libs;
if (matchingLibs.length === 0) return Promise.resolve();

fs.mkdirSync(outputDir);
return Promise.all(libs.map(([tsVersion, tsLib]) => {
return Promise.all(matchingLibs.map(([tsVersion, tsLib]) => {
return new Promise((resolve, reject) => {
const errors = [];
let finishInfo;
Expand Down
12 changes: 11 additions & 1 deletion lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,17 @@ export class ProjectCompiler implements ICompiler {
this.project.options
);

this.program = this.project.typescript.createProgram(rootFilenames, this.project.options, this.host, this.program);
// 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({
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);

const result = emptyCompilationResult(this.project.options.noEmit);
result.optionsErrors = this.reportDiagnostics(this.program.getOptionsDiagnostics());
Expand Down
4 changes: 3 additions & 1 deletion lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ module compile {
let projectDirectory = process.cwd();
let typescript: typeof ts;
let compilerOptions: ts.CompilerOptions;
let projectReferences: ReadonlyArray<ts.ProjectReference>;
let fileName: string;

let rawConfig: any;
Expand Down Expand Up @@ -199,11 +200,12 @@ module compile {
}

compilerOptions = parsed.options;
projectReferences = parsed.projectReferences;
}
}

normalizeCompilerOptions(compilerOptions, typescript);
const project = _project.setupProject(projectDirectory, tsConfigFileName, rawConfig, tsConfigContent, compilerOptions, typescript);
const project = _project.setupProject(projectDirectory, tsConfigFileName, rawConfig, tsConfigContent, compilerOptions, projectReferences, typescript);

return project;
}
Expand Down
24 changes: 20 additions & 4 deletions lib/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export class Output {
private pendingIO = 0;

writeJs(base: string, fileName: string, content: string, sourceMapContent: string, cwd: string, original: input.File) {
this.pipeRejection(this.writeJsAsync(base, fileName, content, sourceMapContent, cwd, original), this.streamJs);
}

private async writeJsAsync(base: string, fileName: string, content: string, sourceMapContent: string, cwd: string, original: input.File) {
const file = new VinylFile({
path: fileName,
contents: Buffer.from(content),
Expand All @@ -38,7 +42,7 @@ export class Output {

this.pendingIO++;

this.applySourceMap(sourceMapContent, original, file).then(appliedSourceMap => {
await this.applySourceMap(sourceMapContent, original, file).then(appliedSourceMap => {
if (appliedSourceMap) file.sourceMap = JSON.parse(appliedSourceMap);
this.streamFull.push(file);
this.streamJs.push(file);
Expand All @@ -48,7 +52,11 @@ export class Output {
});
}

async writeDts(base: string, fileName: string, content: string, declarationMapContent: string, cwd: string, original: input.File) {
writeDts(base: string, fileName: string, content: string, declarationMapContent: string, cwd: string, original: input.File) {
this.pipeRejection(this.writeDtsAsync(base, fileName, content, declarationMapContent, cwd, original), this.streamDts);
}

private async writeDtsAsync(base: string, fileName: string, content: string, declarationMapContent: string, cwd: string, original: input.File) {
const file = new VinylFile({
path: fileName,
contents: Buffer.from(content),
Expand All @@ -58,7 +66,7 @@ export class Output {

this.pendingIO++;

this.applySourceMap(declarationMapContent, original, file).then(appliedSourceMap => {
await this.applySourceMap(declarationMapContent, original, file).then(appliedSourceMap => {
if (appliedSourceMap) file.sourceMap = JSON.parse(appliedSourceMap);
this.streamFull.push(file);
this.streamDts.push(file);
Expand Down Expand Up @@ -118,12 +126,20 @@ export class Output {
}
}

// Avoids UnhandledPromiseRejectionWarning in NodeJS
private pipeRejection<T>(promise: Promise<T>, alternateStream: stream.Readable) {
promise.catch(err => {
this.streamFull.emit("error", err);
alternateStream.emit("error", err);
});
}

finish(result: reporter.CompilationResult) {
this.result = result;

this.mightFinish();
}

private mightFinish() {
if (this.result === undefined || this.pendingIO !== 0) return;

Expand Down
7 changes: 6 additions & 1 deletion lib/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface PartialProject {
rawConfig?: any;
config?: TsConfig;
options?: ts.CompilerOptions;
projectReferences?: ReadonlyArray<ts.ProjectReference>;
}
export interface Project {
(reporter?: Reporter): ICompileStream;
Expand All @@ -34,6 +35,7 @@ export interface Project {
readonly rawConfig: any;
readonly config: TsConfig;
readonly options: ts.CompilerOptions;
readonly projectReferences: ReadonlyArray<ts.ProjectReference> | undefined;
}

export interface ProjectInfo {
Expand All @@ -42,12 +44,13 @@ export interface ProjectInfo {
compiler: ICompiler;
singleOutput: boolean;
options: ts.CompilerOptions;
projectReferences: ReadonlyArray<ts.ProjectReference>;
typescript: typeof ts;
directory: string;
reporter: Reporter;
}

export function setupProject(projectDirectory: string, configFileName: string, rawConfig: any, config: TsConfig, options: ts.CompilerOptions, typescript: typeof ts) {
export function setupProject(projectDirectory: string, configFileName: string, rawConfig: any, config: TsConfig, options: ts.CompilerOptions, projectReferences: ReadonlyArray<ts.ProjectReference>, typescript: typeof ts) {
const input = new FileCache(typescript, options);
const compiler: ICompiler = options.isolatedModules ? new FileCompiler() : new ProjectCompiler();
let running = false;
Expand Down Expand Up @@ -88,12 +91,14 @@ export function setupProject(projectDirectory: string, configFileName: string, r
project.rawConfig = rawConfig;
project.config = config;
project.options = options;
project.projectReferences = projectReferences;

const projectInfo: ProjectInfo = {
input,
singleOutput,
compiler,
options,
projectReferences,
typescript,
directory: projectDirectory,
// Set when `project` is called
Expand Down
Loading

0 comments on commit 95e9c2b

Please sign in to comment.