index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use strict';
  2. const path = require('path');
  3. const valueParser = require('postcss-value-parser');
  4. const normalize = require('normalize-url');
  5. const multiline = /\\[\r\n]/;
  6. // eslint-disable-next-line no-useless-escape
  7. const escapeChars = /([\s\(\)"'])/g;
  8. // Scheme: https://tools.ietf.org/html/rfc3986#section-3.1
  9. // Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3
  10. const ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/;
  11. // Windows paths like `c:\`
  12. const WINDOWS_PATH_REGEX = /^[a-zA-Z]:\\/;
  13. /**
  14. * Originally in sindresorhus/is-absolute-url
  15. *
  16. * @param {string} url
  17. */
  18. function isAbsolute(url) {
  19. if (WINDOWS_PATH_REGEX.test(url)) {
  20. return false;
  21. }
  22. return ABSOLUTE_URL_REGEX.test(url);
  23. }
  24. /**
  25. * @param {string} url
  26. * @param {normalize.Options} options
  27. * @return {string}
  28. */
  29. function convert(url, options) {
  30. if (isAbsolute(url) || url.startsWith('//')) {
  31. let normalizedURL;
  32. try {
  33. normalizedURL = normalize(url, options);
  34. } catch (e) {
  35. normalizedURL = url;
  36. }
  37. return normalizedURL;
  38. }
  39. // `path.normalize` always returns backslashes on Windows, need replace in `/`
  40. return path.normalize(url).replace(new RegExp('\\' + path.sep, 'g'), '/');
  41. }
  42. /**
  43. * @param {import('postcss').AtRule} rule
  44. * @return {void}
  45. */
  46. function transformNamespace(rule) {
  47. rule.params = valueParser(rule.params)
  48. .walk((node) => {
  49. if (
  50. node.type === 'function' &&
  51. node.value.toLowerCase() === 'url' &&
  52. node.nodes.length
  53. ) {
  54. /** @type {valueParser.Node} */ (node).type = 'string';
  55. /** @type {any} */ (node).quote =
  56. node.nodes[0].type === 'string' ? node.nodes[0].quote : '"';
  57. node.value = node.nodes[0].value;
  58. }
  59. if (node.type === 'string') {
  60. node.value = node.value.trim();
  61. }
  62. return false;
  63. })
  64. .toString();
  65. }
  66. /**
  67. * @param {import('postcss').Declaration} decl
  68. * @param {normalize.Options} opts
  69. * @return {void}
  70. */
  71. function transformDecl(decl, opts) {
  72. decl.value = valueParser(decl.value)
  73. .walk((node) => {
  74. if (node.type !== 'function' || node.value.toLowerCase() !== 'url') {
  75. return false;
  76. }
  77. node.before = node.after = '';
  78. if (!node.nodes.length) {
  79. return false;
  80. }
  81. let url = node.nodes[0];
  82. let escaped;
  83. url.value = url.value.trim().replace(multiline, '');
  84. // Skip empty URLs
  85. // Empty URL function equals request to current stylesheet where it is declared
  86. if (url.value.length === 0) {
  87. /** @type {any} */ (url).quote = '';
  88. return false;
  89. }
  90. if (/^data:(.*)?,/i.test(url.value)) {
  91. return false;
  92. }
  93. if (!/^.+-extension:\//i.test(url.value)) {
  94. url.value = convert(url.value, opts);
  95. }
  96. if (escapeChars.test(url.value) && url.type === 'string') {
  97. escaped = url.value.replace(escapeChars, '\\$1');
  98. if (escaped.length < url.value.length + 2) {
  99. url.value = escaped;
  100. /** @type {valueParser.Node} */ (url).type = 'word';
  101. }
  102. } else {
  103. url.type = 'word';
  104. }
  105. return false;
  106. })
  107. .toString();
  108. }
  109. /** @typedef {normalize.Options} Options */
  110. /**
  111. * @type {import('postcss').PluginCreator<Options>}
  112. * @param {Options} opts
  113. * @return {import('postcss').Plugin}
  114. */
  115. function pluginCreator(opts) {
  116. opts = Object.assign(
  117. {},
  118. {
  119. normalizeProtocol: false,
  120. sortQueryParameters: false,
  121. stripHash: false,
  122. stripWWW: false,
  123. stripTextFragment: false,
  124. },
  125. opts
  126. );
  127. return {
  128. postcssPlugin: 'postcss-normalize-url',
  129. OnceExit(css) {
  130. css.walk((node) => {
  131. if (node.type === 'decl') {
  132. return transformDecl(node, opts);
  133. } else if (
  134. node.type === 'atrule' &&
  135. node.name.toLowerCase() === 'namespace'
  136. ) {
  137. return transformNamespace(node);
  138. }
  139. });
  140. },
  141. };
  142. }
  143. pluginCreator.postcss = true;
  144. module.exports = pluginCreator;