cacheMapUtil.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.CSS_FILE_STYLE = exports.ATTR_CACHE_MAP = void 0;
  7. exports.existPath = existPath;
  8. exports.getStyleAndHash = getStyleAndHash;
  9. exports.prepare = prepare;
  10. exports.reset = reset;
  11. exports.serialize = serialize;
  12. var _canUseDom = _interopRequireDefault(require("../../../../_util/canUseDom"));
  13. var _StyleContext = require("../../StyleContext");
  14. const ATTR_CACHE_MAP = exports.ATTR_CACHE_MAP = 'data-ant-cssinjs-cache-path';
  15. /**
  16. * This marks style from the css file.
  17. * Which means not exist in `<style />` tag.
  18. */
  19. const CSS_FILE_STYLE = exports.CSS_FILE_STYLE = '_FILE_STYLE__';
  20. function serialize(cachePathMap) {
  21. return Object.keys(cachePathMap).map(path => {
  22. const hash = cachePathMap[path];
  23. return `${path}:${hash}`;
  24. }).join(';');
  25. }
  26. let cachePathMap;
  27. let fromCSSFile = true;
  28. /**
  29. * @private Test usage only. Can save remove if no need.
  30. */
  31. function reset(mockCache) {
  32. let fromFile = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  33. cachePathMap = mockCache;
  34. fromCSSFile = fromFile;
  35. }
  36. function prepare() {
  37. var _a;
  38. if (!cachePathMap) {
  39. cachePathMap = {};
  40. if ((0, _canUseDom.default)()) {
  41. const div = document.createElement('div');
  42. div.className = ATTR_CACHE_MAP;
  43. div.style.position = 'fixed';
  44. div.style.visibility = 'hidden';
  45. div.style.top = '-9999px';
  46. document.body.appendChild(div);
  47. let content = getComputedStyle(div).content || '';
  48. content = content.replace(/^"/, '').replace(/"$/, '');
  49. // Fill data
  50. content.split(';').forEach(item => {
  51. const [path, hash] = item.split(':');
  52. cachePathMap[path] = hash;
  53. });
  54. // Remove inline record style
  55. const inlineMapStyle = document.querySelector(`style[${ATTR_CACHE_MAP}]`);
  56. if (inlineMapStyle) {
  57. fromCSSFile = false;
  58. (_a = inlineMapStyle.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(inlineMapStyle);
  59. }
  60. document.body.removeChild(div);
  61. }
  62. }
  63. }
  64. function existPath(path) {
  65. prepare();
  66. return !!cachePathMap[path];
  67. }
  68. function getStyleAndHash(path) {
  69. const hash = cachePathMap[path];
  70. let styleStr = null;
  71. if (hash && (0, _canUseDom.default)()) {
  72. if (fromCSSFile) {
  73. styleStr = CSS_FILE_STYLE;
  74. } else {
  75. const style = document.querySelector(`style[${_StyleContext.ATTR_MARK}="${cachePathMap[path]}"]`);
  76. if (style) {
  77. styleStr = style.innerHTML;
  78. } else {
  79. // Clean up since not exist anymore
  80. delete cachePathMap[path];
  81. }
  82. }
  83. }
  84. return [styleStr, hash];
  85. }