diff --git a/README.md b/README.md index 78caf5e..afdac06 100644 --- a/README.md +++ b/README.md @@ -25,8 +25,9 @@ gulp.task('html2js', function () { base: path.join(__dirname, 'html'), //The base path of HTML templates createObj: true, // Indicate wether to define the global object that stores // the global template strings - objName: 'TEMPLATES' // Name of the global template store variable + objName: 'TEMPLATES', // Name of the global template store variable //say the converted string for myTemplate.html will be saved to TEMPLATE['myTemplate.html'] + noObject: false // If set to true, output will be TEMPLATE = ... instead of TEMPLATE['myTemplate.html'] })) .pipe(rename({extname: '.js'})) .pipe(gulp.dest('templates/')); //Output folder diff --git a/lib/compile.js b/lib/compile.js index f8c3775..5c85d5b 100644 --- a/lib/compile.js +++ b/lib/compile.js @@ -34,7 +34,7 @@ var getContent = function(contents, quoteChar, indentString, htmlmin) { }; // compile a template to an angular module -var compileTemplate = function(objName, createObj, templateName, contents, quoteChar, indentString, useStrict, htmlmin) { +var compileTemplate = function(objName, createObj, templateName, contents, quoteChar, indentString, useStrict, htmlmin, noObject) { var content = getContent(contents, quoteChar, indentString, htmlmin); var doubleIndent = indentString + indentString; @@ -46,8 +46,13 @@ var compileTemplate = function(objName, createObj, templateName, contents, quote '}\n'; } - result += objName + '[\'' + templateName + '\'] = ' + - quoteChar + content + quoteChar + '; '; + if(noObject){ + result += objName + ' = ' + + quoteChar + content + quoteChar + '; '; + }else{ + result += objName + '[\'' + templateName + '\'] = ' + + quoteChar + content + quoteChar + '; '; + } return result; }; @@ -60,13 +65,24 @@ module.exports = function(file, options, callback) { options.target= options.target || 'js'; options.htmlmin= options.htmlmin || {}; options.useStrict= options.useStrict || false; + options.noObject= options.noObject || false; function getModule(filepath) { var templateName = path.relative(options.base, filepath); if (options.target === 'js') { - return compileTemplate(options.objName, options.createObj, templateName, file.contents.toString(), options.quoteChar, options.indentString, options.useStrict, options.htmlmin); + return compileTemplate( + options.objName, + options.createObj, + templateName, + file.contents.toString(), + options.quoteChar, + options.indentString, + options.useStrict, + options.htmlmin, + options.noObject + ); } else { gutil.log('Unknow target "' + options.target + '" specified'); } diff --git a/package.json b/package.json index cb64080..c61dc9e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gulp-html2string", - "version": "0.1.1", + "version": "0.1.2", "description": "Converts static HTML templates to JavaScript strings.", "main": "index.js", "scripts": { @@ -23,7 +23,8 @@ ], "contributors": [ "Xiyang Chen ", - "xvfeng" + "xvfeng", + "Juan Camilo O " ], "license": "GPLv3", "bugs": { diff --git a/test/test.js b/test/test.js index d3532e2..7922dcf 100644 --- a/test/test.js +++ b/test/test.js @@ -30,4 +30,27 @@ stream.end(); }); + it('should not create an object if noObject is set', function(cb){ + var stream = templateStrBuilder({objName: 'TEH_TEMPLATES', createObj: true, noObject:true, base: __dirname}); + + stream.on('data', function (data) { + assert.equal(data.contents.toString(), + 'if(typeof TEH_TEMPLATES === \'undefined\') {var TEH_TEMPLATES = {};}\n' + + 'TEH_TEMPLATES = "\\n" +' + + '\n "\\n" +' + + '\n "Test, \'single quote\', \\\"double quote\\\"\\n" +' + + '\n "\\n" +' + + '\n ""; '); + cb(); + }); + + stream.write(new gutil.File({ + path: join(__dirname, './test-template.html'), + contents: fs.readFileSync(join(__dirname, './test-template.html')) + })); + + stream.end(); + }); + })(); +