From 866df02d0ee8abbd2f6720a081e867117d40c006 Mon Sep 17 00:00:00 2001 From: ido Date: Thu, 22 Jan 2015 16:50:00 +0200 Subject: [PATCH] support named amd module --- src/api.js | 2 +- src/options.js | 1 - test/data/div.rt.amd.js | 9 +++++++++ test/data/div.rt.globals.js | 3 +++ test/src/test.js | 38 +++++++++++++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 test/data/div.rt.amd.js create mode 100644 test/data/div.rt.globals.js diff --git a/src/api.js b/src/api.js index bc5f713..94d948b 100644 --- a/src/api.js +++ b/src/api.js @@ -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); diff --git a/src/options.js b/src/options.js index 176245a..04c4d89 100644 --- a/src/options.js +++ b/src/options.js @@ -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' }, { diff --git a/test/data/div.rt.amd.js b/test/data/div.rt.amd.js new file mode 100644 index 0000000..56ca4a4 --- /dev/null +++ b/test/data/div.rt.amd.js @@ -0,0 +1,9 @@ +define('div', [ + 'react/addons', + 'lodash' +], function (React, _) { + 'use strict'; + return function () { + return React.createElement('div', {}); + }; +}); \ No newline at end of file diff --git a/test/data/div.rt.globals.js b/test/data/div.rt.globals.js new file mode 100644 index 0000000..8f6d724 --- /dev/null +++ b/test/data/div.rt.globals.js @@ -0,0 +1,3 @@ +var div = function () { + return React.createElement('div', {}); +}; \ No newline at end of file diff --git a/test/src/test.js b/test/src/test.js index cf8de6f..4039879 100644 --- a/test/src/test.js +++ b/test/src/test.js @@ -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, '>')