From 155aba37cf6ab71e053613a1f9e6ce142069cf3a Mon Sep 17 00:00:00 2001 From: Mathias Helmuth Pedersen Date: Tue, 26 Sep 2017 10:43:34 +0200 Subject: [PATCH 1/6] Add loader config option for customizing svgo plugins --- index.js | 10 ++++++---- package.json | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index a6905f9..2f1802c 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,13 @@ var svg = require('svgo'); +var loaderUtils = require('loader-utils'); var compiler = require('vue-template-compiler'); -var svgo = new svg({ - plugins: ['removeDoctype', 'removeComments'], -}); - module.exports = function (content) { + var options = loaderUtils.getOptions(this); + var svgo = new svg({ + plugins: options.svgo || ['removeDoctype', 'removeComments'], + }); + this.cacheable && this.cacheable(true); this.addDependency(this.resourcePath); diff --git a/package.json b/package.json index db5511a..cfb241d 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "vue-template-compiler": "^2.0.0" }, "dependencies": { + "loader-utils": "^1.1.0", "svgo": "^0.7.2" } } From d3f9a238a2eca291902c98706a4e66097a083c27 Mon Sep 17 00:00:00 2001 From: Mathias Helmuth Pedersen Date: Tue, 26 Sep 2017 12:43:16 +0200 Subject: [PATCH 2/6] Options fallback to empty object instead of undefined if no options given --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 2f1802c..e6cdd80 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ var loaderUtils = require('loader-utils'); var compiler = require('vue-template-compiler'); module.exports = function (content) { - var options = loaderUtils.getOptions(this); + var options = loaderUtils.getOptions(this) || {}; var svgo = new svg({ plugins: options.svgo || ['removeDoctype', 'removeComments'], }); From 50240eeb77ea5ed5b01088a8275c26f594c33a04 Mon Sep 17 00:00:00 2001 From: Mathias Helmuth Pedersen Date: Tue, 26 Sep 2017 15:10:23 +0200 Subject: [PATCH 3/6] Fix svgo options fallback syntax --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index e6cdd80..ddef08e 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ var compiler = require('vue-template-compiler'); module.exports = function (content) { var options = loaderUtils.getOptions(this) || {}; var svgo = new svg({ - plugins: options.svgo || ['removeDoctype', 'removeComments'], + plugins: options.svgo || [{removeDoctype: true}, {removeComments: true}], }); this.cacheable && this.cacheable(true); From 30fc41fb0100219eeb8a76c10fcda277e252e33f Mon Sep 17 00:00:00 2001 From: Mathias Helmuth Pedersen Date: Tue, 26 Sep 2017 15:31:54 +0200 Subject: [PATCH 4/6] Refactor to fit more closely with standard svgo implementation in svgo-loader and image-webpack-loader --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index ddef08e..ab4f24e 100644 --- a/index.js +++ b/index.js @@ -3,10 +3,10 @@ var loaderUtils = require('loader-utils'); var compiler = require('vue-template-compiler'); module.exports = function (content) { - var options = loaderUtils.getOptions(this) || {}; - var svgo = new svg({ - plugins: options.svgo || [{removeDoctype: true}, {removeComments: true}], - }); + var options = loaderUtils.getOptions(this) || { + plugins: [{removeDoctype: true}, {removeComments: true}], + }; + var svgo = new svg(options); this.cacheable && this.cacheable(true); this.addDependency(this.resourcePath); From fe0d34e20514f07f6df7b38c319ea8df14fdb2e9 Mon Sep 17 00:00:00 2001 From: Mathias Helmuth Pedersen Date: Tue, 26 Sep 2017 15:43:08 +0200 Subject: [PATCH 5/6] Restructure svgo options to own variable --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ab4f24e..86e1c6f 100644 --- a/index.js +++ b/index.js @@ -3,10 +3,11 @@ var loaderUtils = require('loader-utils'); var compiler = require('vue-template-compiler'); module.exports = function (content) { - var options = loaderUtils.getOptions(this) || { + var options = loaderUtils.getOptions(this) || {}; + var svgoOptions = options.svgo || { plugins: [{removeDoctype: true}, {removeComments: true}], }; - var svgo = new svg(options); + var svgo = new svg(svgoOptions); this.cacheable && this.cacheable(true); this.addDependency(this.resourcePath); From 767d66bc071c302d97e229cb1fed6ba31f68ee1c Mon Sep 17 00:00:00 2001 From: Mathias Helmuth Pedersen Date: Tue, 26 Sep 2017 15:58:51 +0200 Subject: [PATCH 6/6] Update readme --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 42e12b9..0e259d0 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,15 @@ yarn add --dev vue-template-compiler vue-svg-loader { test: /\.svg$/, loader: 'vue-svg-loader', // `vue-svg` for webpack 1.x + options: { + // optional [svgo](https://github.com/svg/svgo) options + svgo: { + plugins: [ + {removeDoctype: true}, + {removeComments: true} + ] + } + } } ```