|
| 1 | +var tsc = require('broccoli-typescript-compiler').typescript; |
| 2 | +var fs = require('fs'); |
| 3 | +var debug = require('debug')('ember-cli-typescript'); |
| 4 | +var ts = require('typescript'); |
| 5 | +var stew = require('broccoli-stew'); |
| 6 | +var find = stew.find; |
| 7 | +var MergeTrees = require("broccoli-merge-trees"); |
| 8 | +var Funnel = require("broccoli-funnel"); |
| 9 | + |
| 10 | +function TypeScriptPreprocessor(options) { |
| 11 | + debug('creating new instance with options ', options); |
| 12 | + this.name = 'ember-cli-typescript'; |
| 13 | + this.ext = 'ts'; |
| 14 | + this.options = JSON.parse(JSON.stringify(options)); |
| 15 | +} |
| 16 | + |
| 17 | +function readConfig(configFile) { |
| 18 | + var result = ts.readConfigFile(configFile, ts.sys.readFile); |
| 19 | + if (result.error) { |
| 20 | + var message = ts.flattenDiagnosticMessageText(result.error.messageText, "\n"); |
| 21 | + throw new Error(message); |
| 22 | + } |
| 23 | + return result.config; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Return the paths which contain type information. |
| 28 | + */ |
| 29 | +function typePaths(config) { |
| 30 | + var out = [ |
| 31 | + "node_modules/@types", |
| 32 | + ]; |
| 33 | + var paths = (config.compilerOptions && config.compilerOptions.paths) || {}; |
| 34 | + Object.keys(paths).forEach( function eachEntry(k) { |
| 35 | + // paths may contain a /*, eliminate it |
| 36 | + paths[k].forEach(function eachPath(a) { |
| 37 | + var p = a.split("/\*")[0]; |
| 38 | + if (out.indexOf(p) < 0) { |
| 39 | + out.push(p); |
| 40 | + } |
| 41 | + }); |
| 42 | + }); |
| 43 | + debug("type paths", out); |
| 44 | + return out; |
| 45 | +} |
| 46 | + |
| 47 | +TypeScriptPreprocessor.prototype.toTree = function(inputNode, inputPath, outputPath) { |
| 48 | + var config = readConfig("./tsconfig.json"); |
| 49 | + // "include" setting is meant for the IDE integration, |
| 50 | + // broccoli manages its own input files. |
| 51 | + if (config.include) { |
| 52 | + delete config.include; |
| 53 | + } |
| 54 | + config.include = ["**/*"]; |
| 55 | + |
| 56 | + /* |
| 57 | + * Create a funnel with the type files used by the typescript compiler. |
| 58 | + * |
| 59 | + */ |
| 60 | + var types = find(process.cwd(), { |
| 61 | + include: typePaths(config).map(function(a) { return a + '/**/*'}) |
| 62 | + }); |
| 63 | + |
| 64 | + /* |
| 65 | + * Passthrough all the javascript files existing |
| 66 | + * in the source/test folders. |
| 67 | + */ |
| 68 | + var passthrough = new Funnel(inputNode, { |
| 69 | + exclude: ["**/*.ts"], |
| 70 | + annotation: "TypeScript passthrough" |
| 71 | + }); |
| 72 | + |
| 73 | + /* |
| 74 | + * Files to run through the typescript compiler. |
| 75 | + */ |
| 76 | + var filter = new MergeTrees( [types, new Funnel(inputNode, { |
| 77 | + include: ["**/*.ts"], |
| 78 | + annotation: "TypeScript input" |
| 79 | + })]); |
| 80 | + |
| 81 | + /* |
| 82 | + * Put everything together. |
| 83 | + */ |
| 84 | + return new MergeTrees([ |
| 85 | + passthrough, |
| 86 | + tsc(filter, {tsconfig: config}) |
| 87 | + ], { |
| 88 | + overwrite: true, |
| 89 | + annotation: "TypeScript passthrough + ouput" |
| 90 | + }); |
| 91 | + |
| 92 | +}; |
| 93 | + |
| 94 | + |
| 95 | +module.exports = TypeScriptPreprocessor; |
0 commit comments