-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters