Skip to content

Commit 4633a1c

Browse files
authored
Merge pull request #97 from laland/master
adds ability to export module as es6 deafult export
2 parents 74fcfb3 + 25206b3 commit 4633a1c

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ require("html?interpolate=require!./file.ftl");
141141
<div>${require('./components/gallery.html')}</div>
142142
```
143143

144+
### Export format
145+
146+
By default HTML is exported with ```module.exports```, but you can use ```exportAsEs6Default``` flag to export it as ES6 default export (via ```exports.default```)
147+
144148
### Advanced options
145149

146150
If you need to pass [more advanced options](https://github.com/webpack/html-loader/pull/46), especially those which cannot be stringified, you can also define an `htmlLoader`-property on your `webpack.config.js`:

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ module.exports = function(content) {
124124
} else {
125125
content = JSON.stringify(content);
126126
}
127+
128+
var exportsString = config.exportAsEs6Default? "exports.default": "module.exports";
127129

128-
return "module.exports = " + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) {
130+
return exportsString + " = " + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) {
129131
if(!data[match]) return match;
130132
return '" + require(' + JSON.stringify(loaderUtils.urlToRequest(data[match], root)) + ') + "';
131133
}) + ";";

test/loaderTest.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,11 @@ describe("loader", function() {
126126
'module.exports = "<a href=\\"${list.href}\\"><img src=\\"" + require("./test.jpg") + "\\" /></a>";'
127127
);
128128
});
129+
it("should export as es6 default export", function() {
130+
loader.call({
131+
query: "?exportAsEs6Default"
132+
}, '<p>Hello world!</p>').should.be.eql(
133+
'exports.default = "<p>Hello world!</p>";'
134+
);
135+
});
129136
});

0 commit comments

Comments
 (0)