cacheInvalidation.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. Object.defineProperty(exports, "hasContentChanged", {
  6. enumerable: true,
  7. get: function() {
  8. return hasContentChanged;
  9. }
  10. });
  11. const _crypto = /*#__PURE__*/ _interop_require_default(require("crypto"));
  12. const _sharedState = /*#__PURE__*/ _interop_require_wildcard(require("./sharedState"));
  13. function _interop_require_default(obj) {
  14. return obj && obj.__esModule ? obj : {
  15. default: obj
  16. };
  17. }
  18. function _getRequireWildcardCache(nodeInterop) {
  19. if (typeof WeakMap !== "function") return null;
  20. var cacheBabelInterop = new WeakMap();
  21. var cacheNodeInterop = new WeakMap();
  22. return (_getRequireWildcardCache = function(nodeInterop) {
  23. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  24. })(nodeInterop);
  25. }
  26. function _interop_require_wildcard(obj, nodeInterop) {
  27. if (!nodeInterop && obj && obj.__esModule) {
  28. return obj;
  29. }
  30. if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
  31. return {
  32. default: obj
  33. };
  34. }
  35. var cache = _getRequireWildcardCache(nodeInterop);
  36. if (cache && cache.has(obj)) {
  37. return cache.get(obj);
  38. }
  39. var newObj = {};
  40. var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
  41. for(var key in obj){
  42. if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
  43. var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
  44. if (desc && (desc.get || desc.set)) {
  45. Object.defineProperty(newObj, key, desc);
  46. } else {
  47. newObj[key] = obj[key];
  48. }
  49. }
  50. }
  51. newObj.default = obj;
  52. if (cache) {
  53. cache.set(obj, newObj);
  54. }
  55. return newObj;
  56. }
  57. /**
  58. * Calculate the hash of a string.
  59. *
  60. * This doesn't need to be cryptographically secure or
  61. * anything like that since it's used only to detect
  62. * when the CSS changes to invalidate the context.
  63. *
  64. * This is wrapped in a try/catch because it's really dependent
  65. * on how Node itself is build and the environment and OpenSSL
  66. * version / build that is installed on the user's machine.
  67. *
  68. * Based on the environment this can just outright fail.
  69. *
  70. * See https://github.com/nodejs/node/issues/40455
  71. *
  72. * @param {string} str
  73. */ function getHash(str) {
  74. try {
  75. return _crypto.default.createHash("md5").update(str, "utf-8").digest("binary");
  76. } catch (err) {
  77. return "";
  78. }
  79. }
  80. function hasContentChanged(sourcePath, root) {
  81. let css = root.toString();
  82. // We only care about files with @tailwind directives
  83. // Other files use an existing context
  84. if (!css.includes("@tailwind")) {
  85. return false;
  86. }
  87. let existingHash = _sharedState.sourceHashMap.get(sourcePath);
  88. let rootHash = getHash(css);
  89. let didChange = existingHash !== rootHash;
  90. _sharedState.sourceHashMap.set(sourcePath, rootHash);
  91. return didChange;
  92. }