diff --git a/docs/cli.md b/docs/cli.md index 2956fe5..a4cf98a 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -47,7 +47,7 @@ This option allows to override the output file even if there are no changes. ### `-m`, `--modules` -Use output modules. (amd|commonjs|none) - default: none +Use output modules. (amd|commonjs|none|typescript) - default: none ### `-f`, `--format` diff --git a/src/cli.js b/src/cli.js index a3ae464..33d1d9f 100755 --- a/src/cli.js +++ b/src/cli.js @@ -58,7 +58,13 @@ 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); } diff --git a/src/options.js b/src/options.js index 4fa690b..31d2045 100644 --- a/src/options.js +++ b/src/options.js @@ -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', diff --git a/src/reactTemplates.js b/src/reactTemplates.js index 1eaa9e1..9722f91 100644 --- a/src/reactTemplates.js +++ b/src/reactTemplates.js @@ -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'; @@ -351,9 +352,12 @@ function convertTemplateToReact(html, options) { throw RTCodeError.build("rt-require needs 'dependency' and 'as' attributes", context, tag); } 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; } else { @@ -366,16 +370,23 @@ 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 'import ' + reqVar + " = require('" + reqPath + "');"; }).join('\n'); + } else { + vars = _(defines).map(function (reqVar, reqPath) { return 'var ' + 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 +398,9 @@ function generate(data, options) { if (options.modules === 'commonjs') { return templateCommonJSTemplate(data); } + if (options.modules === 'typescript') { + return templateTypescriptTemplate(data); + } return templatePJSTemplate(data); } diff --git a/test/data/div.rt.typescript.ts b/test/data/div.rt.typescript.ts new file mode 100644 index 0000000..e370605 --- /dev/null +++ b/test/data/div.rt.typescript.ts @@ -0,0 +1,6 @@ +import React = require('react/addons'); +import _ = require('lodash'); + + +var fn = function() { return React.createElement('div',{}) }; +export = fn diff --git a/test/src/test.js b/test/src/test.js index 959448f..9e1c1ac 100644 --- a/test/src/test.js +++ b/test/src/test.js @@ -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);