Skip to content

Commit cf3c0b0

Browse files
committed
added: support for the 'parseDynamicRoutes' argument
1 parent 9df6669 commit cf3c0b0

8 files changed

Lines changed: 78 additions & 11 deletions

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ An Underscore.js and Lodash template loader for Webpack
66
### Changelog
77

88
<br>
9-
* 0.7.1: FIX: Check if attribute contains a template expression before replacing it.
9+
* 0.7.2: Support for the `parseDynamicRoutes` argument (deactivated by default).
1010

1111
### Installation
1212

@@ -229,6 +229,39 @@ module.exports = {
229229
};
230230
```
231231

232+
Dynamic attributes won't be afected by this behaviour by default.
233+
234+
```html
235+
<!-- Ignore "root" argument if attribute contains a template expression -->
236+
<img src="/img/cat-<%- currentCat.url %>.png" class="cat-img">
237+
```
238+
239+
In order to append the root directory you'll need to specify the `parseDynamicRoutes` argument.
240+
241+
```javascript
242+
module.exports = {
243+
//...
244+
245+
module: {
246+
loaders: [
247+
{
248+
test: /\.html$/,
249+
loader: "underscore-template-loader",
250+
query: {
251+
root: "myapp",
252+
parseDynamicRoutes: true
253+
}
254+
}
255+
]
256+
}
257+
};
258+
```
259+
260+
```html
261+
<!-- Attribute now translates to "myapp/img/cat-<%- currentCat.url %>.png" -->
262+
<img src="/img/cat-<%- currentCat.url %>.png" class="cat-img">
263+
```
264+
232265
### Macros
233266

234267
Macros allow additional features like including templates or inserting custom text in compiled templates.

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ module.exports = function(content) {
2121
parseMacros = true,
2222
engine = false,
2323
withImports = false,
24-
attributes = ['img:src'];
24+
attributes = ['img:src'],
25+
parseDynamicRoutes = false;
2526

2627
// Parse arguments
2728
var query = this.query instanceof Object ? this.query : loaderUtils.parseQuery(this.query);
@@ -66,6 +67,11 @@ module.exports = function(content) {
6667
var filenameRelative = path.relative(query.prependFilenameComment, this.resource);
6768
content = "\n<!-- " + filenameRelative + " -->\n" + content;
6869
}
70+
71+
// Check if dynamic routes must be parsed
72+
if (query.parseDynamicRoutes !== undefined) {
73+
parseDynamicRoutes = !!query.parseDynamicRoutes;
74+
}
6975
}
7076

7177
// Include additional macros
@@ -84,7 +90,7 @@ module.exports = function(content) {
8490
// Parse attributes
8591
var attributesContext = attributeParser(content, function (tag, attr) {
8692
return attributes.indexOf(tag + ':' + attr) != -1;
87-
}, 'ATTRIBUTE', root);
93+
}, 'ATTRIBUTE', root, parseDynamicRoutes);
8894
content = attributesContext.replaceMatches(content);
8995

9096
// Compile template

lib/attributeParser.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var isTemplate = function (content) {
3737
};
3838

3939
// AttributeContext class
40-
var AttributeContext = function (isRelevantTagAttr, usid, root) {
40+
var AttributeContext = function (isRelevantTagAttr, usid, root, parseDynamicRoutes) {
4141
this.currentDirective = null;
4242
this.matches = [];
4343
this.isRelevantTagAttr = isRelevantTagAttr;
@@ -47,6 +47,7 @@ var AttributeContext = function (isRelevantTagAttr, usid, root) {
4747
};
4848
this.data = {};
4949
this.root = root;
50+
this.parseDynamicRoutes = parseDynamicRoutes;
5051
};
5152

5253
AttributeContext.prototype.replaceMatches = function(content) {
@@ -56,8 +57,10 @@ AttributeContext.prototype.replaceMatches = function(content) {
5657

5758
this.matches.forEach(function (match) {
5859
if (isTemplate(match.value)) {
59-
// Replate template if a "root" option has been defined
60-
if (pathIsAbsolute(match.value) && self.root != undefined) {
60+
// Replace attribute value
61+
// This is used if it contains a template expression and both the "root" and "parseDynamicRoutes"
62+
// were defined
63+
if (pathIsAbsolute(match.value) && self.root !== undefined) {
6164
var x = content.pop();
6265
content.push(x.substr(match.start + match.length));
6366
content.push(match.expression);
@@ -126,7 +129,7 @@ var processMatch = function (match, strUntilValue, name, value, index) {
126129

127130
// Try and set "root" directory when a dynamic attribute is found
128131
if (isTemplate(value)) {
129-
if (pathIsAbsolute(value) && self.root != undefined) {
132+
if (pathIsAbsolute(value) && self.root !== undefined && self.parseDynamicRoutes) {
130133
// Generate new value for replacement
131134
expression = loaderUtils.urlToRequest(value, self.root);
132135
}
@@ -164,7 +167,7 @@ var specs = {
164167

165168
var parser = new Parser(specs);
166169

167-
module.exports = function parse(html, isRelevantTagAttr, usid, root) {
168-
var context = new AttributeContext(isRelevantTagAttr, usid, root);
170+
module.exports = function parse(html, isRelevantTagAttr, usid, root, parseDynamicRoutes) {
171+
var context = new AttributeContext(isRelevantTagAttr, usid, root, parseDynamicRoutes);
169172
return parser.parse('outside', html, context);
170173
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "underscore-template-loader",
3-
"version": "0.7.1",
3+
"version": "0.7.2",
44
"description": "An Underscore and Lodash template loader for Webpack",
55
"main": "index.js",
66
"homepage": "https://github.com/emaphp/underscore-template-loader",

test/loaderTest.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ describe('loader', function () {
155155
done();
156156
});
157157
});
158+
159+
it('should parse dynamic attributes with parseDynamicRoutes', function (done) {
160+
testTemplate(loader, 'dynamic-attribute-with-parseDynamicRoutes.html', {
161+
query: {
162+
root: 'myapp',
163+
parseDynamicRoutes: true
164+
}
165+
}, function (output) {
166+
assert.equal(output, loadOutput('dynamic-attribute-with-parseDynamicRoutes.txt'));
167+
done();
168+
});
169+
});
158170

159171
// FIXME: Changing the underscore tags changes it globally
160172
it('should allow custom underscore tags', function (done) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<img src="img/cat-<%- currentCat.url %>.png" class="cat-img">
2+
<img src="/img/dog-<%- currentDog.url %>.png" class="dog-img">
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = function(obj){
2+
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
3+
with(obj||{}){
4+
__p+='<img src="img/cat-'+
5+
((__t=( currentCat.url ))==null?'':_.escape(__t))+
6+
'.png" class="cat-img">\n<img src="myapp/img/dog-'+
7+
((__t=( currentDog.url ))==null?'':_.escape(__t))+
8+
'.png" class="dog-img">\n';
9+
}
10+
return __p;
11+
};

test/templates/output/dynamic-attribute-with-root.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments
33
with(obj||{}){
44
__p+='<img src="img/cat-'+
55
((__t=( currentCat.url ))==null?'':_.escape(__t))+
6-
'.png" class="cat-img">\n<img src="/bar/img/dog-'+
6+
'.png" class="cat-img">\n<img src="/img/dog-'+
77
((__t=( currentDog.url ))==null?'':_.escape(__t))+
88
'.png" class="dog-img">\n';
99
}

0 commit comments

Comments
 (0)