added jsrt support

This commit is contained in:
avim 2015-03-29 15:55:40 +03:00
parent 92e8ca38cd
commit 084a44b402
4 changed files with 62 additions and 2 deletions

View File

@ -31,15 +31,18 @@ var templatePJSTemplate = _.template('var <%= name %> = function () {\n' +
'return <%= body %>\n' +
'};\n');
var templateTypescriptTemplate = _.template('<%= vars %>\n\n<%= injectedFunctions %>\nvar fn = function() { return <%= body %> };\nexport = fn\n');
var templateJSRTTemplate = _.template('(function () {\n <%= injectedFunctions %>\n return function(){\nreturn <%= body %>}}\n)()');
var templates = {
amd: templateAMDTemplate,
commonjs: templateCommonJSTemplate,
typescript: templateTypescriptTemplate,
es6: templateES6Template,
none: templatePJSTemplate
none: templatePJSTemplate,
jsrt: templateJSRTTemplate
};
var htmlSelfClosingTags = ['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
var templateProp = 'rt-repeat';
@ -443,7 +446,26 @@ function convertTemplateToReact(html, options) {
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);
if (options.modules !== 'typescript') {
if (options.modules !== 'typescript' && options.modules !== 'jsrt') {
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) {
console.log(code);
throw new RTCodeError(e.message, e.index, -1);
}
}
return code;
}
function convertJSRTToJS(text, options) {
options = _.defaults({}, options, defaultOptions);
options.modules = 'jsrt';
var templateMatcherJSRT = /<template>([^]+)<\/template>/gm;
var code = text.replace(templateMatcherJSRT, function (template, html) {
return convertTemplateToReact(html, options).replace(/;$/,'');
});
try {
var tree = esprima.parse(code, {range: true, tokens: true, comment: true});
tree = escodegen.attachComments(tree, tree.comments, tree.tokens);
@ -451,7 +473,6 @@ function convertTemplateToReact(html, options) {
} catch (e) {
throw new RTCodeError(e.message, e.index, -1);
}
}
return code;
}
@ -483,6 +504,7 @@ function normalizeName(name) {
module.exports = {
convertTemplateToReact: convertTemplateToReact,
convertJSRTToJS: convertJSRTToJS,
RTCodeError: RTCodeError,
normalizeName: normalizeName,
_test: {

13
test/data/simple.js Normal file
View File

@ -0,0 +1,13 @@
define([
'react',
'lodash'
], function (React, _) {
var comp = React.createClass({
render: function () {
return function () {
return React.createElement('div', {}, 'hello world');
};
}()
});
return comp;
});

10
test/data/simple.jsrt Normal file
View File

@ -0,0 +1,10 @@
define(['react','lodash'], function (React, _) {
var comp = React.createClass({
render:
<template>
<div>hello world</div>
</template>
});
return comp;
});

View File

@ -148,6 +148,21 @@ test('convert div with all module types', function (t) {
}
});
test('convert jsrt and test source results', function (t) {
var files = ['simple.jsrt'];
t.plan(files.length);
files.forEach(check);
function check(file) {
var filename = path.join(dataPath, file);
var js = readFileNormalized(filename);
var expected = readFileNormalized(path.join(dataPath, file.replace('.jsrt','.js')));
// var expected = fs.readFileSync(filename.replace(".html", ".js")).toString();
var actual = reactTemplates.convertJSRTToJS(js).replace(/\r/g, '').trim();
compareAndWrite(t, actual, expected, filename);
}
});
/**
* @param {string} html
* @return {string}