-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathoutput.ts
174 lines (140 loc) · 5.76 KB
/
output.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import * as stream from 'stream';
import * as path from 'path';
import * as ts from 'typescript';
import * as sourceMap from 'source-map';
import * as VinylFile from 'vinyl';
import * as utils from './utils';
import * as input from './input';
import * as reporter from './reporter';
import * as project from './project';
export class Output {
constructor(_project: project.ProjectInfo, streamFull: stream.Readable, streamJs: stream.Readable, streamDts: stream.Readable) {
this.project = _project;
this.streamFull = streamFull;
this.streamJs = streamJs;
this.streamDts = streamDts;
}
project: project.ProjectInfo;
result: reporter.CompilationResult;
// .js and .d.ts files
streamFull: stream.Readable;
// .js files
streamJs: stream.Readable;
// .d.ts files
streamDts: stream.Readable;
// Number of pending IO operatrions
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),
cwd,
base
});
this.pendingIO++;
await this.applySourceMap(sourceMapContent, original, file).then(appliedSourceMap => {
if (appliedSourceMap) file.sourceMap = JSON.parse(appliedSourceMap);
this.streamFull.push(file);
this.streamJs.push(file);
this.pendingIO--;
this.mightFinish();
});
}
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),
cwd,
base
});
this.pendingIO++;
await this.applySourceMap(declarationMapContent, original, file).then(appliedSourceMap => {
if (appliedSourceMap) file.sourceMap = JSON.parse(appliedSourceMap);
this.streamFull.push(file);
this.streamDts.push(file);
this.pendingIO--;
this.mightFinish();
});
}
private async applySourceMap(sourceMapContent: string, original: input.File, output: VinylFile) {
if (sourceMapContent === undefined) return undefined;
const map = JSON.parse(sourceMapContent);
const directory = path.dirname(output.path);
// gulp-sourcemaps docs:
// paths in the generated source map (`file` and `sources`) are relative to `file.base` (e.g. use `file.relative`).
map.file = utils.forwardSlashes(output.relative);
map.sources = map.sources.map(relativeToOutput);
delete map.sourceRoot;
const consumer = await new sourceMap.SourceMapConsumer(map);
const generator = sourceMap.SourceMapGenerator.fromSourceMap(consumer);
const sourceMapOrigins = this.project.singleOutput
? this.project.input.getFileNames(true).map(fName => this.project.input.getFile(fName))
: [original];
for (const sourceFile of sourceMapOrigins) {
if (!sourceFile || !sourceFile.gulp || !sourceFile.gulp.sourceMap) continue;
const inputOriginalMap = sourceFile.gulp.sourceMap;
const inputMap: sourceMap.RawSourceMap = typeof inputOriginalMap === 'object' ? inputOriginalMap : JSON.parse(inputOriginalMap);
// We should only apply the input mappings if the input mapping isn't empty,
// since `generator.applySourceMap` has a really bad performance on big inputs.
if (inputMap.mappings !== '') {
const inputConsumer = await new sourceMap.SourceMapConsumer(inputMap);
generator.applySourceMap(inputConsumer);
inputConsumer.destroy();
}
if (!inputMap.sources || !inputMap.sourcesContent) continue;
for (let i = 0; i < inputMap.sources.length; i++) {
const absolute = path.resolve(sourceFile.gulp.base, inputMap.sources[i]);
const relative = path.relative(output.base, absolute);
generator.setSourceContent(utils.forwardSlashes(relative), inputMap.sourcesContent[i]);
}
}
consumer.destroy();
return generator.toString();
function relativeToOutput(fileName: string) {
const absolute = path.resolve(directory, fileName);
return utils.forwardSlashes(path.relative(output.base, absolute));
}
}
// 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;
if (this.project.reporter.finish) this.project.reporter.finish(this.result);
if (reporter.countErrors(this.result) !== 0) {
this.streamFull.emit('error', new Error("TypeScript: Compilation failed"));
}
this.streamFull.emit('finish');
this.streamFull.push(null);
this.streamJs.push(null);
this.streamDts.push(null);
}
private getError(info: ts.Diagnostic): reporter.TypeScriptError {
let fileName = info.file && info.file.fileName;
const file = fileName && this.project.input.getFile(fileName);
return utils.getError(info, this.project.typescript, file);
}
diagnostic(info: ts.Diagnostic) {
this.error(this.getError(info));
}
error(error: reporter.TypeScriptError) {
if (!error) return;
// call reporter callback
if (this.project.reporter.error) this.project.reporter.error(<reporter.TypeScriptError> error, this.project.typescript);
// & emit the error on the stream.
}
}