1 | 'use strict';
|
2 |
|
3 | var ts = require('typescript');
|
4 | var Filter = require('broccoli-persistent-filter');
|
5 | var stringify = require('json-stable-stringify');
|
6 | var crypto = require('crypto');
|
7 |
|
8 | var getCallerFile = require('get-caller-file');
|
9 |
|
10 | var loadTSConfig = require('./lib/load-ts-config');
|
11 | var findTSConfig = require('./lib/find-ts-config');
|
12 |
|
13 | module.exports = TypeScript;
|
14 | function TypeScript(inputTree, _options) {
|
15 | var options = _options || {};
|
16 | options.tsconfig = options.tsconfig || findTSConfig(getCallerFile(2));
|
17 |
|
18 | if (!(this instanceof TypeScript)) {
|
19 | return new TypeScript(inputTree, options);
|
20 | }
|
21 |
|
22 | Filter.call(this, inputTree, {
|
23 | persist: true,
|
24 | extensions: ['ts','d.ts'],
|
25 | targetExtension: 'js',
|
26 | name: 'broccoli-typescript-compiler',
|
27 | annotation: options.annotation
|
28 | });
|
29 |
|
30 | this.options = loadTSConfig(options.tsconfig);
|
31 | }
|
32 |
|
33 | TypeScript.prototype = Object.create(Filter.prototype);
|
34 | TypeScript.prototype.constructor = TypeScript;
|
35 |
|
36 | TypeScript.prototype.baseDir = function() {
|
37 | return __dirname;
|
38 | };
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | TypeScript.prototype.optionsHash = function() {
|
47 | if (!this._optionsHash) {
|
48 | this._optionsHash = crypto.createHash('md5').update(stringify(this.options), 'utf8').digest('hex');
|
49 | }
|
50 | return this._optionsHash;
|
51 | };
|
52 |
|
53 | TypeScript.prototype.cacheKeyProcessString = function(string, relativePath) {
|
54 | return this.optionsHash() + Filter.prototype.cacheKeyProcessString.call(this, string, relativePath);
|
55 | };
|
56 |
|
57 | TypeScript.prototype.processString = function (string, relativePath) {
|
58 | return ts.transpileModule(string, {compilerOptions: this.options, fileName: relativePath}).outputText;
|
59 | };
|