Added TypeScript support via the `--modules typescript` flag

This commit is contained in:
Bruno Grieder 2015-02-17 02:23:08 +01:00
parent 87ca187fa6
commit 49b5034f3e
5 changed files with 43 additions and 11 deletions

View File

@ -58,7 +58,14 @@ function handleSingleFile(currentOptions, filename) {
return;// only handle html files
}
try {
api.convertFile(filename, filename + '.js', currentOptions, context);
var ext;
if (currentOptions.modules !== 'typescript') {
ext = '.js';
}
else {
ext='.ts';
}
api.convertFile(filename, filename + ext, currentOptions, context);
} catch (e) {
context.error(e.message, filename, e.line, e.column, e.startOffset, e.endOffset);
}

View File

@ -45,7 +45,7 @@ module.exports = optionator({
alias: 'm',
default: 'none',
type: 'String',
description: 'Use output modules. (amd|commonjs|none)'
description: 'Use output modules. (amd|commonjs|none|typescript)'
}, {
option: 'name',
alias: 'n',

View File

@ -26,6 +26,7 @@ var templatePJSTemplate = _.template('var <%= name %> = function () {\n' +
'<%= injectedFunctions %>\n' +
'return <%= body %>\n' +
'};\n');
var templateTypescriptTemplate = _.template("<%= vars %>\n\n<%= injectedFunctions %>\nvar fn = function() { return <%= body %> };\nexport = fn\n");
var htmlSelfClosingTags = ['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
var templateProp = 'rt-repeat';
@ -352,7 +353,12 @@ function convertTemplateToReact(html, options) {
} else if (tag.children.length) {
throw RTCodeError.build('rt-require may have no children', context, tag);
} else {
defines[tag.attribs.dependency] = tag.attribs.as;
if (options.modules !== 'typescript') {
defines[tag.attribs.dependency] = tag.attribs.as;
}
else {
defines['./'+tag.attribs.dependency] = tag.attribs.as;
}
}
} else if (firstTag === null) {
firstTag = tag;
@ -366,16 +372,25 @@ function convertTemplateToReact(html, options) {
var body = convertHtmlToReact(firstTag, context);
var requirePaths = _(defines).keys().map(function (reqName) { return '"' + reqName + '"'; }).value().join(',');
var requireVars = _(defines).values().value().join(',');
var vars = _(defines).map(function (reqVar, reqPath) { return 'var ' + reqVar + " = require('" + reqPath + "');"; }).join('\n');
var vars;
if (options.modules !== 'typescript') {
vars = _(defines).map(function (reqVar, reqPath) { return 'var ' + reqVar + " = require('" + reqPath + "');"; }).join('\n');
}
else {
vars = _(defines).map(function (reqVar, reqPath) { return 'import ' + reqVar + " = require('" + reqPath + "');"; }).join('\n');
}
var data = {body: body, injectedFunctions: '', requireNames: requireVars, requirePaths: requirePaths, vars: vars, name: options.name};
data.injectedFunctions = context.injectedFunctions.join('\n');
var code = generate(data, options);
try {
var tree = esprima.parse(code, {range: true, tokens: true, comment: true});
tree = escodegen.attachComments(tree, tree.comments, tree.tokens);
code = escodegen.generate(tree, {comment: true});
} catch (e) {
throw new RTCodeError(e.message, e.index, -1);
if (options.modules !== 'typescript') {
try {
var tree = esprima.parse( code, {range: true, tokens: true, comment: true} );
tree = escodegen.attachComments( tree, tree.comments, tree.tokens );
code = escodegen.generate( tree, {comment: true} );
}
catch ( e ) {
throw new RTCodeError( e.message, e.index, -1 );
}
}
return code;
}
@ -387,6 +402,9 @@ function generate(data, options) {
if (options.modules === 'commonjs') {
return templateCommonJSTemplate(data);
}
if (options.modules === 'typescript') {
return templateTypescriptTemplate(data);
}
return templatePJSTemplate(data);
}

View File

@ -0,0 +1,6 @@
import React = require('react/addons');
import _ = require('lodash');
var fn = function() { return React.createElement('div',{}) };
export = fn

View File

@ -131,7 +131,8 @@ test('conversion test commonjs', function (t) {
var files = [
{source: 'div.rt', expected: 'div.rt.commonjs.js', options: {modules: 'commonjs'}},
{source: 'div.rt', expected: 'div.rt.amd.js', options: {modules: 'amd', name: 'div'}},
{source: 'div.rt', expected: 'div.rt.globals.js', options: {modules: 'none', name: 'div'}}
{source: 'div.rt', expected: 'div.rt.globals.js', options: {modules: 'none', name: 'div'}},
{source: 'div.rt', expected: 'div.rt.typescript.ts', options: {modules: 'typescript'}}
];
t.plan(files.length);
files.forEach(check);