index.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. var svgo = require('svgo');
  3. var cosmiconfig = require('cosmiconfig');
  4. const explorer = cosmiconfig.cosmiconfigSync("svgo", {
  5. searchPlaces: [
  6. "package.json",
  7. ".svgorc",
  8. ".svgorc.js",
  9. ".svgorc.json",
  10. ".svgorc.yaml",
  11. ".svgorc.yml",
  12. "svgo.config.js",
  13. ".svgo.yml"
  14. ],
  15. transform: (result) => result && result.config,
  16. cache: true
  17. });
  18. const getSvgoConfigFromSvgrConfig = (config) => {
  19. const params = { overrides: {} };
  20. if (config.icon || config.dimensions === false) {
  21. params.overrides.removeViewBox = false;
  22. }
  23. if (config.native) {
  24. params.overrides.inlineStyles = {
  25. onlyMatchedOnce: false
  26. };
  27. }
  28. return {
  29. plugins: [
  30. {
  31. name: "preset-default",
  32. params
  33. },
  34. "prefixIds"
  35. ]
  36. };
  37. };
  38. const getSvgoConfig = (config, state) => {
  39. const cwd = state.filePath || process.cwd();
  40. if (config.svgoConfig)
  41. return config.svgoConfig;
  42. if (config.runtimeConfig) {
  43. const userConfig = explorer.search(cwd);
  44. if (userConfig)
  45. return userConfig;
  46. }
  47. return getSvgoConfigFromSvgrConfig(config);
  48. };
  49. var __defProp = Object.defineProperty;
  50. var __defProps = Object.defineProperties;
  51. var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
  52. var __getOwnPropSymbols = Object.getOwnPropertySymbols;
  53. var __hasOwnProp = Object.prototype.hasOwnProperty;
  54. var __propIsEnum = Object.prototype.propertyIsEnumerable;
  55. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  56. var __spreadValues = (a, b) => {
  57. for (var prop in b || (b = {}))
  58. if (__hasOwnProp.call(b, prop))
  59. __defNormalProp(a, prop, b[prop]);
  60. if (__getOwnPropSymbols)
  61. for (var prop of __getOwnPropSymbols(b)) {
  62. if (__propIsEnum.call(b, prop))
  63. __defNormalProp(a, prop, b[prop]);
  64. }
  65. return a;
  66. };
  67. var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
  68. const svgoPlugin = (code, config, state) => {
  69. if (!config.svgo)
  70. return code;
  71. const svgoConfig = getSvgoConfig(config, state);
  72. const result = svgo.optimize(code, __spreadProps(__spreadValues({}, svgoConfig), { path: state.filePath }));
  73. if (result.modernError) {
  74. throw result.modernError;
  75. }
  76. return result.data;
  77. };
  78. module.exports = svgoPlugin;
  79. //# sourceMappingURL=index.js.map