@svgr/plugin-svgo
Advanced tools
Comparing version 5.5.0 to 6.0.0-alpha.0
"use strict"; | ||
exports.__esModule = true; | ||
exports.getFilePath = getFilePath; | ||
exports.getBaseSvgoConfig = getBaseSvgoConfig; | ||
exports.getPlugins = getPlugins; | ||
exports.mergeSvgoConfig = mergeSvgoConfig; | ||
exports.getSvgoConfig = getSvgoConfig; | ||
var _deepmerge = _interopRequireDefault(require("deepmerge")); | ||
var _cosmiconfig = require("cosmiconfig"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const explorer = (0, _cosmiconfig.cosmiconfigSync)('svgo', { | ||
searchPlaces: ['package.json', '.svgorc', '.svgorc.js', '.svgorc.json', '.svgorc.yaml', '.svgorc.yml', 'svgo.config.js', '.svgo.yml'], | ||
transform: result => result && result.config, | ||
cache: true | ||
}); | ||
function getFilePath(state) { | ||
return state.filePath || process.cwd(); | ||
} | ||
function getBaseSvgoConfig(config) { | ||
const baseSvgoConfig = { | ||
plugins: [{ | ||
prefixIds: true | ||
}] | ||
function getSvgoConfigFromSvgrConfig(config) { | ||
const preset = config.icon || config.dimensions === false ? { | ||
name: 'preset-default', | ||
params: { | ||
overrides: { | ||
removeViewBox: false | ||
} | ||
} | ||
} : 'preset-default'; | ||
return { | ||
plugins: [preset, 'prefixIds'] | ||
}; | ||
if (config.icon || config.dimensions === false) { | ||
baseSvgoConfig.plugins.push({ | ||
removeViewBox: false | ||
}); | ||
} | ||
return baseSvgoConfig; | ||
} | ||
function getPlugins(config) { | ||
if (!config || !config.plugins) { | ||
return []; | ||
} | ||
function getSvgoConfig(config, state) { | ||
const cwd = state.filePath || process.cwd(); | ||
if (config.svgoConfig) return config.svgoConfig; | ||
if (!Array.isArray(config.plugins)) { | ||
throw Error('`svgoConfig.plugins` must be an array'); | ||
if (config.runtimeConfig) { | ||
const userConfig = explorer.search(cwd); | ||
if (userConfig) return userConfig; | ||
} | ||
return config.plugins; | ||
} | ||
function extractPlugins(config) { | ||
if (!config) return []; | ||
if (!config.plugins) return []; | ||
if (!Array.isArray(config.plugins)) return [config.plugins]; | ||
return config.plugins; | ||
} | ||
function mergePlugins(configs) { | ||
const plugins = configs.reduce((merged, config) => _deepmerge.default.all([merged, ...extractPlugins(config)]), {}); | ||
return Object.keys(plugins).reduce((array, key) => { | ||
array.push({ | ||
[key]: plugins[key] | ||
}); | ||
return array; | ||
}, []); | ||
} | ||
function mergeSvgoConfig(...configs) { | ||
const plugins = mergePlugins(configs); | ||
return { ..._deepmerge.default.all(configs.filter(Boolean)), | ||
plugins | ||
}; | ||
return getSvgoConfigFromSvgrConfig(config); | ||
} |
101
lib/index.js
@@ -6,107 +6,16 @@ "use strict"; | ||
var _svgo = _interopRequireDefault(require("svgo")); | ||
var _svgo = require("svgo"); | ||
var _cosmiconfig = require("cosmiconfig"); | ||
var _config = require("./config"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/* eslint-disable no-underscore-dangle */ | ||
const explorer = (0, _cosmiconfig.cosmiconfigSync)('svgo', { | ||
searchPlaces: ['package.json', '.svgorc', '.svgorc.js', '.svgorc.json', '.svgorc.yaml', '.svgorc.yml', 'svgo.config.js', '.svgo.yml'], | ||
transform: result => result && result.config, | ||
cache: true | ||
}); | ||
function encodeSVGDatauri(str, type) { | ||
let prefix = 'data:image/svg+xml'; // base64 | ||
if (!type || type === 'base64') { | ||
prefix += ';base64,'; | ||
if (Buffer.from) { | ||
str = prefix + Buffer.from(str).toString('base64'); | ||
} else { | ||
// eslint-disable-next-line | ||
str = prefix + new Buffer(str).toString('base64'); | ||
} // URI encoded | ||
} else if (type === 'enc') { | ||
str = `${prefix},${encodeURIComponent(str)}`; // unencoded | ||
} else if (type === 'unenc') { | ||
str = `${prefix},${str}`; | ||
} | ||
return str; | ||
} // See https://github.com/svg/svgo/blob/master/lib/svgo.js#L24 | ||
// _optimizeOnce is synchronous internally | ||
function optimizeSync(svgstr, info) { | ||
const { | ||
config | ||
} = this; | ||
if (config.error) { | ||
throw config.error; | ||
} | ||
const maxPassCount = config.multipass ? 10 : 1; | ||
let counter = 0; | ||
let prevResultSize = Number.POSITIVE_INFINITY; | ||
let result; | ||
const optimizeOnceCallback = svgjs => { | ||
if (svgjs.error) { | ||
throw svgjs.error; | ||
} // eslint-disable-next-line no-plusplus | ||
if (++counter < maxPassCount && svgjs.data.length < prevResultSize) { | ||
prevResultSize = svgjs.data.length; | ||
this._optimizeOnce(svgjs.data, info, optimizeOnceCallback); | ||
} else { | ||
if (config.datauri) { | ||
svgjs.data = encodeSVGDatauri(svgjs.data, config.datauri); | ||
} | ||
if (info.path) { | ||
svgjs.path = info.path; | ||
} | ||
result = svgjs; | ||
} | ||
}; | ||
this._optimizeOnce(svgstr, info, optimizeOnceCallback); | ||
return result; | ||
} | ||
function createSvgo(config, rcConfig) { | ||
const baseSvgoConfig = (0, _config.getBaseSvgoConfig)(config); | ||
const mergedConfig = (0, _config.mergeSvgoConfig)(baseSvgoConfig, rcConfig, config.svgoConfig); | ||
return new _svgo.default(mergedConfig); | ||
} | ||
function getInfo(state) { | ||
return state.filePath ? { | ||
input: 'file', | ||
path: state.filePath | ||
} : { | ||
input: 'string' | ||
}; | ||
} | ||
function svgoPlugin(code, config, state) { | ||
if (!config.svgo) return code; | ||
const filePath = (0, _config.getFilePath)(state); | ||
const svgoRcConfig = config.runtimeConfig ? explorer.search(filePath) : {}; | ||
const svgo = createSvgo(config, svgoRcConfig); | ||
const svgoConfig = (0, _config.getSvgoConfig)(config, state); | ||
const { | ||
data | ||
} = optimizeSync.call(svgo, code, getInfo(state)); | ||
} = (0, _svgo.optimize)(code, { ...svgoConfig, | ||
path: state.filePath | ||
}); | ||
return data; | ||
} |
{ | ||
"name": "@svgr/plugin-svgo", | ||
"description": "Optimize SVG", | ||
"version": "5.5.0", | ||
"version": "6.0.0-alpha.0", | ||
"main": "lib/index.js", | ||
@@ -29,7 +29,7 @@ "repository": "https://github.com/gregberge/svgr/tree/master/packages/plugin-svgo", | ||
"dependencies": { | ||
"cosmiconfig": "^7.0.0", | ||
"cosmiconfig": "^7.0.1", | ||
"deepmerge": "^4.2.2", | ||
"svgo": "^1.2.2" | ||
"svgo": "^2.5.0" | ||
}, | ||
"gitHead": "b5920550bd966f876cb65c5e23af180461e5aa23" | ||
"gitHead": "4ab46a3a4f7b9aa589e991c98058473b8bd48af6" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7837
45
1
+ Added@trysound/sax@0.2.0(transitive)
+ Addedcommander@7.2.0(transitive)
+ Addedcss-select@4.3.0(transitive)
+ Addedcss-what@6.1.0(transitive)
+ Addeddom-serializer@1.4.1(transitive)
+ Addeddomhandler@4.3.1(transitive)
+ Addeddomutils@2.8.0(transitive)
+ Addednth-check@2.1.1(transitive)
+ Addedsvgo@2.8.0(transitive)
- Removed@types/q@1.5.8(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedargparse@1.0.10(transitive)
- Removedarray-buffer-byte-length@1.0.1(transitive)
- Removedarray.prototype.reduce@1.0.7(transitive)
- Removedarraybuffer.prototype.slice@1.0.3(transitive)
- Removedavailable-typed-arrays@1.0.7(transitive)
- Removedcall-bind@1.0.7(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcoa@2.0.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedcss-select@2.1.0(transitive)
- Removedcss-select-base-adapter@0.1.1(transitive)
- Removedcss-tree@1.0.0-alpha.37(transitive)
- Removedcss-what@3.4.2(transitive)
- Removeddata-view-buffer@1.0.1(transitive)
- Removeddata-view-byte-length@1.0.1(transitive)
- Removeddata-view-byte-offset@1.0.0(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddefine-properties@1.2.1(transitive)
- Removeddom-serializer@0.2.2(transitive)
- Removeddomelementtype@1.3.1(transitive)
- Removeddomutils@1.7.0(transitive)
- Removedes-abstract@1.23.3(transitive)
- Removedes-array-method-boxes-properly@1.0.0(transitive)
- Removedes-define-property@1.0.0(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedes-object-atoms@1.0.0(transitive)
- Removedes-set-tostringtag@2.0.3(transitive)
- Removedes-to-primitive@1.2.1(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedesprima@4.0.1(transitive)
- Removedfor-each@0.3.3(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedfunction.prototype.name@1.1.6(transitive)
- Removedfunctions-have-names@1.2.3(transitive)
- Removedget-intrinsic@1.2.4(transitive)
- Removedget-symbol-description@1.0.2(transitive)
- Removedglobalthis@1.0.4(transitive)
- Removedgopd@1.0.1(transitive)
- Removedhas-bigints@1.0.2(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)
- Removedhas-tostringtag@1.0.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedinternal-slot@1.0.7(transitive)
- Removedis-array-buffer@3.0.4(transitive)
- Removedis-bigint@1.0.4(transitive)
- Removedis-boolean-object@1.1.2(transitive)
- Removedis-callable@1.2.7(transitive)
- Removedis-data-view@1.0.1(transitive)
- Removedis-date-object@1.0.5(transitive)
- Removedis-negative-zero@2.0.3(transitive)
- Removedis-number-object@1.0.7(transitive)
- Removedis-regex@1.1.4(transitive)
- Removedis-shared-array-buffer@1.0.3(transitive)
- Removedis-string@1.0.7(transitive)
- Removedis-symbol@1.0.4(transitive)
- Removedis-typed-array@1.1.13(transitive)
- Removedis-weakref@1.0.2(transitive)
- Removedisarray@2.0.5(transitive)
- Removedjs-yaml@3.14.1(transitive)
- Removedmdn-data@2.0.4(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removednth-check@1.0.2(transitive)
- Removedobject-inspect@1.13.2(transitive)
- Removedobject-keys@1.1.1(transitive)
- Removedobject.assign@4.1.5(transitive)
- Removedobject.getownpropertydescriptors@2.1.8(transitive)
- Removedobject.values@1.2.0(transitive)
- Removedpossible-typed-array-names@1.0.0(transitive)
- Removedq@1.5.1(transitive)
- Removedregexp.prototype.flags@1.5.3(transitive)
- Removedsafe-array-concat@1.1.2(transitive)
- Removedsafe-regex-test@1.0.3(transitive)
- Removedsax@1.2.4(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedset-function-name@2.0.2(transitive)
- Removedside-channel@1.0.6(transitive)
- Removedsprintf-js@1.0.3(transitive)
- Removedstring.prototype.trim@1.2.9(transitive)
- Removedstring.prototype.trimend@1.0.8(transitive)
- Removedstring.prototype.trimstart@1.0.8(transitive)
- Removedsupports-color@5.5.0(transitive)
- Removedsvgo@1.3.2(transitive)
- Removedtyped-array-buffer@1.0.2(transitive)
- Removedtyped-array-byte-length@1.0.1(transitive)
- Removedtyped-array-byte-offset@1.0.2(transitive)
- Removedtyped-array-length@1.0.6(transitive)
- Removedunbox-primitive@1.0.2(transitive)
- Removedunquote@1.1.1(transitive)
- Removedutil.promisify@1.0.1(transitive)
- Removedwhich-boxed-primitive@1.0.2(transitive)
- Removedwhich-typed-array@1.1.15(transitive)
Updatedcosmiconfig@^7.0.1
Updatedsvgo@^2.5.0