Use TypeScript Compiler API Directly
In 1.4.0 we can use the TypeScript API directly to preprocess our files. This lets us get rid of a dependency. https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API We can also use this to provide our default libraries so that we don't need to keep the references in the test file.
Sebastian Markbage committed
Feb 1, 2015 at 22:41 UTC
f6fd4a8506ed0ccf29c17cd11490f550a13d4444
4 files changed
+77
-19
jest/preprocessor.js
+9
-15
@@ -3,7 +3,14 @@
3
var ReactTools = require('../main.js');
4
5
var coffee = require('coffee-script');
6
-var ts = require('ts-compiler');
6
+var tsPreprocessor = require('./ts-preprocessor');
7
+
8
+var defaultLibraries = [
9
+ require.resolve('./jest.d.ts'),
10
+ require.resolve('../src/modern/class/React.d.ts')
11
+];
12
+
13
+var ts = tsPreprocessor(defaultLibraries);
14
15
module.exports = {
16
process: function(src, path) {
@@ -11,20 +18,7 @@ module.exports = {
18
return coffee.compile(src, {'bare': true});
19
}
20
if (path.match(/\.ts$/) && !path.match(/\.d\.ts$/)) {
14
- ts.compile([path], {
15
- skipWrite: true,
16
- module: 'commonjs'
17
- }, function(err, results) {
18
- if (err) {
19
- throw err;
20
- }
21
- results.forEach(function(file) {
22
- // This is gross, but jest doesn't provide an asynchronous way to
23
- // process a module, and ts currently runs syncronously.
24
- src = file.text;
25
- });
26
- });
27
- return src;
21
+ return ts.compile(src, path);
22
}
23
return ReactTools.transform(src, {harmony: true});
24
}
jest/ts-preprocessor.js
new
+67
@@ -0,0 +1,67 @@
1
+'use strict';
2
+
3
+var fs = require('fs');
4
+var path = require('path');
5
+var ts = require('typescript');
6
+
7
+var tsOptions = { module: 'commonjs' };
8
+
9
+function formatErrorMessage(error) {
10
+ return (
11
+ error.file.filename + '(' +
12
+ error.file.getLineAndCharacterFromPosition(error.start).line +
13
+ '): ' +
14
+ error.messageText
15
+ );
16
+}
17
+
18
+function compile(defaultLib, content, contentFilename) {
19
+ var output = null;
20
+ var compilerHost = {
21
+ getSourceFile: function(filename, languageVersion) {
22
+ if (filename === contentFilename) {
23
+ return ts.createSourceFile(filename, content, 'ES5', '0');
24
+ }
25
+ return defaultLib;
26
+ },
27
+ writeFile: function(name, text, writeByteOrderMark) {
28
+ if (output === null) {
29
+ output = text;
30
+ } else {
31
+ throw new Error('Expected only one dependency.');
32
+ }
33
+ },
34
+ getCanonicalFileName: function(filename) { return filename; },
35
+ getCurrentDirectory: function() { return ''; },
36
+ getNewLine: function() { return '\n'; }
37
+ };
38
+ var program = ts.createProgram([contentFilename], tsOptions, compilerHost);
39
+ var errors = program.getDiagnostics();
40
+ if (!errors.length) {
41
+ var checker = program.getTypeChecker(true);
42
+ errors = checker.getDiagnostics();
43
+ checker.emitFiles();
44
+ }
45
+ if (errors.length) {
46
+ throw new Error(errors.map(formatErrorMessage).join('\n'));
47
+ }
48
+ return output;
49
+}
50
+
51
+module.exports = function(defaultLibs) {
52
+ var defaultLibSource = fs.readFileSync(
53
+ path.join(path.dirname(require.resolve('typescript')), 'lib.d.ts')
54
+ );
55
+
56
+ for (var i = 0; i < defaultLibs.length; i++) {
57
+ defaultLibSource += '\n' + fs.readFileSync(defaultLibs[i]);
58
+ }
59
+
60
+ var defaultLibSourceFile = ts.createSourceFile(
61
+ 'lib.d.ts', defaultLibSource, 'ES5'
62
+ );
63
+
64
+ return {
65
+ compile: compile.bind(null, defaultLibSourceFile)
66
+ };
67
+};
package.json
+1
-1
@@ -60,7 +60,7 @@
60
"recast": "^0.9.11",
61
"sauce-tunnel": "~1.1.0",
62
"tmp": "~0.0.18",
63
- "ts-compiler": "^2.0.0",
63
+ "typescript": "^1.4.0",
64
"uglify-js": "~2.4.0",
65
"uglifyify": "^2.4.0",
66
"wd": "~0.2.6"
src/modern/class/__tests__/ReactTypeScriptClass-test.ts
-3
@@ -7,9 +7,6 @@
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*/
9
10
-///<reference path='../../../../jest/jest.d.ts'/>
11
-///<reference path='../React.d.ts'/>
12
-
10
import React = require('React');
11
12
// Before Each