From aade6ad40c328c680ef8dc40a992e7c8aa24701f Mon Sep 17 00:00:00 2001 From: Kayne Barclay Date: Wed, 25 Oct 2017 15:55:19 -0700 Subject: [PATCH] fixes #141: Updating base64 option to be encoding --- README.md | 10 ++++++++++ index.js | 5 +++-- test/loader.spec.js | 6 +++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 84bb5c7b..8052dbbe 100644 --- a/README.md +++ b/README.md @@ -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/) diff --git a/index.js b/index.js index 36ebfbde..ef64d3fa 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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); } @@ -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+'"'; } diff --git a/test/loader.spec.js b/test/loader.spec.js index a0c41dfe..6da796f3 100644 --- a/test/loader.spec.js +++ b/test/loader.spec.js @@ -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); @@ -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);