Skip to content

Commit

Permalink
fixes bhovhannes#141: Updating base64 option to be encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Kayne Barclay committed Oct 25, 2017
1 parent 74b2c9c commit aade6ad
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ Internet Explorer (including IE11) stops parsing style-elements in SVG data-URIs
require('svg-url-loader?iesafe!./file.svg');
```

### `encoding`

This option controls which encoding to use when constructing a data-URI for an SVG. When set to a non-"none" value, quotes are never applied to the outputted data-URI.

Possible values are "base64" and "none". Defaults to "none".

``` javascript
require('svg-url-loader?encoding=base64!./file.svg');
```

## Usage

[Documentation: Loaders](https://webpack.js.org/concepts/loaders/)
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = function(content) {
this.cacheable && this.cacheable();

var query = loaderUtils.getOptions(this) || {};
query.encoding = query.encoding || "none";

var limit = query.limit ? parseInt(query.limit, 10) : 0;

Expand All @@ -21,7 +22,7 @@ module.exports = function(content) {
}

var data;
if (query.base64) {
if (query.encoding === "base64") {
if (typeof newContent === "string") {
newContent = new Buffer(newContent);
}
Expand All @@ -38,7 +39,7 @@ module.exports = function(content) {
}

if (!(query.iesafe && hasStyleElement && data.length > 4096)) {
if (!query.base64 && !query.noquotes) {
if (query.encoding === "none" && !query.noquotes) {
data = '"'+data+'"';
}

Expand Down
6 changes: 3 additions & 3 deletions test/loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ describe('svg-url-loader', function() {
});
config.module.rules[0].use[0].options.iesafe = true;
config.module.rules[0].use[0].options.name = 'foo.svg';
config.module.rules[0].use[0].options.base64 = true;
config.module.rules[0].use[0].options.encoding = "base64";

webpack(config, function(err) {
expect(err).to.be(null);
Expand All @@ -338,12 +338,12 @@ describe('svg-url-loader', function() {
});
});

describe("base64", function() {
describe("encoding is base64", function() {
it('should convert SVG file to base64 encoded data-uri string', function(done) {
var config = assign({}, globalConfig, {
entry: './test/input/icon.js'
});
config.module.rules[0].use[0].options.base64 = true;
config.module.rules[0].use[0].options.encoding = "base64";

webpack(config, function(err) {
expect(err).to.be(null);
Expand Down

0 comments on commit aade6ad

Please sign in to comment.