Skip to content

Commit f6fd4a8

Browse files
committed
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.
1 parent 905bfce commit f6fd4a8

File tree

4 files changed

+77
-19
lines changed

4 files changed

+77
-19
lines changed

jest/preprocessor.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,22 @@
33
var ReactTools = require('../main.js');
44

55
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);
714

815
module.exports = {
916
process: function(src, path) {
1017
if (path.match(/\.coffee$/)) {
1118
return coffee.compile(src, {'bare': true});
1219
}
1320
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);
2822
}
2923
return ReactTools.transform(src, {harmony: true});
3024
}

jest/ts-preprocessor.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"recast": "^0.9.11",
6161
"sauce-tunnel": "~1.1.0",
6262
"tmp": "~0.0.18",
63-
"ts-compiler": "^2.0.0",
63+
"typescript": "^1.4.0",
6464
"uglify-js": "~2.4.0",
6565
"uglifyify": "^2.4.0",
6666
"wd": "~0.2.6"

src/modern/class/__tests__/ReactTypeScriptClass-test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

10-
///<reference path='../../../../jest/jest.d.ts'/>
11-
///<reference path='../React.d.ts'/>
12-
1310
import React = require('React');
1411

1512
// Before Each

0 commit comments

Comments
 (0)