index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = svgoPlugin;
  4. var _svgo = _interopRequireDefault(require("svgo"));
  5. var _cosmiconfig = require("cosmiconfig");
  6. var _config = require("./config");
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. /* eslint-disable no-underscore-dangle */
  9. const explorer = (0, _cosmiconfig.cosmiconfigSync)('svgo', {
  10. searchPlaces: ['package.json', '.svgorc', '.svgorc.js', '.svgorc.json', '.svgorc.yaml', '.svgorc.yml', 'svgo.config.js', '.svgo.yml'],
  11. transform: result => result && result.config,
  12. cache: true
  13. });
  14. function encodeSVGDatauri(str, type) {
  15. let prefix = 'data:image/svg+xml'; // base64
  16. if (!type || type === 'base64') {
  17. prefix += ';base64,';
  18. if (Buffer.from) {
  19. str = prefix + Buffer.from(str).toString('base64');
  20. } else {
  21. // eslint-disable-next-line
  22. str = prefix + new Buffer(str).toString('base64');
  23. } // URI encoded
  24. } else if (type === 'enc') {
  25. str = `${prefix},${encodeURIComponent(str)}`; // unencoded
  26. } else if (type === 'unenc') {
  27. str = `${prefix},${str}`;
  28. }
  29. return str;
  30. } // See https://github.com/svg/svgo/blob/master/lib/svgo.js#L24
  31. // _optimizeOnce is synchronous internally
  32. function optimizeSync(svgstr, info) {
  33. const {
  34. config
  35. } = this;
  36. if (config.error) {
  37. throw config.error;
  38. }
  39. const maxPassCount = config.multipass ? 10 : 1;
  40. let counter = 0;
  41. let prevResultSize = Number.POSITIVE_INFINITY;
  42. let result;
  43. const optimizeOnceCallback = svgjs => {
  44. if (svgjs.error) {
  45. throw svgjs.error;
  46. } // eslint-disable-next-line no-plusplus
  47. if (++counter < maxPassCount && svgjs.data.length < prevResultSize) {
  48. prevResultSize = svgjs.data.length;
  49. this._optimizeOnce(svgjs.data, info, optimizeOnceCallback);
  50. } else {
  51. if (config.datauri) {
  52. svgjs.data = encodeSVGDatauri(svgjs.data, config.datauri);
  53. }
  54. if (info.path) {
  55. svgjs.path = info.path;
  56. }
  57. result = svgjs;
  58. }
  59. };
  60. this._optimizeOnce(svgstr, info, optimizeOnceCallback);
  61. return result;
  62. }
  63. function createSvgo(config, rcConfig) {
  64. const baseSvgoConfig = (0, _config.getBaseSvgoConfig)(config);
  65. const mergedConfig = (0, _config.mergeSvgoConfig)(baseSvgoConfig, rcConfig, config.svgoConfig);
  66. return new _svgo.default(mergedConfig);
  67. }
  68. function getInfo(state) {
  69. return state.filePath ? {
  70. input: 'file',
  71. path: state.filePath
  72. } : {
  73. input: 'string'
  74. };
  75. }
  76. function svgoPlugin(code, config, state) {
  77. if (!config.svgo) return code;
  78. const filePath = (0, _config.getFilePath)(state);
  79. const svgoRcConfig = config.runtimeConfig ? explorer.search(filePath) : {};
  80. const svgo = createSvgo(config, svgoRcConfig);
  81. const {
  82. data
  83. } = optimizeSync.call(svgo, code, getInfo(state));
  84. return data;
  85. }