Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@svgr/plugin-svgo

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@svgr/plugin-svgo - npm Package Compare versions

Comparing version 5.5.0 to 6.0.0-alpha.0

79

lib/config.js
"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);
}

@@ -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"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc