|
1 | 1 | // preprocessor.js
|
| 2 | +var fs = require('fs'); |
2 | 3 | var path = require('path');
|
3 |
| -var ts = require('ts-compiler'); |
| 4 | +var typescript = require('typescript'); |
4 | 5 | var react = require('react-tools');
|
5 | 6 |
|
| 7 | +var CACHE_DIR = path.join(path.resolve(__dirname + '/../build')); |
| 8 | + |
| 9 | +function isFileNewer(a, b) { |
| 10 | + try { |
| 11 | + return fs.statSync(a).mtime > fs.statSync(b).mtime; |
| 12 | + } catch (ex) { |
| 13 | + return false; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +function compileTypeScript(filePath) { |
| 18 | + var options = { |
| 19 | + outDir: CACHE_DIR, |
| 20 | + noEmitOnError: true, |
| 21 | + target: typescript.ScriptTarget.ES5, |
| 22 | + module: typescript.ModuleKind.CommonJS |
| 23 | + }; |
| 24 | + |
| 25 | + // re-use cached source if possible |
| 26 | + var outputPath = path.join(options.outDir, path.basename(filePath, '.ts')) + '.js'; |
| 27 | + if (isFileNewer(outputPath, filePath)) { |
| 28 | + return fs.readFileSync(outputPath).toString(); |
| 29 | + } |
| 30 | + |
| 31 | + if (fs.existsSync(outputPath)) { |
| 32 | + fs.unlinkSync(outputPath); |
| 33 | + } |
| 34 | + |
| 35 | + var host = typescript.createCompilerHost(options); |
| 36 | + var program = typescript.createProgram([filePath], options, host); |
| 37 | + var checker = typescript.createTypeChecker(program, true /* produceDiagnostics */); |
| 38 | + var result = checker.emitFiles(); |
| 39 | + |
| 40 | + var allErrors = program.getDiagnostics().concat(checker.getDiagnostics()) |
| 41 | + .concat(result.diagnostics); |
| 42 | + allErrors.forEach(function(diagnostic) { |
| 43 | + var lineChar = diagnostic.file.getLineAndCharacterFromPosition(diagnostic.start); |
| 44 | + console.error('%s %d:%d %s', diagnostic.file.filename, lineChar.line, lineChar.character, diagnostic.messageText); |
| 45 | + }); |
| 46 | + |
| 47 | + if (result.emitResultStatus !== typescript.EmitReturnStatus.Succeeded) { |
| 48 | + throw new Error('Compiling ' + filePath + ' failed'); |
| 49 | + } |
| 50 | + |
| 51 | + return fs.readFileSync(outputPath).toString(); |
| 52 | +} |
| 53 | + |
6 | 54 | module.exports = {
|
7 | 55 | process: function(src, filePath) {
|
8 | 56 | if (filePath.match(/\.ts$/) && !filePath.match(/\.d\.ts$/)) {
|
9 |
| - ts.compile([filePath], { |
10 |
| - skipWrite: true, |
11 |
| - module: 'commonjs' |
12 |
| - }, function(err, results) { |
13 |
| - if (err) { |
14 |
| - throw err; |
15 |
| - } |
16 |
| - results.forEach(function(file) { |
17 |
| - // This is gross, but jest doesn't provide an asynchronous way to |
18 |
| - // process a module, and ts currently runs syncronously. |
19 |
| - src = file.text; |
20 |
| - }); |
21 |
| - }); |
22 |
| - return src; |
23 |
| - } |
24 |
| - if (filePath.match(/\.js$/)) { |
25 |
| - return react.transform(src, {harmony: true}).replace( |
26 |
| - /require\('immutable/g, |
27 |
| - "require('" + path.relative(path.dirname(filePath), process.cwd()) |
28 |
| - ); |
| 57 | + return compileTypeScript(filePath); |
| 58 | + } else if (filePath.match(/\.js$/)) { |
| 59 | + var result = react.transform(src, {harmony: true}).replace( |
| 60 | + /require\('immutable/g, |
| 61 | + "require('" + path.relative(path.dirname(filePath), process.cwd()) |
| 62 | + ); |
| 63 | + return result; |
29 | 64 | }
|
30 | 65 | return src;
|
31 | 66 | }
|
|
0 commit comments