1
0
mirror of https://github.com/bobwen-dev/react-templates synced 2025-04-12 00:56:39 +02:00

support named amd module

This commit is contained in:
ido 2015-01-22 16:50:00 +02:00
parent bd47783a0c
commit 866df02d0e
5 changed files with 51 additions and 2 deletions

View File

@ -26,7 +26,7 @@ function convertFile(source, target, options, context) {
}
var html = fs.readFileSync(source).toString();
if (!options.name) {
if (options.modules === 'none' && !options.name) {
options.name = reactTemplates.normalizeName(path.basename(source, path.extname(source))) + 'RT';
}
var js = convertTemplateToReact(html, options);

View File

@ -49,7 +49,6 @@ module.exports = optionator({
}, {
option: 'name',
alias: 'n',
default: 'filenameRT',
type: 'String',
description: 'When using globals, the name for the variable. The default is the [file name]RT'
}, {

9
test/data/div.rt.amd.js Normal file
View File

@ -0,0 +1,9 @@
define('div', [
'react/addons',
'lodash'
], function (React, _) {
'use strict';
return function () {
return React.createElement('div', {});
};
});

View File

@ -0,0 +1,3 @@
var div = function () {
return React.createElement('div', {});
};

View File

@ -105,6 +105,44 @@ test('conversion test', function (t) {
}
});
test('conversion test globals', function (t) {
var files = ['div.rt'];
t.plan(files.length);
files.forEach(check);
function check(testFile) {
var filename = path.join(dataPath, testFile);
var html = readFileNormalized(filename);
var expected = readFileNormalized(filename + '.globals.js');
// var expected = fs.readFileSync(filename.replace(".html", ".js")).toString();
var actual = reactTemplates.convertTemplateToReact(html, {modules: 'none', name: 'div'}).replace(/\r/g, '').trim();
t.equal(actual, expected);
if (actual !== expected) {
fs.writeFileSync(filename + '.actual.js', actual);
}
}
});
test('conversion test amd with name', function (t) {
var files = ['div.rt'];
t.plan(files.length);
files.forEach(check);
function check(testFile) {
var filename = path.join(dataPath, testFile);
var html = readFileNormalized(filename);
var expected = readFileNormalized(filename + '.amd.js');
// var expected = fs.readFileSync(filename.replace(".html", ".js")).toString();
var actual = reactTemplates.convertTemplateToReact(html, {modules: 'amd', name: 'div'}).replace(/\r/g, '').trim();
t.equal(actual, expected);
if (actual !== expected) {
fs.writeFileSync(filename + '.actual.js', actual);
}
}
});
function normalizeHtml(html) {
return cheerio.load(html, {normalizeWhitespace: true}).html()
.replace(/\>\s+/mg, '>')