component-builder-handlebars
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -14,3 +14,6 @@ /* | ||
*/ | ||
var fs = require('fs'); | ||
var exists = fs.existsSync; | ||
var read = fs.readFileSync; | ||
var path = require('path'); | ||
@@ -21,2 +24,3 @@ var Handlebars = require('handlebars'); | ||
* Precompile Handlebars templates and partials. | ||
* | ||
* @param {Object} opts - extname: define the handlebars extension name. | ||
@@ -27,86 +31,69 @@ * - partialRegex: Regex to determine if the file is a partial. | ||
*/ | ||
module.exports = function(opts) { | ||
opts = opts || {}; | ||
// by default the extension name is .hbs | ||
var extname = opts.extname || '.hbs'; | ||
// by default the partial file name have to start with _ | ||
var partialRegex = opts.partialRegex || /^_/; | ||
var isHandlebarsTemplate = function(file) { | ||
return extname === path.extname(file); | ||
}; | ||
return function(build, done){ | ||
setImmediate(done); | ||
var list = build.components; | ||
var partials = []; | ||
var componentName = list[0].name; | ||
var removePartialPrefix = function(partial) { | ||
return partial.replace(partialRegex, ''); | ||
}; | ||
build.map('templates', function(file, conf){ | ||
var ext = path.extname(file.filename); | ||
if (extname != ext) return file; | ||
return function(builder) { | ||
builder.hook('before scripts', function(pkg, complete) { | ||
var tpl = pkg.config.templates; | ||
var componentName = pkg.config.name; | ||
var runtimeFilepath, runtime; | ||
var absolutepath = conf.path(file.filename); | ||
var filename = path.basename(absolutepath, extname); | ||
// we include the runtime only for the root. | ||
if (pkg.root) { | ||
// add handlebars runtime script. | ||
// If the file, in the Handlebars module, does not exists | ||
// we use the runtime file from /lib. | ||
runtimeFilepath = path.join(__dirname, '../node_modules/handlebars/dist/handlebars.runtime.js'); | ||
if (!fs.existsSync(runtimeFilepath)) { | ||
runtimeFilepath = path.join(__dirname, '/handlebars.runtime.js'); | ||
} | ||
var src = read(absolutepath, 'utf-8'); | ||
var compiled = Handlebars.precompile(src); | ||
var output = 'Handlebars.template(' + compiled + ')'; | ||
runtime = fs.readFileSync(runtimeFilepath); | ||
builder.addFile('scripts', 'handlebars.runtime.js', runtime + 'window.Handlebars = Handlebars;'); | ||
// auto-invoke the module | ||
builder.append('require("' + componentName + '/handlebars.runtime");'); | ||
if (partialRegex.test(filename)) { | ||
var modulePath = file.filename.replace(filename + extname, ''); | ||
filename = filename.replace(partialRegex, ''); | ||
partials.push('Handlebars.registerPartial("' + conf.name + '/' + modulePath + filename + '", ' + output + ');'); | ||
} | ||
if (!tpl) { | ||
return complete(); | ||
} | ||
var script = {}; | ||
script.filename = file.filename.replace(extname, '.js'); | ||
script.contents = 'module.exports = ' + output; | ||
conf.scripts = conf.scripts || []; | ||
conf.scripts.push(script); | ||
tpl = tpl.filter(isHandlebarsTemplate); | ||
return file; | ||
}); | ||
// list of partials that we will process after. | ||
var partials = []; | ||
build.handlebars = runtime(list[0].name, partials); | ||
} | ||
} | ||
// precompile handlebars templates and partials. | ||
tpl.forEach(function(modulePath) { | ||
var absolutepath = pkg.path(modulePath); | ||
var filename = path.basename(absolutepath, extname); | ||
/** | ||
* Handlebars runtime with partials. | ||
* | ||
* @param {String} name | ||
* @param {String[]} partials | ||
* @return {String} | ||
* @api private | ||
*/ | ||
// precompile handlebars templates. | ||
var src = fs.readFileSync(absolutepath, 'utf-8'); | ||
var compiled = Handlebars.precompile(src); | ||
var output = 'Handlebars.template(' + compiled + ')'; | ||
function runtime(name, partials){ | ||
var runtimeFilepath = path.join(__dirname, '../node_modules/handlebars/dist/handlebars.runtime.js'); | ||
if (!exists(runtimeFilepath)) runtimeFilepath = path.join(__dirname, '/handlebars.runtime.js'); | ||
if (partialRegex.test(filename)) { | ||
// add namespace to the partial. | ||
modulePath = modulePath.replace(filename + extname, ''); | ||
filename = removePartialPrefix(filename); | ||
partials.push('Handlebars.registerPartial("' + componentName + '/' + modulePath + filename + '", ' + output + ');'); | ||
var script = read(runtimeFilepath, 'utf-8'); | ||
script += 'this.Handlebars = window.Handlebars = Handlebars;'; | ||
return; | ||
} | ||
if (!partials.length) return script; | ||
// add the template as a module. | ||
modulePath = modulePath.replace(extname, '.js'); | ||
pkg.addFile('scripts', modulePath, 'module.exports = ' + output); | ||
}); | ||
script += ';(function(){\n'; | ||
partials.forEach(function(p){ | ||
script += p; | ||
}); | ||
script += '\n})();' | ||
if (partials.length === 0) { | ||
return complete(); | ||
} | ||
// add all partials into the same module 'handlebars.partials.js'. | ||
pkg.addFile('scripts', 'handlebars.partials.js', partials.join('\n')); | ||
// auto-invoke the module to automatically register partials. | ||
builder.append('require("' + componentName + '/handlebars.partials");'); | ||
return complete(); | ||
}); | ||
}; | ||
}; | ||
return script; | ||
} |
{ | ||
"name": "component-builder-handlebars", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Builder.js plugin to precompile Handlebars templates", | ||
@@ -30,5 +30,5 @@ "author": "Antoine Lehurt <keuwah@gmail.com>", | ||
"should": "~1.2.1", | ||
"component-builder": "~0.8.1", | ||
"component-builder": "~0.12.0", | ||
"jshint": "~0.9.1" | ||
} | ||
} | ||
} |
@@ -10,2 +10,2 @@ { | ||
] | ||
} | ||
} |
@@ -0,1 +1,2 @@ | ||
var Builder = require('component-builder'); | ||
@@ -8,3 +9,3 @@ var plugin = require('../lib/plugin'); | ||
var req = function(res, moduleName) { | ||
return 'var window = "";' + res.require + res.js + '; require("' + moduleName + '")'; | ||
return 'var window = "";' + res.handlebars + res.requirejs + res.scripts + res.templates + '; require("' + moduleName + '")'; | ||
}; | ||
@@ -15,2 +16,6 @@ | ||
builder.use(plugin()); | ||
builder.use(Builder.commonjs('scripts')); | ||
builder.use(Builder.commonjs('templates')); | ||
builder.use(Builder.concat('scripts')); | ||
builder.use(Builder.concat('templates')); | ||
@@ -35,4 +40,8 @@ builder.build(function(err, res) { | ||
var builder = new Builder('test/fixtures/with-dependencies'); | ||
builder.addLookup('test/fixtures/with-dependencies/local'); | ||
builder.path('local'); | ||
builder.use(plugin()); | ||
builder.use(Builder.commonjs('scripts')); | ||
builder.use(Builder.commonjs('templates')); | ||
builder.use(Builder.concat('scripts')); | ||
builder.use(Builder.concat('templates')); | ||
@@ -55,2 +64,6 @@ builder.build(function(err, res) { | ||
builder.use(plugin()); | ||
builder.use(Builder.commonjs('scripts')); | ||
builder.use(Builder.commonjs('templates')); | ||
builder.use(Builder.concat('scripts')); | ||
builder.use(Builder.concat('templates')); | ||
@@ -69,5 +82,9 @@ builder.build(function(err, res) { | ||
var builder = new Builder('test/fixtures/tpl-only-in-dependencies'); | ||
builder.addLookup('test/fixtures/tpl-only-in-dependencies/local'); | ||
builder.addSourceURLs(); | ||
builder.path('local'); | ||
builder.development(); | ||
builder.use(plugin()); | ||
builder.use(Builder.commonjs('scripts')); | ||
builder.use(Builder.commonjs('templates')); | ||
builder.use(Builder.concat('scripts')); | ||
builder.use(Builder.concat('templates')); | ||
@@ -84,2 +101,2 @@ builder.build(function(err, res) { | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30
417
17697