commit
b1e17e6bd9
|
@ -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`
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
import React = require('react/addons');
|
||||
import _ = require('lodash');
|
||||
|
||||
|
||||
var fn = function() { return React.createElement('div',{}) };
|
||||
export = fn
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue