mirror of
https://github.com/bobwen-dev/react-templates
synced 2025-04-12 00:56:39 +02:00
finished support for jsrt
This commit is contained in:
parent
084a44b402
commit
737272f7c3
@ -5,6 +5,7 @@ var path = require('path');
|
|||||||
var chalk = require('chalk');
|
var chalk = require('chalk');
|
||||||
var reactTemplates = require('./reactTemplates');
|
var reactTemplates = require('./reactTemplates');
|
||||||
var convertTemplateToReact = reactTemplates.convertTemplateToReact;
|
var convertTemplateToReact = reactTemplates.convertTemplateToReact;
|
||||||
|
var convertJSRTToJS = reactTemplates.convertJSRTToJS;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} source
|
* @param {string} source
|
||||||
@ -30,7 +31,12 @@ function convertFile(source, target, options, context) {
|
|||||||
if (shouldAddName) {
|
if (shouldAddName) {
|
||||||
options.name = reactTemplates.normalizeName(path.basename(source, path.extname(source))) + 'RT';
|
options.name = reactTemplates.normalizeName(path.basename(source, path.extname(source))) + 'RT';
|
||||||
}
|
}
|
||||||
var js = convertTemplateToReact(html, options);
|
var js;
|
||||||
|
if (options.modules === 'jsrt') {
|
||||||
|
js = convertJSRTToJS(html, options)
|
||||||
|
} else {
|
||||||
|
js = convertTemplateToReact(html, options);
|
||||||
|
}
|
||||||
if (!options.dryRun) {
|
if (!options.dryRun) {
|
||||||
fs.writeFileSync(target, js);
|
fs.writeFileSync(target, js);
|
||||||
}
|
}
|
||||||
|
21
src/cli.js
21
src/cli.js
@ -53,18 +53,23 @@ function printVersions(currentOptions) {
|
|||||||
* @param {string} filename file name to process
|
* @param {string} filename file name to process
|
||||||
*/
|
*/
|
||||||
function handleSingleFile(currentOptions, filename) {
|
function handleSingleFile(currentOptions, filename) {
|
||||||
if (path.extname(filename) !== '.rt') {
|
|
||||||
context.error('invalid file, only handle rt files', filename);
|
|
||||||
return;// only handle html files
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
var ext;
|
var sourceExt = path.extname(filename);
|
||||||
|
var outputFilename;
|
||||||
|
if (sourceExt == '.rt') {
|
||||||
if (currentOptions.modules !== 'typescript') {
|
if (currentOptions.modules !== 'typescript') {
|
||||||
ext = '.js';
|
outputFilename = filename + '.js';
|
||||||
} else {
|
} else {
|
||||||
ext = '.ts';
|
outputFilename = filename + '.ts';
|
||||||
}
|
}
|
||||||
api.convertFile(filename, filename + ext, currentOptions, context);
|
} else if (sourceExt === '.jsrt'){
|
||||||
|
outputFilename = filename.replace(/\.jsrt$/, '.js');
|
||||||
|
currentOptions = _.assign({},currentOptions, {'modules' : 'jsrt'});
|
||||||
|
} else {
|
||||||
|
context.error('invalid file, only handle rt/jsrt files', filename);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
api.convertFile(filename, outputFilename, currentOptions, context);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
context.error(e.message, filename, e.line, e.column, e.startOffset, e.endOffset);
|
context.error(e.message, filename, e.line, e.column, e.startOffset, e.endOffset);
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ module.exports = optionator({
|
|||||||
alias: 'm',
|
alias: 'm',
|
||||||
default: 'none',
|
default: 'none',
|
||||||
type: 'String',
|
type: 'String',
|
||||||
description: 'Use output modules. (amd|commonjs|none|es6|typescript)'
|
description: 'Use output modules. (amd|commonjs|none|es6|typescript|jsrt)'
|
||||||
}, {
|
}, {
|
||||||
option: 'name',
|
option: 'name',
|
||||||
alias: 'n',
|
alias: 'n',
|
||||||
|
@ -462,7 +462,7 @@ function convertTemplateToReact(html, options) {
|
|||||||
function convertJSRTToJS(text, options) {
|
function convertJSRTToJS(text, options) {
|
||||||
options = _.defaults({}, options, defaultOptions);
|
options = _.defaults({}, options, defaultOptions);
|
||||||
options.modules = 'jsrt';
|
options.modules = 'jsrt';
|
||||||
var templateMatcherJSRT = /<template>([^]+)<\/template>/gm;
|
var templateMatcherJSRT = /<template>([^]*?)<\/template>/gm;
|
||||||
var code = text.replace(templateMatcherJSRT, function (template, html) {
|
var code = text.replace(templateMatcherJSRT, function (template, html) {
|
||||||
return convertTemplateToReact(html, options).replace(/;$/,'');
|
return convertTemplateToReact(html, options).replace(/;$/,'');
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user