forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
905bfce
commit f6fd4a8
Showing
4 changed files
with
77 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict'; | ||
|
||
var fs = require('fs'); | ||
var path = require('path'); | ||
var ts = require('typescript'); | ||
|
||
var tsOptions = { module: 'commonjs' }; | ||
|
||
function formatErrorMessage(error) { | ||
return ( | ||
error.file.filename + '(' + | ||
error.file.getLineAndCharacterFromPosition(error.start).line + | ||
'): ' + | ||
error.messageText | ||
); | ||
} | ||
|
||
function compile(defaultLib, content, contentFilename) { | ||
var output = null; | ||
var compilerHost = { | ||
getSourceFile: function(filename, languageVersion) { | ||
if (filename === contentFilename) { | ||
return ts.createSourceFile(filename, content, 'ES5', '0'); | ||
} | ||
return defaultLib; | ||
}, | ||
writeFile: function(name, text, writeByteOrderMark) { | ||
if (output === null) { | ||
output = text; | ||
} else { | ||
throw new Error('Expected only one dependency.'); | ||
} | ||
}, | ||
getCanonicalFileName: function(filename) { return filename; }, | ||
getCurrentDirectory: function() { return ''; }, | ||
getNewLine: function() { return '\n'; } | ||
}; | ||
var program = ts.createProgram([contentFilename], tsOptions, compilerHost); | ||
var errors = program.getDiagnostics(); | ||
if (!errors.length) { | ||
var checker = program.getTypeChecker(true); | ||
errors = checker.getDiagnostics(); | ||
checker.emitFiles(); | ||
} | ||
if (errors.length) { | ||
throw new Error(errors.map(formatErrorMessage).join('\n')); | ||
} | ||
return output; | ||
} | ||
|
||
module.exports = function(defaultLibs) { | ||
var defaultLibSource = fs.readFileSync( | ||
path.join(path.dirname(require.resolve('typescript')), 'lib.d.ts') | ||
); | ||
|
||
for (var i = 0; i < defaultLibs.length; i++) { | ||
defaultLibSource += '\n' + fs.readFileSync(defaultLibs[i]); | ||
} | ||
|
||
var defaultLibSourceFile = ts.createSourceFile( | ||
'lib.d.ts', defaultLibSource, 'ES5' | ||
); | ||
|
||
return { | ||
compile: compile.bind(null, defaultLibSourceFile) | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters