Skip to content

Commit

Permalink
handlebars as a peer dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
emaphp committed Jan 22, 2015
1 parent 33b26bd commit d6fa4c4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 47 deletions.
87 changes: 42 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,47 +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 + ");");
};

module.exports.Handlebars = Handlebars;
module.exports = function() {
var loaderUtils = require('loader-utils');
var path = require('path');
var fs = require('fs');
var readFile = function(filepath, root) {
var self = readFile;
self.buffer = self.buffer || {};

if (filepath in self.buffer)
return self.buffer[filepath];
var content = readContent(fs.readFileSync(path.join(root, filepath), 'utf8'), root);
self.buffer[filepath] = content;
return self.buffer[filepath];
};
var readContent = function(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 rawContent = readFile(path.basename(file), path.join(root, path.dirname(file)));
content = content.replace(matches[0], rawContent);
matches = includeRegex.exec(content);
}

return content;
};

return function(content) {
this.cacheable && this.cacheable();
var callback = this.async();
content = readContent(content, this.context);
var fn = Handlebars.precompile(content);
callback(null, "var Handlebars = require('" + require.resolve('handlebars') + "');\n" +
"module.exports = (Handlebars[\"default\"] || Handlebars).template(" + fn + ");");
};
}();

module.exports.Handlebars = Handlebars;
6 changes: 4 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.2.0",
"version": "0.2.1",
"description": "A Handlebars template loader for Webpack",
"main": "index.js",
"homepage": "https://github.com/emaphp/handlebars-template-loader",
Expand All @@ -16,9 +16,11 @@
"url": "https://github.com/emaphp/handlebars-template-loader.git"
},
"dependencies": {
"handlebars": "^2.0.0",
"loader-utils": "^0.2.5"
},
"peerDependencies": {
"handlebars": "^2.0.0"
},
"keywords": [
"handlebars",
"template",
Expand Down

0 comments on commit d6fa4c4

Please sign in to comment.