|
| 1 | +/* eslint-env node */ |
1 | 2 | const fs = require('fs'); |
2 | 3 | const path = require('path'); |
3 | 4 |
|
4 | 5 | const debug = require('debug')('ember-cli-typescript'); |
5 | | -const find = require('broccoli-stew').find; |
6 | | -const Funnel = require("broccoli-funnel"); |
7 | | -const MergeTrees = require("broccoli-merge-trees"); |
8 | | -const ts = require('typescript'); |
| 6 | +const funnel = require('broccoli-funnel'); |
| 7 | +const mergeTrees = require('broccoli-merge-trees'); |
9 | 8 | const tsc = require('broccoli-typescript-compiler').typescript; |
10 | | -const UnwatchedDir = require('broccoli-source').UnwatchedDir; |
11 | 9 |
|
12 | | -function readConfig(configFile) { |
13 | | - const result = ts.readConfigFile(configFile, ts.sys.readFile); |
14 | | - if (result.error) { |
15 | | - const message = ts.flattenDiagnosticMessageText(result.error.messageText, "\n"); |
16 | | - throw new Error(message); |
17 | | - } |
18 | | - return result.config; |
19 | | -} |
20 | | - |
21 | | -/** |
22 | | - * Return the paths which contain type information. |
23 | | - */ |
24 | | -function typePaths(config) { |
25 | | - const base = ["node_modules/@types"]; |
26 | | - |
27 | | - const cfgPaths = (config.compilerOptions && config.compilerOptions.paths) || {}; |
| 10 | +const BroccoliDebug = require('broccoli-debug'); |
28 | 11 |
|
29 | | - const toTypePaths = paths => (splitPaths, key) => { |
30 | | - // paths may end in a `/*`; keep everything before it |
31 | | - const upToSlashStar = path => path.split("/\*")[0]; |
32 | | - |
33 | | - // only store unique paths |
34 | | - const notAlreadyStoredIn = storedPaths => path => !storedPaths.includes(path); |
35 | | - |
36 | | - const newPaths = paths[key] |
37 | | - .map(upToSlashStar) |
38 | | - .filter(notAlreadyStoredIn(splitPaths)); |
39 | | - |
40 | | - return splitPaths.concat(newPaths); |
41 | | - }; |
42 | | - |
43 | | - const out = Object.keys(cfgPaths).reduce(toTypePaths(cfgPaths), base); |
44 | | - debug("type paths", out); |
45 | | - return out; |
46 | | -} |
| 12 | +let tag = 0; |
47 | 13 |
|
48 | 14 | class TypeScriptPreprocessor { |
49 | 15 | constructor(options) { |
50 | | - debug('creating new instance with options ', options); |
51 | 16 | this.name = 'ember-cli-typescript'; |
52 | | - this.ext = 'ts'; |
53 | | - this.options = JSON.parse(JSON.stringify(options)); |
| 17 | + this._tag = tag++; |
| 18 | + |
| 19 | + // Update the config for how Broccoli handles the file system: no need for |
| 20 | + // includes, always emit, and let Broccoli manage any outDir. |
| 21 | + this.config = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'tsconfig.json'))); |
| 22 | + this.config.compilerOptions.noEmit = false; |
| 23 | + delete this.config.compilerOptions.outDir; |
| 24 | + delete this.config.include; |
54 | 25 | } |
55 | 26 |
|
56 | 27 | toTree(inputNode, inputPath, outputPath) { |
57 | | - const tsconfig = readConfig(path.join(".", "tsconfig.json")); |
58 | | - |
59 | | - // The `include` setting is meant for the IDE integration; broccoli manages |
60 | | - // manages its own input files. |
61 | | - tsconfig.include = ["**/*.ts"]; |
62 | | - |
63 | | - // tsc needs to emit files on the broccoli pipeline, but not in the default |
64 | | - // config. Otherwise its compiled `.js` files may be created inadvertently. |
65 | | - tsconfig.compilerOptions.noEmit = false; |
66 | | - delete tsconfig.compilerOptions.outDir; |
67 | | - |
68 | | - // Create a funnel with the type files used by the typescript compiler. |
69 | | - // These will change infrequently (read: usually not at all) so grab each as |
70 | | - // an *unwatched* directory, and return it at the proper location. |
71 | | - const typeTrees = typePaths(tsconfig).map((typePath) => { |
72 | | - const typeTree = new UnwatchedDir(typePath); |
73 | | - return new Funnel(typeTree, { destDir: typePath }); |
74 | | - }); |
75 | | - |
76 | | - const types = new MergeTrees(typeTrees); |
77 | | - |
78 | | - // Passthrough all the javascript files existing in the source/test folders. |
79 | | - const passthrough = new Funnel(inputNode, { |
80 | | - exclude: ["**/*.ts"], |
81 | | - annotation: "TypeScript passthrough" |
| 28 | + const js = funnel(inputNode, { |
| 29 | + exclude: ['**/*.ts'], |
| 30 | + annotation: 'JS files', |
82 | 31 | }); |
83 | 32 |
|
84 | | - // Files to run through the typescript compiler. |
85 | | - const filter = new MergeTrees([ |
86 | | - types, |
87 | | - new Funnel(inputNode, { |
88 | | - include: ["**/*.ts"], |
89 | | - annotation: "TypeScript input" |
90 | | - }) |
91 | | - ]); |
| 33 | + const debugTree = BroccoliDebug.buildDebugCallback('ember-cli-typescript'); |
| 34 | + |
| 35 | + const uncompiledTs = debugTree( |
| 36 | + funnel(inputNode, { |
| 37 | + include: ['**/*.ts'], |
| 38 | + annotation: 'uncompiled TS files', |
| 39 | + }), |
| 40 | + `${this._tag}` |
| 41 | + ); |
| 42 | + |
| 43 | + const ts = debugTree( |
| 44 | + tsc(uncompiledTs, { |
| 45 | + throwOnError: !this.config.compilerOptions.noEmitOnError, |
| 46 | + annotation: 'Compiled TS files', |
| 47 | + include: ['**/*'], |
| 48 | + tsconfig: this.config, |
| 49 | + }), |
| 50 | + `${this._tag}` |
| 51 | + ); |
92 | 52 |
|
93 | 53 | // Put everything together. |
94 | | - return new MergeTrees([ |
95 | | - passthrough, |
96 | | - tsc(filter, { tsconfig }) |
97 | | - ], { |
| 54 | + return mergeTrees([js, ts], { |
98 | 55 | overwrite: true, |
99 | | - annotation: "TypeScript passthrough + ouput" |
| 56 | + annotation: 'merged JS & compiled TS', |
100 | 57 | }); |
101 | 58 | } |
102 | 59 | } |
|
0 commit comments