From 3b8abad36bb771aeec4e859daf641152ca6d7926 Mon Sep 17 00:00:00 2001 From: Reznichenko Vladimir Date: Mon, 26 Sep 2016 11:50:34 +0300 Subject: [PATCH 1/2] adds ability to export module as es6 deafult export --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d5c73494..e37255cf 100644 --- a/index.js +++ b/index.js @@ -125,7 +125,9 @@ module.exports = function(content) { content = JSON.stringify(content); } - return "module.exports = " + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) { + var exportsString = config.exportAsEs6Default? "exports.default": "module.exports"; + + 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)) + ') + "'; }) + ";"; From 25206b3988492f695daea0737b89b2d513aabf41 Mon Sep 17 00:00:00 2001 From: Reznichenko Vladimir Date: Thu, 20 Oct 2016 10:36:25 +0300 Subject: [PATCH 2/2] [#PR97] updates tests and docs --- README.md | 4 ++++ index.js | 2 +- test/loaderTest.js | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) 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 e37255cf..78c0af71 100644 --- a/index.js +++ b/index.js @@ -124,7 +124,7 @@ module.exports = function(content) { } else { content = JSON.stringify(content); } - + var exportsString = config.exportAsEs6Default? "exports.default": "module.exports"; return exportsString + " = " + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) { 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!

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