Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 20 additions & 4 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
};
Expand All @@ -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');
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -23,7 +23,8 @@
],
"contributors": [
"Xiyang Chen <settinghead@gmail.com>",
"xvfeng"
"xvfeng",
"Juan Camilo O <jcoc611@gmail.com>"
],
"license": "GPLv3",
"bugs": {
Expand Down
23 changes: 23 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<html>\\n" +' +
'\n "<head></head>\\n" +' +
'\n "<body class=\\\"hello\\\">Test, \'single quote\', \\\"double quote\\\"<body>\\n" +' +
'\n "</html>\\n" +' +
'\n ""; ');
cb();
});

stream.write(new gutil.File({
path: join(__dirname, './test-template.html'),
contents: fs.readFileSync(join(__dirname, './test-template.html'))
}));

stream.end();
});

})();