UNPKG

1.72 kBJavaScriptView Raw
1'use strict';
2
3var ts = require('typescript');
4var Filter = require('broccoli-persistent-filter');
5var stringify = require('json-stable-stringify');
6var crypto = require('crypto');
7
8var getCallerFile = require('get-caller-file');
9
10var loadTSConfig = require('./lib/load-ts-config');
11var findTSConfig = require('./lib/find-ts-config');
12
13module.exports = TypeScript;
14function 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
33TypeScript.prototype = Object.create(Filter.prototype);
34TypeScript.prototype.constructor = TypeScript;
35
36TypeScript.prototype.baseDir = function() {
37 return __dirname;
38};
39
40/*
41 * @private
42 *
43 * @method optionsString
44 * @returns a stringifeid version of the input options
45 */
46TypeScript.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
53TypeScript.prototype.cacheKeyProcessString = function(string, relativePath) {
54 return this.optionsHash() + Filter.prototype.cacheKeyProcessString.call(this, string, relativePath);
55};
56
57TypeScript.prototype.processString = function (string, relativePath) {
58 return ts.transpileModule(string, {compilerOptions: this.options, fileName: relativePath}).outputText;
59};