underscore-template-loader
Advanced tools
Comparing version 0.4.0 to 0.4.1
84
index.js
@@ -9,38 +9,38 @@ var path = require('path'); | ||
var _ = require('underscore'); | ||
} catch(e) { | ||
} catch (e) { | ||
var _ = require('lodash'); | ||
} | ||
module.exports = function () { | ||
var includeRegex = /<@include\s+([\/\w\.]*?[\w]+\.[\w]+)>/g; | ||
module.exports = function() { | ||
var includeRegex = /<@include\s+([\/\w\.]*?[\w]+\.[\w]+)>/g; | ||
// Returns a template file content | ||
var readFile = function(filepath, root) { | ||
var self = readFile; | ||
self.buffer = self.buffer || {}; | ||
var readFile = function(filepath, root) { | ||
var self = readFile; | ||
self.buffer = self.buffer || {}; | ||
if (filepath in self.buffer) { | ||
return self.buffer[filepath]; | ||
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 content = readContent(fs.readFileSync(path.join(root, filepath), 'utf8'), root); | ||
self.buffer[filepath] = content; | ||
return self.buffer[filepath]; | ||
}; | ||
// Parses an external file content | ||
var readContent = function(content, root) { | ||
var matches = includeRegex.exec(content); | ||
var readContent = function(content, root) { | ||
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); | ||
} | ||
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 content; | ||
}; | ||
return function(content) { | ||
return function(content) { | ||
var query = loaderUtils.parseQuery(this.query); | ||
@@ -51,6 +51,6 @@ var root = query.root; | ||
if (_.isObject(query)) { | ||
// Apply template settings | ||
_.each(_.pick(query, 'interpolate', 'escape', 'evaluate'), function (value, key) { | ||
_.templateSettings[key] = new RegExp(value, 'g'); | ||
}); | ||
// Apply template settings | ||
_.each(_.pick(query, 'interpolate', 'escape', 'evaluate', 'attributes', 'prependFilenameComment'), function(value, key) { | ||
_.templateSettings[key] = new RegExp(value, 'g'); | ||
}); | ||
@@ -66,6 +66,6 @@ // Set tag+attribute to parse for external resources | ||
} | ||
} | ||
} | ||
// Generates a random string for further proccessing | ||
var randomIdent = function () { | ||
var randomIdent = function() { | ||
return "@@@URL" + Math.random() + "@@@"; | ||
@@ -75,3 +75,3 @@ }; | ||
// Obtain external resource links | ||
var links = attributeParser(content, function (tag, attr) { | ||
var links = attributeParser(content, function(tag, attr) { | ||
return attributes.indexOf(tag + ':' + attr) >= 0; | ||
@@ -84,3 +84,3 @@ }); | ||
content = [content]; | ||
links.forEach(function (link) { | ||
links.forEach(function(link) { | ||
// Ignore absolute paths | ||
@@ -115,11 +115,19 @@ if (/^\//.exec(link.value) && root == false) { | ||
this.cacheable && this.cacheable(); | ||
var callback = this.async(); | ||
this.cacheable && this.cacheable(); | ||
var callback = this.async(); | ||
// Read file content | ||
content = readContent(content, this.context); | ||
content = readContent(content, this.context); | ||
// Prepend a html comment with the filename in it | ||
if (query.prependFilenameComment) { | ||
var filename = loaderUtils.getRemainingRequest(this); | ||
var filenameRelative = path.relative(query.prependFilenameComment, filename); | ||
content = "\n<!-- " + filenameRelative + " -->\n" + content; | ||
} | ||
// Replace random generated strings with require | ||
var source = _.template(content).source; | ||
content = source.replace(/@@@URL[0-9\.]+@@@/g, function (match) { | ||
content = source.replace(/@@@URL[0-9\.]+@@@/g, function(match) { | ||
if (!data[match]) { | ||
@@ -132,6 +140,6 @@ return match; | ||
callback(null, "module.exports = " + content + ";"); | ||
}; | ||
callback(null, "module.exports = " + content + ";"); | ||
}; | ||
}(); | ||
module.exports._ = _; | ||
module.exports._ = _; |
{ | ||
"name": "underscore-template-loader", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "An Underscore and Lodash template loader for Webpack", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
var Parser = require("fastparse"); | ||
var processMatch = function (match, strUntilValue, name, value, index) { | ||
var processMatch = function(match, strUntilValue, name, value, index) { | ||
if (!this.isRelevantTagAttr(this.currentTag, name)) { | ||
@@ -17,3 +17,3 @@ return; | ||
outside: { | ||
"<([a-zA-Z\\-:]+)\\s*": function (match, tagName) { | ||
"<([a-zA-Z\\-:]+)\\s*": function(match, tagName) { | ||
this.currentTag = tagName; | ||
@@ -24,3 +24,3 @@ return 'inside'; | ||
inside: { | ||
"\\s+": true, // Eat up whitespace | ||
"\\s+": true, // Eat up whitespace | ||
">": 'outside', // End of attributes | ||
@@ -33,3 +33,3 @@ "(([a-zA-Z\\-]+)\\s*=\\s*\")([^\"]*)\"": processMatch, | ||
module.exports = function parse (html, isRelevantTagAttr) { | ||
module.exports = function parse(html, isRelevantTagAttr) { | ||
return parser.parse('outside', html, { | ||
@@ -40,2 +40,2 @@ currentTag: null, | ||
}).matches; | ||
}; | ||
}; |
@@ -46,2 +46,22 @@ underscore-template-loader | ||
<br/> | ||
####Prepending filename comment | ||
When debugging a large single page app with the DevTools, it's often hard to find the template that contains a bug. With the following config a HTML comment is prepended to the template with the relative path in it (e.g. `<!-- view/user/edit.html -->`). | ||
```javascript | ||
module.exports = { | ||
//... | ||
loaders: [ | ||
//... | ||
{ | ||
test: /\.html$/, | ||
loader: "underscore-template-loader", | ||
query: { | ||
prependFilenameComment: __dirname, | ||
} | ||
} | ||
] | ||
}; | ||
``` | ||
<br/> | ||
####Template settings | ||
@@ -48,0 +68,0 @@ |
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
11962
142
211