Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Added options to define export format #73

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

<h2 align="center">Example Usage</h2>

```js
Expand Down
4 changes: 3 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ module.exports = {
classPrefix: false,
idPrefix: false,
warnTags: [],
warnTagAttrs: []
warnTagAttrs: [],
exportAsDefault: false,
exportAsEs6Default: false
};
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions tests/svg-inline-loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});