-
Notifications
You must be signed in to change notification settings - Fork 74
/
Tsifier.js
259 lines (203 loc) · 6.56 KB
/
Tsifier.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
var convert = require('convert-source-map');
var events = require('events');
var fs = require('fs');
var log = require('debuglog')(require('../package').name);
var path = require('path');
var through = require('through2');
var time = require('./time');
var util = require('util');
var _ = require('lodash');
module.exports = function (ts) {
var CompileError = require('./CompileError')(ts);
var Host = require('./Host')(ts);
var currentDirectory = fs.realpathSync(process.cwd());
function isTypescript(file) {
return (/\.tsx?$/i).test(file);
}
function isTypescriptDeclaration(file) {
return (/\.d\.ts$/i).test(file);
}
function tsToJs(tsFile) {
return tsFile.replace(/\.tsx?$/i, '.js');
}
function tsxToJsx(tsxFile) {
return tsxFile.replace(/\.tsx$/i, '.jsx');
}
function getRelativeFilename(file) {
return './' + path.relative(currentDirectory, file)
.replace(/\\/g, '/');
}
function parseOptions(opts) {
var configFile = ts.findConfigFile(currentDirectory);
if (configFile) {
var configFileContents = require(path.join(currentDirectory, configFile));
opts = _.extend(configFileContents.compilerOptions, opts);
}
var parsed = ts.parseConfigFile({
compilerOptions: opts || {},
files: []
}, null, '');
// Always generate inline sourcemaps; browserify will only use these if --debug is set
parsed.options.sourceMap = false;
parsed.options.inlineSourceMap = true;
parsed.options.inlineSources = true;
// Use CommonJS module mode unless we are in ES6 mode (where it is invalid)
parsed.options.module = parsed.options.target === ts.ScriptTarget.ES6 ?
ts.ModuleKind.None :
ts.ModuleKind.CommonJS;
// Blacklist --out and --outDir; these options are irrelevant because files are written to
// tsify's in-memory cache instead of the filesystem
delete parsed.options.out;
delete parsed.options.outDir;
return parsed.options;
}
function Tsifier(opts) {
var self = this;
self.opts = parseOptions(opts);
self.host = new Host(currentDirectory, this.opts.target);
self.host.on('file', function (file, id) {
self.emit('file', file, id);
});
}
util.inherits(Tsifier, events.EventEmitter);
Tsifier.prototype.reset = function () {
this.host._reset();
};
Tsifier.prototype.generateCache = function (files) {
var tsFiles = files.filter(isTypescript)
.map(getRelativeFilename);
if (tsFiles.length === 0)
return;
this.addAll(tsFiles);
this.compile();
};
Tsifier.prototype.addAll = function (files) {
var self = this;
files.forEach(function (file) {
self.host._addFile(file, true);
});
};
Tsifier.prototype.compile = function () {
var self = this;
var rootFilenames = _(self.host.files).filter('root').map('filename').valueOf();
log('Compiling files:');
rootFilenames.forEach(function (file) { log(' %s', file); });
var createProgram_t0 = time.start();
var program = ts.createProgram(rootFilenames, self.opts, self.host);
time.stop(createProgram_t0, 'createProgram');
var syntaxDiagnostics = self.checkSyntax(program);
if (syntaxDiagnostics.length) {
log('Compilation encountered fatal syntax errors');
return;
}
var semanticDiagnostics = self.checkSemantics(program);
if (semanticDiagnostics.length && self.opts.noEmitOnError) {
log('Compilation encountered fatal semantic errors');
return;
}
var emit_t0 = time.start();
var emitOutput = program.emit();
time.stop(emit_t0, 'emit program');
var emittedDiagnostics = self.checkEmittedOutput(emitOutput);
if (emittedDiagnostics.length && self.opts.noEmitOnError) {
log('Compilation encountered fatal errors during emit');
return;
}
log('Compilation completed without errors');
};
Tsifier.prototype.checkSyntax = function (program) {
var self = this;
var syntaxCheck_t0 = time.start();
var syntaxDiagnostics = program.getSyntacticDiagnostics();
time.stop(syntaxCheck_t0, 'syntax checking');
syntaxDiagnostics.forEach(function (error) {
self.emit('error', new CompileError(error));
});
if (syntaxDiagnostics.length) {
self.host.error = true;
}
return syntaxDiagnostics;
};
Tsifier.prototype.checkSemantics = function (program) {
var self = this;
var semanticDiagnostics_t0 = time.start();
var semanticDiagnostics = program.getGlobalDiagnostics();
if (semanticDiagnostics.length === 0) {
semanticDiagnostics = program.getSemanticDiagnostics();
}
time.stop(semanticDiagnostics_t0, 'semantic checking');
semanticDiagnostics.forEach(function (error) {
self.emit('error', new CompileError(error));
});
if (semanticDiagnostics.length && self.opts.noEmitOnError) {
self.host.error = true;
}
return semanticDiagnostics;
};
Tsifier.prototype.checkEmittedOutput = function (emitOutput) {
var self = this;
var emittedDiagnostics = emitOutput.diagnostics;
emittedDiagnostics.forEach(function (error) {
self.emit('error', new CompileError(error));
});
if (emittedDiagnostics.length && self.opts.noEmitOnError) {
self.host.error = true;
}
return emittedDiagnostics;
};
Tsifier.prototype.transform = function (file) {
var self = this;
if (!isTypescript(file))
return through();
if (isTypescriptDeclaration(file))
return through(transform);
return through(transform, flush);
function transform(chunk, enc, next) {
next();
}
function flush(next) {
if (self.host.error)
return;
var compiled = self.getCompiledFile(getRelativeFilename(file));
if (compiled) {
this.push(compiled);
}
this.push(null);
next();
}
};
Tsifier.prototype.getCompiledFile = function (tsFile, alreadyMissedCache) {
var self = this;
var normalized = ts.normalizePath(tsFile);
var outputFile;
var output;
if (ts.isTsx && ts.isTsx(tsFile)) {
outputFile = tsxToJsx(normalized);
output = self.host.output[outputFile];
}
if (!output) {
outputFile = tsToJs(normalized);
output = self.host.output[outputFile];
}
if (!output) {
if (alreadyMissedCache)
return;
log('Cache miss on %s', normalized);
self.generateCache([tsFile]);
if (self.host.error)
return;
return self.getCompiledFile(tsFile, true);
}
output = self.setFullSourcePathInSourcemap(output, normalized);
return output;
};
Tsifier.prototype.setFullSourcePathInSourcemap = function (output, normalized) {
var sourcemap = convert.fromComment(output);
sourcemap.setProperty('sources', [normalized]);
return output.replace(convert.commentRegex, sourcemap.toComment());
}
var result = Tsifier;
result.isTypescript = isTypescript;
result.isTypescriptDeclaration = isTypescriptDeclaration;
return result;
};