Skip to content

Commit a5fe53a

Browse files
authored
Merge pull request #80 from Justinidlerz/master
only ${require('..')} to be translate.
2 parents 10280c2 + 51efc66 commit a5fe53a

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,22 @@ require("html?interpolate!./file.html");
122122
```html
123123
<img src="${require(`./images/gallery.png`)}">
124124

125+
<div>${require('./components/gallery.html')}</div>
126+
```
127+
And if you only want to use `require` in template and any other `${}` are not to be translate you can set `interpolate` flag to `require`, like so:
128+
129+
```js
130+
require("html?interpolate=require!./file.ftl");
131+
```
132+
133+
```html
134+
135+
<#list list as list>
136+
<a href="${list.href!}" />${list.name}</a>
137+
</#list>
138+
139+
<img src="${require(`./images/gallery.png`)}">
140+
125141
<div>${require('./components/gallery.html')}</div>
126142
```
127143

index.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,35 @@ module.exports = function(content) {
6565
});
6666
content.reverse();
6767
content = content.join("");
68+
69+
if (config.interpolate === 'require'){
70+
71+
var reg = /\$\{require\([^)]*\)\}/g;
72+
var result;
73+
var reqList = [];
74+
while(result = reg.exec(content)){
75+
reqList.push({
76+
length : result[0].length,
77+
start : result.index,
78+
value : result[0]
79+
})
80+
}
81+
reqList.reverse();
82+
content = [content];
83+
reqList.forEach(function(link) {
84+
var x = content.pop();
85+
do {
86+
var ident = randomIdent();
87+
} while(data[ident]);
88+
data[ident] = link.value.substring(11,link.length - 3)
89+
content.push(x.substr(link.start + link.length));
90+
content.push(ident);
91+
content.push(x.substr(0, link.start));
92+
});
93+
content.reverse();
94+
content = content.join("");
95+
}
96+
6897
if(typeof config.minimize === "boolean" ? config.minimize : this.minimize) {
6998
var minimizeOptions = assign({}, config);
7099

@@ -90,14 +119,15 @@ module.exports = function(content) {
90119
content = htmlMinifier.minify(content, minimizeOptions);
91120
}
92121

93-
if(config.interpolate) {
122+
if(config.interpolate && config.interpolate !== 'require') {
94123
content = compile('`' + content + '`').code;
95124
} else {
96125
content = JSON.stringify(content);
97126
}
98-
99-
return "module.exports = " + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) {
127+
128+
return "module.exports = " + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) {
100129
if(!data[match]) return match;
101130
return '" + require(' + JSON.stringify(loaderUtils.urlToRequest(data[match], root)) + ') + "';
102131
}) + ";";
132+
103133
}

test/loaderTest.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,11 @@ describe("loader", function() {
119119
'module.exports = "<img src=\\"" + ("Hello " + (1 + 1)) + "\\">";'
120120
);
121121
});
122+
it("should enable interpolations when using interpolate=require flag and only require function to be translate", function() {
123+
loader.call({
124+
query: "?interpolate=require"
125+
}, '<a href="${list.href}"><img src="${require("./test.jpg")}" /></a>').should.be.eql(
126+
'module.exports = "<a href=\\"${list.href}\\"><img src=\\"" + require("./test.jpg") + "\\" /></a>";'
127+
);
128+
});
122129
});

0 commit comments

Comments
 (0)