Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ require("html?interpolate=require!./file.ftl");
<div>${require('./components/gallery.html')}</div>
```

### Export format

By default HTML is exported with ```module.exports```, but you can use ```exportAsEs6Default``` flag to export it as ES6 default export (via ```exports.default```)

### Advanced options

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`:
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ module.exports = function(content) {
} else {
content = JSON.stringify(content);
}

var exportsString = config.exportAsEs6Default? "exports.default": "module.exports";

return "module.exports = " + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) {
return exportsString + " = " + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) {
if(!data[match]) return match;
return '" + require(' + JSON.stringify(loaderUtils.urlToRequest(data[match], root)) + ') + "';
}) + ";";
Expand Down
7 changes: 7 additions & 0 deletions test/loaderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,11 @@ describe("loader", function() {
'module.exports = "<a href=\\"${list.href}\\"><img src=\\"" + require("./test.jpg") + "\\" /></a>";'
);
});
it("should export as es6 default export", function() {
loader.call({
query: "?exportAsEs6Default"
}, '<p>Hello world!</p>').should.be.eql(
'exports.default = "<p>Hello world!</p>";'
);
});
});