Skip to content

Commit

Permalink
added: support for include tags
Browse files Browse the repository at this point in the history
  • Loading branch information
emaphp committed Oct 20, 2014
1 parent 95ddc5b commit cb462db
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
36 changes: 36 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
var Handlebars = require('handlebars');
var loaderUtils = require('loader-utils');
var path = require('path');
var fs = require('fs');

function resolveContent(content, root) {
var includeRegex = /<!--include\s+([\/\w\.]*?[\w]+\.[\w]+)-->/g;
var matches = includeRegex.exec(content);

while (matches != null) {
var file = loaderUtils.urlToRequest(matches[1]);
var basename = path.basename(file);
var dirname = path.join(root, path.dirname(file));
var rawContent = readFile(basename, dirname);
content = content.replace(matches[0], rawContent);
matches = includeRegex.exec(content);
}

return content;
}

function readFile(filepath, root) {
var self = readFile;

if (typeof(self.buffer) == "undefined") {
self.buffer = {};
}

if (filepath in self.buffer) {
return self.buffer[filepath];
}

var content = resolveContent(fs.readFileSync(path.join(root, filepath), 'utf8'), root);
self.buffer[filepath] = content;
return self.buffer[filepath];
}

module.exports = function(content) {
this.cacheable && this.cacheable();
var callback = this.async();
content = resolveContent(content, this.context);
var fn = Handlebars.precompile(content);
callback(null, "var Handlebars = require('" + require.resolve('handlebars') + "');\n" +
"module.exports = (Handlebars[\"default\"] || Handlebars).template(" + fn + ");");
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": "handlebars-template-loader",
"version": "0.1.0",
"version": "0.2.0",
"description": "A Handlebars template loader for Webpack",
"main": "index.js",
"homepage": "https://github.com/emaphp/handlebars-template-loader",
Expand All @@ -16,7 +16,8 @@
"url": "https://github.com/emaphp/handlebars-template-loader.git"
},
"dependencies": {
"handlebars": "^2.0.0"
"handlebars": "^2.0.0",
"loader-utils": "^0.2.5"
},
"keywords": [
"handlebars",
Expand Down

0 comments on commit cb462db

Please sign in to comment.