1 | 'use strict';
|
2 |
|
3 | var realpath = require('fs.realpath');
|
4 | var log = require('util').debuglog(require('./package').name);
|
5 | var through = require('through2');
|
6 | var path = require('path');
|
7 |
|
8 | function tsify(b, opts) {
|
9 |
|
10 | if (typeof b === 'string') {
|
11 | throw new Error('tsify appears to have been configured as a transform; it must be configured as a plugin.');
|
12 | }
|
13 | var ts = opts.typescript || require('typescript');
|
14 | if (typeof ts === 'string' || ts instanceof String) {
|
15 | ts = require(ts);
|
16 | }
|
17 |
|
18 | var Tsifier = require('./lib/Tsifier')(ts);
|
19 | var tsifier = new Tsifier(opts, b._options);
|
20 |
|
21 | tsifier.on('error', function (error) {
|
22 | b.pipeline.emit('error', error);
|
23 | });
|
24 | tsifier.on('file', function (file, id) {
|
25 | b.emit('file', file, id);
|
26 | });
|
27 |
|
28 | setupPipeline();
|
29 |
|
30 | var transformOpts = {
|
31 | global: opts.global
|
32 | };
|
33 | b.transform(tsifier.transform.bind(tsifier), transformOpts);
|
34 |
|
35 | b.on('reset', function () {
|
36 | setupPipeline();
|
37 | });
|
38 |
|
39 | function setupPipeline() {
|
40 | if (tsifier.opts.jsx && b._extensions.indexOf('.tsx') === -1)
|
41 | b._extensions.unshift('.tsx');
|
42 |
|
43 | if (b._extensions.indexOf('.ts') === -1)
|
44 | b._extensions.unshift('.ts');
|
45 |
|
46 | b.pipeline.get('record').push(gatherEntryPoints());
|
47 | }
|
48 |
|
49 | function gatherEntryPoints() {
|
50 | var rows = [];
|
51 | return through.obj(transform, flush);
|
52 |
|
53 | function transform(row, enc, next) {
|
54 | rows.push(row);
|
55 | next();
|
56 | }
|
57 |
|
58 | function flush(next) {
|
59 | var self = this;
|
60 | var ignoredFiles = [];
|
61 | var entryFiles = rows
|
62 | .map(function (row) {
|
63 | var file = row.file || row.id;
|
64 | if (file) {
|
65 | if (row.source !== undefined) {
|
66 | ignoredFiles.push(file);
|
67 | } else if (row.basedir) {
|
68 | return path.resolve(row.basedir, file);
|
69 | } else if (path.isAbsolute(file)) {
|
70 | return file;
|
71 | } else {
|
72 | ignoredFiles.push(file);
|
73 | }
|
74 | }
|
75 | return null;
|
76 | })
|
77 | .filter(function (file) { return file; })
|
78 | .map(function (file) { return realpath.realpathSync(file); });
|
79 | if (entryFiles.length) {
|
80 | log('Files from browserify entry points:');
|
81 | entryFiles.forEach(function (file) { log(' %s', file); });
|
82 | }
|
83 | if (ignoredFiles.length) {
|
84 | log('Ignored browserify entry points:');
|
85 | ignoredFiles.forEach(function (file) { log(' %s', file); });
|
86 | }
|
87 | tsifier.reset();
|
88 | tsifier.generateCache(entryFiles, ignoredFiles);
|
89 | rows.forEach(function (row) { self.push(row); });
|
90 | self.push(null);
|
91 | next();
|
92 | }
|
93 | }
|
94 | }
|
95 |
|
96 | module.exports = tsify;
|