From cb462db95c566a6f456dc5f2912f588c0d2150f3 Mon Sep 17 00:00:00 2001 From: emaphp Date: Mon, 20 Oct 2014 14:35:33 -0300 Subject: [PATCH] added: support for include tags --- index.js | 36 ++++++++++++++++++++++++++++++++++++ package.json | 5 +++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 6adeb3b..73d65a3 100644 --- a/index.js +++ b/index.js @@ -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 = //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 + ");"); diff --git a/package.json b/package.json index 4520cfe..9567760 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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",