From 68ecd43b1070beec496befd485246f1a12046f73 Mon Sep 17 00:00:00 2001 From: Root-Core Date: Sun, 10 Dec 2017 03:42:50 +0100 Subject: [PATCH 1/4] Added exportAsDefault and exportAsEs6Default to config --- config.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config.js b/config.js index 5dde54f..8c5c0a0 100644 --- a/config.js +++ b/config.js @@ -11,5 +11,7 @@ module.exports = { classPrefix: false, idPrefix: false, warnTags: [], - warnTagAttrs: [] + warnTagAttrs: [], + exportAsDefault: false, + exportAsEs6Default: false }; From 8cfdd977edc64381b90c906276d79dc70d831f26 Mon Sep 17 00:00:00 2001 From: Root-Core Date: Sun, 10 Dec 2017 03:44:18 +0100 Subject: [PATCH 2/4] Made export string configurable --- index.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 584d278..450f153 100644 --- a/index.js +++ b/index.js @@ -62,7 +62,14 @@ function SVGInlineLoader(content) { // Configuration var query = loaderUtils.parseQuery(this.query); - return "module.exports = " + JSON.stringify(getExtractedSVG(content, query)); + var exportsString = "module.exports = "; + if (query.exportAsDefault) { + exportsString = "exports.default = "; + } else if (query.exportAsEs6Default) { + exportsString = "exports default "; + } + + return exportsString + JSON.stringify(getExtractedSVG(content, query)); } SVGInlineLoader.getExtractedSVG = getExtractedSVG; From 5bbc4245f09fec825838e8204730968cd3c23cb7 Mon Sep 17 00:00:00 2001 From: Root-Core Date: Sun, 10 Dec 2017 03:52:09 +0100 Subject: [PATCH 3/4] Added documentation regarding export formats --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 5285eb5..0472df1 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,18 @@ Adds a prefix to ids to avoid collision across svg files. default: `idPrefix: false` +#### `exportAsDefault: boolean`, `exportAsEs6Default: boolean` + +There are different export formats available: + ++ ```module.exports``` (default, cjs format). "Hello world" becomes ```module.exports = "Hello world";``` ++ ```exports.default``` (when ```exportAsDefault``` param is set, es6to5 format). "Hello world" becomes ```exports.default = "Hello world";``` ++ ```exports default``` (when ```exportAsEs6Default``` param is set, es6 format). "Hello world" becomes ```exports default "Hello world";``` + +default: `exportAsDefault: false` + +default: `exportAsEs6Default: false` +

Example Usage

```js From c966381379ce0095db8c185e3f19a9b111f938b4 Mon Sep 17 00:00:00 2001 From: Root-Core Date: Sun, 10 Dec 2017 03:53:17 +0100 Subject: [PATCH 4/4] Added tests for export formats / SVGInlineLoader --- tests/svg-inline-loader.test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/svg-inline-loader.test.js b/tests/svg-inline-loader.test.js index 676440c..dfa6419 100644 --- a/tests/svg-inline-loader.test.js +++ b/tests/svg-inline-loader.test.js @@ -153,3 +153,22 @@ describe('getExtractedSVG()', function(){ console.warn = oldConsoleWarn; // reset console back }); }); + +describe('SVGInlineLoader', function() { + var processedSVG = SVGInlineLoader.getExtractedSVG(svgWithRect); + + it("should export as commonjs export", function() { + expect(SVGInlineLoader.call({ query: "" }, svgWithRect)) + .to.be.eql('module.exports = ' + JSON.stringify(processedSVG)); + }); + + it("should export as default export for es6to5 transpilation", function() { + expect(SVGInlineLoader.call({ query: "?exportAsDefault" }, svgWithRect)) + .to.be.eql('exports.default = ' + JSON.stringify(processedSVG)); + }); + + it("should export as es6 default export", function() { + expect(SVGInlineLoader.call({ query: "?exportAsEs6Default" }, svgWithRect)) + .to.be.eql('exports default ' + JSON.stringify(processedSVG)); + }); +});