index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
  7. var _svgo = require("svgo");
  8. var _url = require("./lib/url");
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. const PLUGIN = 'postcss-svgo';
  11. const dataURI = /data:image\/svg\+xml(;((charset=)?utf-8|base64))?,/i;
  12. const dataURIBase64 = /data:image\/svg\+xml;base64,/i; // the following regex will globally match:
  13. // \b([\w-]+) --> a word (a sequence of one or more [alphanumeric|underscore|dash] characters; followed by
  14. // \s*=\s* --> an equal sign character (=) between optional whitespaces; followed by
  15. // \\"([\S\s]+?)\\" --> any characters (including whitespaces and newlines) between literal escaped quotes (\")
  16. const escapedQuotes = /\b([\w-]+)\s*=\s*\\"([\S\s]+?)\\"/g;
  17. /**
  18. * @param {string} input the SVG string
  19. * @param {boolean} encode whether to encode the result
  20. * @return {object} the minification result
  21. */
  22. function minifySVG(input, opts) {
  23. let svg = input;
  24. let decodedUri, isUriEncoded;
  25. try {
  26. decodedUri = (0, _url.decode)(input);
  27. isUriEncoded = decodedUri !== input;
  28. } catch (e) {
  29. // Swallow exception if we cannot decode the value
  30. isUriEncoded = false;
  31. }
  32. if (isUriEncoded) {
  33. svg = decodedUri;
  34. }
  35. if (opts.encode !== undefined) {
  36. isUriEncoded = opts.encode;
  37. } // normalize all escaped quote characters from svg attributes
  38. // from <svg attr=\"value\"... /> to <svg attr="value"... />
  39. // see: https://github.com/cssnano/cssnano/issues/1194
  40. svg = svg.replace(escapedQuotes, '$1="$2"');
  41. const result = (0, _svgo.optimize)(svg, opts);
  42. if (result.error) {
  43. throw new Error(result.error);
  44. }
  45. return {
  46. result: result.data,
  47. isUriEncoded
  48. };
  49. }
  50. function minify(decl, opts, postcssResult) {
  51. const parsed = (0, _postcssValueParser.default)(decl.value);
  52. decl.value = parsed.walk(node => {
  53. if (node.type !== 'function' || node.value.toLowerCase() !== 'url' || !node.nodes.length) {
  54. return;
  55. }
  56. let {
  57. value,
  58. quote
  59. } = node.nodes[0];
  60. let optimizedValue;
  61. try {
  62. if (dataURIBase64.test(value)) {
  63. const url = new URL(value);
  64. const base64String = `${url.protocol}${url.pathname}`.replace(dataURI, '');
  65. const svg = Buffer.from(base64String, 'base64').toString('utf8');
  66. const {
  67. result
  68. } = minifySVG(svg, opts);
  69. const data = Buffer.from(result).toString('base64');
  70. optimizedValue = 'data:image/svg+xml;base64,' + data + url.hash;
  71. } else if (dataURI.test(value)) {
  72. const svg = value.replace(dataURI, '');
  73. const {
  74. result,
  75. isUriEncoded
  76. } = minifySVG(svg, opts);
  77. let data = isUriEncoded ? (0, _url.encode)(result) : result; // Should always encode # otherwise we yield a broken SVG
  78. // in Firefox (works in Chrome however). See this issue:
  79. // https://github.com/cssnano/cssnano/issues/245
  80. data = data.replace(/#/g, '%23');
  81. optimizedValue = 'data:image/svg+xml;charset=utf-8,' + data;
  82. quote = isUriEncoded ? '"' : "'";
  83. } else {
  84. return;
  85. }
  86. } catch (error) {
  87. decl.warn(postcssResult, `${error}`);
  88. return;
  89. }
  90. node.nodes[0] = Object.assign({}, node.nodes[0], {
  91. value: optimizedValue,
  92. quote: quote,
  93. type: 'string',
  94. before: '',
  95. after: ''
  96. });
  97. return false;
  98. });
  99. decl.value = decl.value.toString();
  100. }
  101. function pluginCreator(opts = {}) {
  102. return {
  103. postcssPlugin: PLUGIN,
  104. OnceExit(css, {
  105. result
  106. }) {
  107. css.walkDecls(decl => {
  108. if (!dataURI.test(decl.value)) {
  109. return;
  110. }
  111. minify(decl, opts, result);
  112. });
  113. }
  114. };
  115. }
  116. pluginCreator.postcss = true;
  117. var _default = pluginCreator;
  118. exports.default = _default;
  119. module.exports = exports.default;