Skip to content

Commit b880e31

Browse files
authored
Merge pull request #45 from emberwatch/latest-b-c-ts
Update to (beta of) latest broccoli-typescript-compiler
2 parents 268483b + d09922b commit b880e31

File tree

6 files changed

+81
-128
lines changed

6 files changed

+81
-128
lines changed
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
{
22
"compilerOptions": {
3-
"target": "ES6",
3+
"target": "es2017",
44
"allowJs": true,
55
"moduleResolution": "node",
66
"noEmitOnError": false,
77
"noEmit": true,
8+
"sourceMap": true,
89
"baseUrl": ".",
910
"paths": {
1011
"<%= dasherizedPackageName %>/tests/*": ["tests/*"],
1112
"<%= dasherizedPackageName %>/*": ["app/*"]
1213
}
1314
},
14-
"include": [
15-
"app/**/*",
16-
"tests/**/*"
15+
"exclude": [
16+
"tmp",
17+
"dist",
18+
"node_modules",
19+
"bower_components"
1720
]
1821
}
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
var path = require('path');
1+
/* eslint-env node */
2+
3+
const path = require('path');
24

35
module.exports = {
46
description: 'Initialize files needed for typescript compilation',
57

6-
files: function() {
8+
files() {
79
return [
810
path.join(this.path, 'files', 'tsconfig.json'),
9-
path.join(this.path, 'files', 'app', 'config', 'environment.d.ts')
11+
path.join(this.path, 'files', 'app', 'config', 'environment.d.ts'),
1012
];
1113
},
1214

13-
mapFile: function() {
15+
mapFile() {
1416
const result = this._super.mapFile.apply(this, arguments);
1517

1618
const tsconfigPattern = `${path.sep}tsconfig.json`;
@@ -24,15 +26,16 @@ module.exports = {
2426
}
2527
},
2628

27-
normalizeEntityName: function() {
29+
normalizeEntityName() {
2830
// Entity name is optional right now, creating this hook avoids an error.
2931
},
3032

31-
afterInstall: function() {
33+
afterInstall() {
3234
return this.addPackagesToProject([
33-
{ name: 'typescript', target: '^2.1' },
34-
{ name: '@types/ember', target: '^2.7.41' },
35-
{ name: '@types/rsvp', target: '^3.3.0' }
35+
{ name: 'typescript', target: '^2.4.2' },
36+
{ name: '@types/ember', target: '^2.7.43' },
37+
{ name: '@types/rsvp', target: '^3.3.0' },
38+
{ name: '@types/ember-testing-helpers' },
3639
]);
37-
}
38-
}
40+
},
41+
};

index.js

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
/* eslint-env node */
22

3-
'use strict';
4-
var path = require('path');
5-
var process = require('process');
6-
73
let TsPreprocessor;
84
try {
95
TsPreprocessor = require('./lib/typescript-preprocessor');
10-
} catch ( ex ) {
6+
} catch (ex) {
117
// Do nothing; we just won't have the plugin available. This means that if you
128
// somehow end up in a state where it doesn't load, the preprocessor *will*
139
// fail, but this is necessary because the preprocessor depends on packages
@@ -17,30 +13,23 @@ try {
1713
module.exports = {
1814
name: 'ember-cli-typescript',
1915

20-
21-
included: function(app) {
22-
this._super.included.apply(this, arguments);
23-
this.app = app;
24-
25-
},
26-
27-
blueprintsPath: function() {
28-
return path.join(__dirname, 'blueprints');
29-
},
30-
31-
setupPreprocessorRegistry: function(type, registry) {
16+
setupPreprocessorRegistry(type, registry) {
3217
if (!TsPreprocessor) {
33-
console.log("Note: TypeScript preprocessor not available -- some dependencies not installed. (If this is during installation of the add-on, this is as expected. If it is while building, serving, or testing the application, this is an error.)");
18+
this.ui.write(
19+
'Note: TypeScript preprocessor not available -- some dependencies not installed. ' +
20+
'(If this is during installation of the add-on, this is as expected. If it is ' +
21+
'while building, serving, or testing the application, this is an error.)'
22+
);
3423
return;
3524
}
3625

3726
try {
38-
var plugin = new TsPreprocessor({includeExtensions: ['.ts','.js']});
39-
registry.add('js', plugin);
40-
} catch ( ex ) {
41-
console.log( "Missing or invalid tsconfig.json, please fix or run `ember generate ember-cli-typescript`." );
42-
console.log( ' ' + ex.toString());
27+
registry.add('js', new TsPreprocessor());
28+
} catch (ex) {
29+
this.ui.write(
30+
'Missing or invalid tsconfig.json, please fix or run `ember generate ember-cli-typescript`.'
31+
);
32+
this.ui.write(' ' + ex.toString());
4333
}
44-
}
45-
34+
},
4635
};

lib/typescript-preprocessor.js

Lines changed: 37 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,59 @@
1+
/* eslint-env node */
12
const fs = require('fs');
23
const path = require('path');
34

45
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');
98
const tsc = require('broccoli-typescript-compiler').typescript;
10-
const UnwatchedDir = require('broccoli-source').UnwatchedDir;
119

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');
2811

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;
4713

4814
class TypeScriptPreprocessor {
4915
constructor(options) {
50-
debug('creating new instance with options ', options);
5116
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;
5425
}
5526

5627
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',
8231
});
8332

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+
);
9252

9353
// Put everything together.
94-
return new MergeTrees([
95-
passthrough,
96-
tsc(filter, { tsconfig })
97-
], {
54+
return mergeTrees([js, ts], {
9855
overwrite: true,
99-
annotation: "TypeScript passthrough + ouput"
56+
annotation: 'merged JS & compiled TS',
10057
});
10158
}
10259
}

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "ember-cli-typescript",
3-
"version": "0.4.0",
3+
"version": "1.0.0-beta.1",
44
"description": "Allow ember apps to use typescript files.",
55
"keywords": [
66
"ember-addon",
77
"typescript"
88
],
99
"license": "MIT",
10-
"author": "Marius Seritan",
10+
"author": "Chris Krycho <[email protected]> (http://www.chriskrycho.com)",
1111
"contributors": [
12+
"Marius Seritan",
1213
"David Gardiner",
1314
"Philip Bjorge"
1415
],
@@ -27,17 +28,18 @@
2728
"test": "ember try:each"
2829
},
2930
"dependencies": {
31+
"broccoli-debug": "^0.6.3",
3032
"broccoli-funnel": "^1.0.6",
3133
"broccoli-merge-trees": "^1.1.4",
3234
"broccoli-plugin": "^1.2.1",
3335
"broccoli-source": "^1.1.0",
3436
"broccoli-stew": "^1.4.0",
35-
"broccoli-typescript-compiler": "^1.0.1",
37+
"broccoli-typescript-compiler": "^2.0.0",
3638
"debug": "^2.2.0",
3739
"ember-cli-babel": "^6.3.0"
3840
},
3941
"devDependencies": {
40-
"@types/ember": "^2.7.34",
42+
"@types/ember": "^2.7.43",
4143
"broccoli-asset-rev": "^2.4.5",
4244
"ember-cli": "^2.13.3",
4345
"ember-cli-app-version": "^2.0.0",
@@ -57,11 +59,10 @@
5759
"ember-resolver": "^4.0.0",
5860
"ember-source": "~2.13.3",
5961
"loader.js": "^4.2.3",
60-
"typescript": "^2.4.1"
62+
"typescript": "^2.4.2"
6163
},
6264
"peerDependencies": {
63-
"@types/ember": "^2.7.34",
64-
"typescript": "^2.4.1"
65+
"typescript": "^2.4.2"
6566
},
6667
"engines": {
6768
"node": "^4.5 || 6.* || >= 7.*"

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"paths": {}
99
},
1010
"include": [
11-
"app/**/*"
11+
"**/*"
1212
]
1313
}

0 commit comments

Comments
 (0)