55// for the babylon JSX parser to work
66
77import isPlainObject from "lodash.isplainobject" ;
8+ import cloneDeep from "lodash.clonedeep" ;
89
910const essentialPlugins = [
1011 "removeDoctype" ,
@@ -16,13 +17,15 @@ export function validateAndFix(opts: any = {}) {
1617 if ( ! isPlainObject ( opts ) )
1718 throw new Error ( "Expected options.svgo to be Object." ) ;
1819
19- if ( opts . plugins === void 0 ) opts . plugins = [ ] ;
20+ let cleanOpts = cloneDeep ( opts ) ;
2021
21- if ( ! Array . isArray ( opts . plugins ) )
22+ if ( cleanOpts . plugins === void 0 ) cleanOpts . plugins = [ ] ;
23+
24+ if ( ! Array . isArray ( cleanOpts . plugins ) )
2225 throw new Error ( "Expected options.svgo.plugins to be an array" ) ;
2326
24- if ( opts . plugins . length === 0 ) {
25- opts . plugins = [ ...essentialPlugins ] . map ( p => ( { [ p ] : true } ) ) ;
27+ if ( cleanOpts . plugins . length === 0 ) {
28+ cleanOpts . plugins = [ ...essentialPlugins ] . map ( p => ( { [ p ] : true } ) ) ;
2629 }
2730
2831 const state = new Map ( ) ;
@@ -32,7 +35,7 @@ export function validateAndFix(opts: any = {}) {
3235 }
3336
3437 // parse through input plugins and mark enabled ones
35- for ( const plugin of opts . plugins ) {
38+ for ( const plugin of cleanOpts . plugins ) {
3639 if ( isPlainObject ( plugin ) ) {
3740 for ( const pluginName of Object . keys ( plugin ) ) {
3841 if ( essentialPlugins . indexOf ( pluginName ) > - 1 ) {
@@ -55,14 +58,16 @@ export function validateAndFix(opts: any = {}) {
5558 // add missing plugins
5659 for ( const p of essentialPlugins ) {
5760 if ( ! state . get ( p ) ) {
58- opts . plugins . push ( p ) ;
61+ cleanOpts . plugins . push ( p ) ;
5962 }
6063 }
6164
6265 // convert strings to objects to match the form svgo accepts
63- for ( let i = 0 ; i < opts . plugins . length ; i ++ ) {
64- if ( typeof opts . plugins [ i ] === "string" ) {
65- opts . plugins [ i ] = { [ opts . plugins [ i ] ] : true } ;
66+ for ( let i = 0 ; i < cleanOpts . plugins . length ; i ++ ) {
67+ if ( typeof cleanOpts . plugins [ i ] === "string" ) {
68+ cleanOpts . plugins [ i ] = { [ cleanOpts . plugins [ i ] ] : true } ;
6669 }
6770 }
71+
72+ return cleanOpts ;
6873}
0 commit comments