underscore-template-loader
Advanced tools
Comparing version 0.7.3 to 0.8.0
@@ -37,3 +37,3 @@ var Parser = require("fastparse"); | ||
this.matches.reverse(); | ||
this.matches.forEach(function (match) { | ||
@@ -52,3 +52,3 @@ do { | ||
content.reverse(); | ||
content.reverse(); | ||
return content.join(''); | ||
@@ -112,2 +112,12 @@ }; | ||
var processObjectArg = function (match, value, index, length) { | ||
if (!this.currentMacro) return; | ||
this.currentMacro.args.push({ | ||
start: index + value.length, | ||
index: index, | ||
length: length, | ||
value: JSON.parse(value) | ||
}); | ||
}; | ||
// Parser configuration | ||
@@ -142,2 +152,3 @@ var specs = { | ||
"\\s*(true|false)\\s*": processBooleanArg, | ||
"\\s*({.+})\\s*": processObjectArg, | ||
"\\s+": true | ||
@@ -152,2 +163,2 @@ } | ||
return parser.parse('outside', html, context); | ||
}; | ||
}; |
@@ -13,6 +13,10 @@ var loaderUtils = require('loader-utils'); | ||
// Used to translate require macros to override arguments | ||
var objExtend = function (args, obj) {args = Array.prototype.slice.call(args);var _a = args.slice(1); _a.unshift(Object.assign(obj, args[0])); return _a;}; | ||
// Default macros | ||
module.exports = { | ||
require: function (resourcePath) { | ||
return "require(" + JSON.stringify(loaderUtils.urlToRequest(resourcePath)) + ").apply(null,arguments)"; | ||
require: function (resourcePath, args) { | ||
var argsExpr = args ? '(' + objExtend + ')' + '(arguments, ' + JSON.stringify(args) + ')' : 'arguments'; | ||
return "require(" + JSON.stringify(loaderUtils.urlToRequest(resourcePath)) + ").apply(null," + argsExpr + ")"; | ||
}, | ||
@@ -29,3 +33,3 @@ | ||
nl: function () { | ||
nl: function (times) { | ||
var str = strRepeat('\\n', typeof(times) == 'undefined' ? 1 : parseInt(times)); | ||
@@ -32,0 +36,0 @@ return "'" + str + "'"; |
{ | ||
"name": "underscore-template-loader", | ||
"version": "0.7.3", | ||
"version": "0.8.0", | ||
"description": "An Underscore and Lodash template loader for Webpack", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,3 +9,3 @@ underscore-template-loader | ||
<br> | ||
* 0.7.2: Support for the `parseDynamicRoutes` argument (deactivated by default). | ||
* 0.8: Macros now support object literals as arguments | ||
@@ -285,2 +285,10 @@ ### Installation | ||
This macro also supports an object literal as an additional argument. | ||
```html | ||
<div class="top-section"> | ||
@require('header.html', {"title": "First Section"}) | ||
</div> | ||
``` | ||
#### The *include* macro | ||
@@ -367,3 +375,3 @@ | ||
Macros can accept an arbitrary number of arguments. Only boolean, strings and numeric types are supported. | ||
Macros can accept an arbitrary number of arguments of different types: boolean, strings, numbers an object literals are supported. | ||
@@ -370,0 +378,0 @@ ```javascript |
@@ -95,2 +95,9 @@ var fs = require('fs'); | ||
it('should be possible to require a template with custom args', function (done) { | ||
testTemplate(loader, 'require_with_args.html', {}, function (output) { | ||
assert.equal(output, loadOutput('require_with_args.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should be possible to include a template', function (done) { | ||
@@ -97,0 +104,0 @@ testTemplate(loader, 'include.html', {}, function (output) { |
@@ -11,2 +11,3 @@ var fs = require('fs'); | ||
var loadOutput = require('./lib/loadOutput'); | ||
var toText = require('./lib/toText.js'); | ||
@@ -88,2 +89,18 @@ function testTemplate(loader, template, options, testFn) { | ||
it('should receive object arguments', function(done) { | ||
testTemplate(loader, 'macro_object_args.html', { | ||
options: { | ||
macros: { | ||
object_test: function(arg) { | ||
assert.typeOf(arg, 'object'); | ||
return '"' + toText(arg) + '"'; | ||
} | ||
} | ||
} | ||
}, function(output) { | ||
assert.equal(output.trimRight(), loadOutput('macro_object_args.txt').trimRight()); | ||
done(); | ||
}); | ||
}); | ||
it('should receive argument list', function (done) { | ||
@@ -97,3 +114,3 @@ testTemplate(loader, 'macro_argument_list.html', { | ||
assert.typeOf(third, 'number'); | ||
var output = ''; | ||
@@ -110,3 +127,3 @@ for (var i = 0; i < arguments.length; i++) { | ||
assert.typeOf(third, 'boolean'); | ||
var output = ''; | ||
@@ -123,3 +140,3 @@ for (var i = 0; i < arguments.length; i++) { | ||
assert.typeOf(third, 'string'); | ||
var output = ''; | ||
@@ -191,2 +208,2 @@ for (var i = 0; i < arguments.length; i++) { | ||
}); | ||
}); | ||
}); |
54783
61
874
427