5defe2b7757f45497c78faaefa54e6a8f792cb2bb7e324c6102565cc4ca8abb591727e4ee4047fb09b2494860bcb03e1ba48ecaf109035b3ee66079c23d8a2 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. 'use strict';
  2. const pluginsMap = require('../../plugins/plugins.js');
  3. const pluginsOrder = [
  4. 'removeDoctype',
  5. 'removeXMLProcInst',
  6. 'removeComments',
  7. 'removeMetadata',
  8. 'removeXMLNS',
  9. 'removeEditorsNSData',
  10. 'cleanupAttrs',
  11. 'mergeStyles',
  12. 'inlineStyles',
  13. 'minifyStyles',
  14. 'convertStyleToAttrs',
  15. 'cleanupIDs',
  16. 'prefixIds',
  17. 'removeRasterImages',
  18. 'removeUselessDefs',
  19. 'cleanupNumericValues',
  20. 'cleanupListOfValues',
  21. 'convertColors',
  22. 'removeUnknownsAndDefaults',
  23. 'removeNonInheritableGroupAttrs',
  24. 'removeUselessStrokeAndFill',
  25. 'removeViewBox',
  26. 'cleanupEnableBackground',
  27. 'removeHiddenElems',
  28. 'removeEmptyText',
  29. 'convertShapeToPath',
  30. 'convertEllipseToCircle',
  31. 'moveElemsAttrsToGroup',
  32. 'moveGroupAttrsToElems',
  33. 'collapseGroups',
  34. 'convertPathData',
  35. 'convertTransform',
  36. 'removeEmptyAttrs',
  37. 'removeEmptyContainers',
  38. 'mergePaths',
  39. 'removeUnusedNS',
  40. 'sortAttrs',
  41. 'sortDefsChildren',
  42. 'removeTitle',
  43. 'removeDesc',
  44. 'removeDimensions',
  45. 'removeAttrs',
  46. 'removeAttributesBySelector',
  47. 'removeElementsByAttr',
  48. 'addClassesToSVGElement',
  49. 'removeStyleElement',
  50. 'removeScriptElement',
  51. 'addAttributesToSVGElement',
  52. 'removeOffCanvasPaths',
  53. 'reusePaths',
  54. ];
  55. const defaultPlugins = pluginsOrder.filter((name) => pluginsMap[name].active);
  56. exports.defaultPlugins = defaultPlugins;
  57. const extendDefaultPlugins = (plugins) => {
  58. console.warn(
  59. '\n"extendDefaultPlugins" utility is deprecated.\n' +
  60. 'Use "preset-default" plugin with overrides instead.\n' +
  61. 'For example:\n' +
  62. `{\n` +
  63. ` name: 'preset-default',\n` +
  64. ` params: {\n` +
  65. ` overrides: {\n` +
  66. ` // customize plugin options\n` +
  67. ` convertShapeToPath: {\n` +
  68. ` convertArcs: true\n` +
  69. ` },\n` +
  70. ` // disable plugins\n` +
  71. ` convertPathData: false\n` +
  72. ` }\n` +
  73. ` }\n` +
  74. `}\n`
  75. );
  76. const extendedPlugins = pluginsOrder.map((name) => ({
  77. name,
  78. active: pluginsMap[name].active,
  79. }));
  80. for (const plugin of plugins) {
  81. const resolvedPlugin = resolvePluginConfig(plugin);
  82. const index = pluginsOrder.indexOf(resolvedPlugin.name);
  83. if (index === -1) {
  84. extendedPlugins.push(plugin);
  85. } else {
  86. extendedPlugins[index] = plugin;
  87. }
  88. }
  89. return extendedPlugins;
  90. };
  91. exports.extendDefaultPlugins = extendDefaultPlugins;
  92. const resolvePluginConfig = (plugin) => {
  93. let configParams = {};
  94. if (typeof plugin === 'string') {
  95. // resolve builtin plugin specified as string
  96. const pluginConfig = pluginsMap[plugin];
  97. if (pluginConfig == null) {
  98. throw Error(`Unknown builtin plugin "${plugin}" specified.`);
  99. }
  100. return {
  101. ...pluginConfig,
  102. name: plugin,
  103. active: true,
  104. params: { ...pluginConfig.params, ...configParams },
  105. };
  106. }
  107. if (typeof plugin === 'object' && plugin != null) {
  108. if (plugin.name == null) {
  109. throw Error(`Plugin name should be specified`);
  110. }
  111. if (plugin.fn) {
  112. // resolve custom plugin with implementation
  113. return {
  114. active: true,
  115. ...plugin,
  116. params: { ...configParams, ...plugin.params },
  117. };
  118. } else {
  119. // resolve builtin plugin specified as object without implementation
  120. const pluginConfig = pluginsMap[plugin.name];
  121. if (pluginConfig == null) {
  122. throw Error(`Unknown builtin plugin "${plugin.name}" specified.`);
  123. }
  124. return {
  125. ...pluginConfig,
  126. active: true,
  127. ...plugin,
  128. params: { ...pluginConfig.params, ...configParams, ...plugin.params },
  129. };
  130. }
  131. }
  132. return null;
  133. };
  134. exports.resolvePluginConfig = resolvePluginConfig;