Skip to content

Commit 786b538

Browse files
committed
Update release
1 parent b2635de commit 786b538

File tree

14 files changed

+96
-45
lines changed

14 files changed

+96
-45
lines changed

release/compiler.d.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,38 @@ import * as ts from 'typescript';
22
import { File } from './input';
33
import { Host } from './host';
44
import { ProjectInfo } from './project';
5+
import { FinalTransformers } from './types';
56
export interface ICompiler {
6-
prepare(project: ProjectInfo): void;
7+
prepare(project: ProjectInfo, finalTransformers?: FinalTransformers): void;
78
inputFile(file: File): void;
89
inputDone(): void;
910
}
1011
/**
1112
* Compiles a whole project, with full type checking
1213
*/
1314
export declare class ProjectCompiler implements ICompiler {
15+
finalTransformers: FinalTransformers;
1416
host: Host;
1517
project: ProjectInfo;
1618
program: ts.Program;
1719
private hasSourceMap;
18-
prepare(project: ProjectInfo): void;
20+
prepare(project: ProjectInfo, finalTransformers?: FinalTransformers): void;
1921
inputFile(file: File): void;
2022
inputDone(): void;
2123
private attachContentToFile;
2224
private emit;
2325
private emitFile;
24-
private reportDiagnostics;
2526
private removeSourceMapComment;
2627
}
2728
export declare class FileCompiler implements ICompiler {
29+
finalTransformers: FinalTransformers;
2830
host: Host;
2931
project: ProjectInfo;
3032
private output;
3133
private previousOutput;
3234
private compilationResult;
33-
prepare(project: ProjectInfo): void;
35+
prepare(project: ProjectInfo, finalTransformers: FinalTransformers): void;
3436
private write;
3537
inputFile(file: File): void;
3638
inputDone(): void;
3739
}
38-
//# sourceMappingURL=compiler.d.ts.map

release/compiler.js

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3+
const ts = require("typescript");
34
const path = require("path");
45
const input_1 = require("./input");
56
const host_1 = require("./host");
@@ -9,7 +10,8 @@ const utils = require("./utils");
910
* Compiles a whole project, with full type checking
1011
*/
1112
class ProjectCompiler {
12-
prepare(project) {
13+
prepare(project, finalTransformers) {
14+
this.finalTransformers = finalTransformers;
1315
this.project = project;
1416
this.hasSourceMap = false;
1517
}
@@ -44,18 +46,25 @@ class ProjectCompiler {
4446
})
4547
: this.project.typescript.createProgram(rootFilenames, this.project.options, this.host, this.program);
4648
const result = reporter_1.emptyCompilationResult(this.project.options.noEmit);
47-
result.optionsErrors = this.reportDiagnostics(this.program.getOptionsDiagnostics());
48-
result.syntaxErrors = this.reportDiagnostics(this.program.getSyntacticDiagnostics());
49-
result.globalErrors = this.reportDiagnostics(this.program.getGlobalDiagnostics());
50-
result.semanticErrors = this.reportDiagnostics(this.program.getSemanticDiagnostics());
49+
const optionErrors = this.program.getOptionsDiagnostics();
50+
const syntaxErrors = this.program.getSyntacticDiagnostics();
51+
const globalErrors = this.program.getGlobalDiagnostics();
52+
const semanticErrors = this.program.getSemanticDiagnostics();
53+
result.optionsErrors = optionErrors.length;
54+
result.syntaxErrors = syntaxErrors.length;
55+
result.globalErrors = globalErrors.length;
56+
result.semanticErrors = semanticErrors.length;
57+
let declarationErrors = [];
5158
if (this.project.options.declaration) {
52-
result.declarationErrors = this.program.getDeclarationDiagnostics().length;
59+
declarationErrors = this.program.getDeclarationDiagnostics();
60+
result.declarationErrors = declarationErrors.length;
5361
}
62+
const preEmitDiagnostics = [...optionErrors, ...syntaxErrors, ...globalErrors, ...semanticErrors, ...declarationErrors];
5463
if (this.project.singleOutput) {
5564
const output = {
5665
file: undefined
5766
};
58-
this.emit(result, (fileName, content) => {
67+
this.emit(result, preEmitDiagnostics, (fileName, content) => {
5968
this.attachContentToFile(output, fileName, content);
6069
});
6170
this.emitFile(output, currentDirectory);
@@ -68,7 +77,7 @@ class ProjectCompiler {
6877
const file = this.project.input.getFile(fileName);
6978
output[fileName] = { file };
7079
}
71-
this.emit(result, (fileName, content, writeByteOrderMark, onError, sourceFiles) => {
80+
this.emit(result, preEmitDiagnostics, (fileName, content, writeByteOrderMark, onError, sourceFiles) => {
7281
if (sourceFiles.length !== 1) {
7382
throw new Error("Failure: sourceFiles in WriteFileCallback should have length 1, got " + sourceFiles.length);
7483
}
@@ -106,11 +115,19 @@ class ProjectCompiler {
106115
break;
107116
}
108117
}
109-
emit(result, callback) {
110-
const emitOutput = this.program.emit(undefined, callback);
111-
result.emitErrors += emitOutput.diagnostics.length;
112-
this.reportDiagnostics(emitOutput.diagnostics);
118+
emit(result, preEmitDiagnostics, callback) {
119+
const emitOutput = this.program.emit(undefined, callback, undefined, false, this.finalTransformers ? this.finalTransformers(this.program) : undefined);
113120
result.emitSkipped = emitOutput.emitSkipped;
121+
// `emitOutput.diagnostics` might contain diagnostics that were already part of `preEmitDiagnostics`.
122+
// See https://github.com/Microsoft/TypeScript/issues/20876
123+
// We use sortAndDeduplicateDiagnostics to remove duplicate diagnostics.
124+
// We then count the number of diagnostics in `diagnostics` that we not in `preEmitDiagnostics`
125+
// to count the number of emit diagnostics.
126+
const diagnostics = ts.sortAndDeduplicateDiagnostics([...preEmitDiagnostics, ...emitOutput.diagnostics]);
127+
result.emitErrors += diagnostics.length - preEmitDiagnostics.length;
128+
for (const error of diagnostics) {
129+
this.project.output.diagnostic(error);
130+
}
114131
}
115132
emitFile({ file, jsFileName, dtsFileName, dtsMapFileName, jsContent, dtsContent, dtsMapContent, jsMapContent }, currentDirectory) {
116133
if (!jsFileName)
@@ -148,15 +165,12 @@ class ProjectCompiler {
148165
this.project.output.writeJs(base, jsFileName, jsContent, jsMapContent, file ? file.gulp.cwd : currentDirectory, file);
149166
}
150167
if (dtsContent !== undefined) {
168+
if (dtsMapContent !== undefined) {
169+
dtsContent = this.removeSourceMapComment(dtsContent);
170+
}
151171
this.project.output.writeDts(baseDeclarations, dtsFileName, dtsContent, dtsMapContent, file ? file.gulp.cwd : currentDirectory, file);
152172
}
153173
}
154-
reportDiagnostics(diagnostics) {
155-
for (const error of diagnostics) {
156-
this.project.output.diagnostic(error);
157-
}
158-
return diagnostics.length;
159-
}
160174
removeSourceMapComment(content) {
161175
// By default the TypeScript automaticly inserts a source map comment.
162176
// This should be removed because gulp-sourcemaps takes care of that.
@@ -173,7 +187,8 @@ class FileCompiler {
173187
this.previousOutput = {};
174188
this.compilationResult = undefined;
175189
}
176-
prepare(project) {
190+
prepare(project, finalTransformers) {
191+
this.finalTransformers = finalTransformers;
177192
this.project = project;
178193
this.project.input.noParse = true;
179194
this.compilationResult = reporter_1.emptyCompilationResult(this.project.options.noEmit);
@@ -196,8 +211,13 @@ class FileCompiler {
196211
this.write(file, old.fileName, old.diagnostics, old.content, old.sourceMap);
197212
return;
198213
}
199-
const diagnostics = [];
200-
const outputString = this.project.typescript.transpile(file.content, this.project.options, file.fileNameOriginal, diagnostics);
214+
const output = this.project.typescript.transpileModule(file.content, {
215+
compilerOptions: this.project.options,
216+
fileName: file.fileNameOriginal,
217+
reportDiagnostics: true,
218+
transformers: this.finalTransformers ? this.finalTransformers() : undefined,
219+
});
220+
const outputString = output.outputText;
201221
let index = outputString.lastIndexOf('\n');
202222
let mapString = outputString.substring(index + 1);
203223
if (mapString.substring(0, 1) === '\r')
@@ -214,7 +234,7 @@ class FileCompiler {
214234
// map.sources[0] = path.relative(map.sourceRoot, file.gulp.path);
215235
const [fileNameExtensionless] = utils.splitExtension(file.fileNameOriginal);
216236
const [, extension] = utils.splitExtension(map.file); // js or jsx
217-
this.write(file, fileNameExtensionless + '.' + extension, diagnostics, outputString.substring(0, index), JSON.stringify(map));
237+
this.write(file, fileNameExtensionless + '.' + extension, output.diagnostics, outputString.substring(0, index), JSON.stringify(map));
218238
}
219239
inputDone() {
220240
this.project.output.finish(this.compilationResult);

release/host.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ export declare class Host implements ts.CompilerHost {
1919
realpath: (path: string) => string;
2020
getDirectories: (path: string) => string[];
2121
directoryExists: (path: string) => boolean;
22+
readDirectory: (rootDir: string, extensions: string[], excludes: string[], includes: string[], depth?: number) => string[];
2223
}
23-
//# sourceMappingURL=host.d.ts.map

release/host.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Host {
2929
this.realpath = (path) => this.fallback.realpath(path);
3030
this.getDirectories = (path) => this.fallback.getDirectories(path);
3131
this.directoryExists = (path) => this.fallback.directoryExists(path);
32+
this.readDirectory = (rootDir, extensions, excludes, includes, depth) => this.fallback.readDirectory(rootDir, extensions, excludes, includes, depth);
3233
this.typescript = typescript;
3334
this.fallback = typescript.createCompilerHost(options);
3435
this.currentDirectory = currentDirectory;

release/input.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as ts from 'typescript';
22
import * as utils from './utils';
3-
import * as VinylFile from 'vinyl';
3+
import { VinylFile } from './types';
44
export declare enum FileChangeState {
55
New = 0,
66
Equal = 1,
@@ -67,4 +67,3 @@ export declare class FileCache {
6767
commonSourceDirectory: string;
6868
isChanged(onlyGulp?: boolean): boolean;
6969
}
70-
//# sourceMappingURL=input.d.ts.map

release/main.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as ts from 'typescript';
22
import * as _project from './project';
33
import * as _reporter from './reporter';
4+
import { GetCustomTransformers } from './types';
45
declare function compile(proj: _project.Project, theReporter?: _reporter.Reporter): compile.CompileStream;
56
declare function compile(settings: compile.Settings, theReporter?: _reporter.Reporter): compile.CompileStream;
67
declare function compile(): compile.CompileStream;
@@ -30,6 +31,7 @@ declare module compile {
3031
declarationFiles?: boolean;
3132
noExternalResolve?: boolean;
3233
sortOutput?: boolean;
34+
getCustomTransformers?: GetCustomTransformers;
3335
typescript?: typeof ts;
3436
isolatedModules?: boolean;
3537
rootDir?: string;
@@ -47,4 +49,3 @@ declare module compile {
4749
function filter(...args: any[]): void;
4850
}
4951
export = compile;
50-
//# sourceMappingURL=main.d.ts.map

release/main.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ function compile(param, theReporter) {
3131
}
3232
return proj(theReporter);
3333
}
34+
function getFinalTransformers(getCustomTransformers) {
35+
if (typeof getCustomTransformers === 'function') {
36+
return getCustomTransformers;
37+
}
38+
if (typeof getCustomTransformers === 'string') {
39+
try {
40+
getCustomTransformers = require(getCustomTransformers);
41+
}
42+
catch (err) {
43+
throw new Error(`Failed to load customTransformers from "${getCustomTransformers}": ${err.message}`);
44+
}
45+
if (typeof getCustomTransformers !== 'function') {
46+
throw new Error(`Custom transformers in "${getCustomTransformers}" should export a function, got ${typeof getCustomTransformers}`);
47+
}
48+
return getCustomTransformers;
49+
}
50+
return null;
51+
}
3452
function getTypeScript(typescript) {
3553
if (typescript)
3654
return typescript;
@@ -43,7 +61,7 @@ function getTypeScript(typescript) {
4361
}
4462
}
4563
function checkAndNormalizeSettings(settings) {
46-
const { declarationFiles, noExternalResolve, sortOutput, typescript } = settings, standardSettings = __rest(settings, ["declarationFiles", "noExternalResolve", "sortOutput", "typescript"]);
64+
const { getCustomTransformers, declarationFiles, noExternalResolve, sortOutput, typescript } = settings, standardSettings = __rest(settings, ["getCustomTransformers", "declarationFiles", "noExternalResolve", "sortOutput", "typescript"]);
4765
if (settings.sourceRoot !== undefined) {
4866
console.warn('gulp-typescript: sourceRoot isn\'t supported any more. Use sourceRoot option of gulp-sourcemaps instead.');
4967
}
@@ -81,6 +99,7 @@ function reportErrors(errors, typescript, ignore = []) {
8199
(function (compile) {
82100
compile.reporter = _reporter;
83101
function createProject(fileNameOrSettings, settings) {
102+
let finalTransformers;
84103
let tsConfigFileName = undefined;
85104
let tsConfigContent = undefined;
86105
let projectDirectory = process.cwd();
@@ -100,6 +119,7 @@ function reportErrors(errors, typescript, ignore = []) {
100119
else {
101120
settings = fileNameOrSettings || {};
102121
}
122+
finalTransformers = getFinalTransformers(settings.getCustomTransformers);
103123
typescript = getTypeScript(settings.typescript);
104124
settings = checkAndNormalizeSettings(settings);
105125
const settingsResult = typescript.convertCompilerOptionsFromJson(settings, projectDirectory);
@@ -123,7 +143,7 @@ function reportErrors(errors, typescript, ignore = []) {
123143
}
124144
}
125145
normalizeCompilerOptions(compilerOptions, typescript);
126-
const project = _project.setupProject(projectDirectory, tsConfigFileName, rawConfig, tsConfigContent, compilerOptions, projectReferences, typescript);
146+
const project = _project.setupProject(projectDirectory, tsConfigFileName, rawConfig, tsConfigContent, compilerOptions, projectReferences, typescript, finalTransformers);
127147
return project;
128148
}
129149
compile.createProject = createProject;

release/output.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,3 @@ export declare class Output {
2424
diagnostic(info: ts.Diagnostic): void;
2525
error(error: reporter.TypeScriptError): void;
2626
}
27-
//# sourceMappingURL=output.d.ts.map

release/project.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Reporter } from './reporter';
55
import { FileCache } from './input';
66
import { Output } from './output';
77
import { ICompiler } from './compiler';
8-
import { TsConfig } from './types';
8+
import { FinalTransformers, TsConfig } from './types';
99
export interface Project {
1010
(reporter?: Reporter): ICompileStream;
1111
src(this: Project): NodeJS.ReadWriteStream;
@@ -28,9 +28,8 @@ export interface ProjectInfo {
2828
directory: string;
2929
reporter: Reporter;
3030
}
31-
export declare function setupProject(projectDirectory: string, configFileName: string, rawConfig: any, config: TsConfig, options: ts.CompilerOptions, projectReferences: ReadonlyArray<ts.ProjectReference>, typescript: typeof ts): Project;
31+
export declare function setupProject(projectDirectory: string, configFileName: string, rawConfig: any, config: TsConfig, options: ts.CompilerOptions, projectReferences: ReadonlyArray<ts.ProjectReference>, typescript: typeof ts, finalTransformers: FinalTransformers): Project;
3232
export interface ICompileStream extends NodeJS.ReadWriteStream {
3333
js: stream.Readable;
3434
dts: stream.Readable;
3535
}
36-
//# sourceMappingURL=project.d.ts.map

release/project.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const reporter_1 = require("./reporter");
1818
const input_1 = require("./input");
1919
const output_1 = require("./output");
2020
const compiler_1 = require("./compiler");
21-
function setupProject(projectDirectory, configFileName, rawConfig, config, options, projectReferences, typescript) {
21+
function setupProject(projectDirectory, configFileName, rawConfig, config, options, projectReferences, typescript, finalTransformers) {
2222
const input = new input_1.FileCache(typescript, options);
2323
const compiler = options.isolatedModules ? new compiler_1.FileCompiler() : new compiler_1.ProjectCompiler();
2424
let running = false;
@@ -34,7 +34,7 @@ function setupProject(projectDirectory, configFileName, rawConfig, config, optio
3434
}
3535
running = true;
3636
input.reset();
37-
compiler.prepare(projectInfo);
37+
compiler.prepare(projectInfo, finalTransformers);
3838
const stream = new CompileStream(projectInfo);
3939
projectInfo.output = new output_1.Output(projectInfo, stream, stream.js, stream.dts);
4040
projectInfo.reporter = reporter || reporter_1.defaultReporter();

release/reporter.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as ts from 'typescript';
2-
import * as VinylFile from 'vinyl';
2+
import { VinylFile } from './types';
33
export interface TypeScriptError extends Error {
44
fullFilename?: string;
55
relativeFilename?: string;
@@ -41,4 +41,3 @@ export declare function nullReporter(): Reporter;
4141
export declare function defaultReporter(): Reporter;
4242
export declare function longReporter(): Reporter;
4343
export declare function fullReporter(fullFilename?: boolean): Reporter;
44-
//# sourceMappingURL=reporter.d.ts.map

release/reporter.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ function defaultReporter() {
6363
}
6464
exports.defaultReporter = defaultReporter;
6565
function longReporter() {
66-
const typescript = require('typescript');
6766
return {
68-
error: (error) => {
67+
error: (error, typescript) => {
6968
if (error.tsFile) {
7069
console.log('[' + colors.gray('gulp-typescript') + '] ' + colors.red(error.fullFilename
7170
+ '(' + error.startPosition.line + ',' + error.startPosition.character + '): ')

release/types.d.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1+
/// <reference types="node" />
2+
import * as ts from 'typescript';
3+
export declare type FinalTransformers = undefined | ((program?: ts.Program) => ts.CustomTransformers);
4+
export declare type GetCustomTransformers = string | ((program?: ts.Program) => ts.CustomTransformers | undefined);
15
export interface TsConfig {
26
files?: string[];
37
include?: string[];
48
exclude?: string[];
59
compilerOptions?: any;
610
}
7-
//# sourceMappingURL=types.d.ts.map
11+
export interface VinylFile {
12+
contents: Buffer | NodeJS.ReadableStream | null;
13+
cwd: string;
14+
base: string;
15+
path: string;
16+
dirname: string;
17+
basename: string;
18+
stem: string;
19+
extname: string;
20+
sourceMap?: any;
21+
}

release/utils.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@ export declare function getCommonBasePathOfArray(paths: string[]): string;
2121
export declare function getError(info: ts.Diagnostic, typescript: typeof ts, file?: File): reporter.TypeScriptError;
2222
export declare function deprecate(title: string, alternative: string, description?: string): void;
2323
export declare function message(title: string, alternative: string, description?: string): void;
24-
//# sourceMappingURL=utils.d.ts.map

0 commit comments

Comments
 (0)