Skip to content

Commit d885413

Browse files
committed
Merge pull request #31 from sheerun/master
Add support for es6 interpolation
2 parents 4442790 + 3a7053f commit d885413

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ require("html?root=.!./fileB.html");
7474

7575
```
7676

77+
## Interpolation
78+
79+
You can use `interpolate` flag to enable interpolation syntax for ES6 template strings, like so:
80+
81+
```
82+
require("html?interpolate!./file.html");
83+
```
84+
85+
```
86+
<img src="${require(`./images/gallery.png`)}" />
87+
<div>${require('./partials/gallery.html')}</div>
88+
```
89+
7790
## License
7891

7992
MIT (http://www.opensource.org/licenses/mit-license.php)

index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var SourceNode = require("source-map").SourceNode;
88
var loaderUtils = require("loader-utils");
99
var url = require("url");
1010
var assign = require("object-assign");
11+
var compile = require("es6-templates").compile;
1112

1213
function randomIdent() {
1314
return "xxxHTMLLINKxxx" + Math.random() + Math.random() + "xxx";
@@ -37,15 +38,14 @@ module.exports = function(content) {
3738
content = [content];
3839
links.forEach(function(link) {
3940
if(!loaderUtils.isUrlRequest(link.value, root)) return;
40-
41+
4142
var uri = url.parse(link.value);
4243
if (uri.hash !== null && uri.hash !== undefined) {
43-
uri.hash = null;
44-
link.value = uri.format();
45-
link.length = link.value.length;
44+
uri.hash = null;
45+
link.value = uri.format();
46+
link.length = link.value.length;
4647
}
4748

48-
4949
do {
5050
var ident = randomIdent();
5151
} while(data[ident]);
@@ -77,7 +77,14 @@ module.exports = function(content) {
7777

7878
content = htmlMinifier.minify(content, minimizeOptions);
7979
}
80-
return "module.exports = " + JSON.stringify(content).replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) {
80+
81+
if (query.interpolate) {
82+
content = compile('`' + content + '`').code;
83+
} else {
84+
content = JSON.stringify(content);
85+
}
86+
87+
return "module.exports = " + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) {
8188
if(!data[match]) return match;
8289
return '" + require(' + JSON.stringify(loaderUtils.urlToRequest(data[match], root)) + ') + "';
8390
}) + ";";

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"author": "Tobias Koppers @sokra",
55
"description": "html loader module for webpack",
66
"dependencies": {
7+
"es6-templates": "^0.2.2",
78
"fastparse": "^1.0.0",
89
"html-minifier": "^0.7.2",
910
"loader-utils": "~0.2.2",

test/loaderTest.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,16 @@ describe("loader", function() {
7474
'module.exports = "<img src=\\"" + require("./icons.svg") + "#hash\\">";'
7575
);
7676
});
77+
it("should ignore interpolations by default", function() {
78+
loader.call({}, '<img src="${"Hello " + (1+1)}">').should.be.eql(
79+
'module.exports = "<img src=\\"${\\"Hello \\" + (1+1)}\\">";'
80+
);
81+
});
82+
it("should enable interpolations when using interpolate flag", function() {
83+
loader.call({
84+
query: "?interpolate"
85+
}, '<img src="${"Hello " + (1+1)}">').should.be.eql(
86+
'module.exports = "<img src=\\"" + ("Hello " + (1 + 1)) + "\\">";'
87+
);
88+
});
7789
});

0 commit comments

Comments
 (0)