-
Notifications
You must be signed in to change notification settings - Fork 74
/
index.js
96 lines (81 loc) · 2.36 KB
/
index.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
'use strict';
var realpath = require('fs.realpath');
var log = require('util').debuglog(require('./package').name);
var through = require('through2');
var path = require('path');
function tsify(b, opts) {
if (typeof b === 'string') {
throw new Error('tsify appears to have been configured as a transform; it must be configured as a plugin.');
}
var ts = opts.typescript || require('typescript');
if (typeof ts === 'string' || ts instanceof String) {
ts = require(ts);
}
var Tsifier = require('./lib/Tsifier')(ts);
var tsifier = new Tsifier(opts, b._options);
tsifier.on('error', function (error) {
b.pipeline.emit('error', error);
});
tsifier.on('file', function (file, id) {
b.emit('file', file, id);
});
setupPipeline();
var transformOpts = {
global: opts.global
};
b.transform(tsifier.transform.bind(tsifier), transformOpts);
b.on('reset', function () {
setupPipeline();
});
function setupPipeline() {
if (tsifier.opts.jsx && b._extensions.indexOf('.tsx') === -1)
b._extensions.unshift('.tsx');
if (b._extensions.indexOf('.ts') === -1)
b._extensions.unshift('.ts');
b.pipeline.get('record').push(gatherEntryPoints());
}
function gatherEntryPoints() {
var rows = [];
return through.obj(transform, flush);
function transform(row, enc, next) {
rows.push(row);
next();
}
function flush(next) {
var self = this;
var ignoredFiles = [];
var entryFiles = rows
.map(function (row) {
var file = row.file || row.id;
if (file) {
if (row.source !== undefined) {
ignoredFiles.push(file);
} else if (row.basedir) {
return path.resolve(row.basedir, file);
} else if (path.isAbsolute(file)) {
return file;
} else {
ignoredFiles.push(file);
}
}
return null;
})
.filter(function (file) { return file; })
.map(function (file) { return realpath.realpathSync(file); });
if (entryFiles.length) {
log('Files from browserify entry points:');
entryFiles.forEach(function (file) { log(' %s', file); });
}
if (ignoredFiles.length) {
log('Ignored browserify entry points:');
ignoredFiles.forEach(function (file) { log(' %s', file); });
}
tsifier.reset();
tsifier.generateCache(entryFiles, ignoredFiles);
rows.forEach(function (row) { self.push(row); });
self.push(null);
next();
}
}
}
module.exports = tsify;