diff --git a/README.md b/README.md index d996165d..343ee5b9 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,10 @@ require("html?interpolate=require!./file.ftl");
${require('./components/gallery.html')}
``` +### 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`: diff --git a/index.js b/index.js index d5c73494..78c0af71 100644 --- a/index.js +++ b/index.js @@ -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)) + ') + "'; }) + ";"; diff --git a/test/loaderTest.js b/test/loaderTest.js index 09408ab9..a5a67f73 100644 --- a/test/loaderTest.js +++ b/test/loaderTest.js @@ -126,4 +126,11 @@ describe("loader", function() { 'module.exports = "";' ); }); + it("should export as es6 default export", function() { + loader.call({ + query: "?exportAsEs6Default" + }, '

Hello world!

').should.be.eql( + 'exports.default = "

Hello world!

";' + ); + }); });