extract-antd-no-reset.mjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import fs from "fs";
  2. import path from "path";
  3. import { fileURLToPath } from "node:url";
  4. const __filename = fileURLToPath(import.meta.url);
  5. const __dirname = path.dirname(__filename);
  6. // Paths
  7. const findNearestNodeModules = (dir) => {
  8. const nodeModulesPath = path.join(dir, "node_modules");
  9. if (fs.existsSync(nodeModulesPath)) {
  10. return nodeModulesPath;
  11. }
  12. const parentDir = path.dirname(dir);
  13. if (parentDir === dir) {
  14. throw new Error("No node_modules directory found");
  15. }
  16. return findNearestNodeModules(parentDir);
  17. };
  18. const nodeModulesPath = findNearestNodeModules(__dirname);
  19. const sourcePath = path.join(nodeModulesPath, "./antd/dist/antd.css");
  20. if (!fs.existsSync(sourcePath)) {
  21. throw new Error(`Source file not found: ${sourcePath}`);
  22. }
  23. let targetDirPath = path.join(nodeModulesPath, "@humansignal/editor/src/assets/styles");
  24. if (!fs.existsSync(targetDirPath)) {
  25. if (fs.existsSync(path.join(__dirname, "../../libs/editor/src/assets/styles"))) {
  26. targetDirPath = path.join(__dirname, "../../libs/editor/src/assets/styles");
  27. }
  28. }
  29. const targetPath = path.join(targetDirPath, "./antd-no-reset.css");
  30. // Read the source file
  31. const sourceContent = fs.readFileSync(sourcePath, "utf8");
  32. // Extract header and add our custom comments
  33. const header = `/*!
  34. *
  35. * antd v4.24.15 (without CSS reset)
  36. *
  37. * Copyright 2015-present, Alipay, Inc.
  38. * All rights reserved.
  39. *
  40. * Modified version to exclude global resets
  41. *
  42. */
  43. /* stylelint-disable */
  44. /* This is a modified version of antd.css that excludes global reset styles */
  45. `;
  46. // These are CSS reset selectors we want to remove
  47. const resetSelectors = [
  48. /^html,\s*body\s*{[\s\S]*?}/m,
  49. /^input::-ms-clear,\s*input::-ms-reveal\s*{[\s\S]*?}/m,
  50. /^\*,\s*\*::before,\s*\*::after\s*{[\s\S]*?}/m,
  51. /^html\s*{[\s\S]*?}/m,
  52. /^@-ms-viewport\s*{[\s\S]*?}/m,
  53. /^body\s*{[\s\S]*?}/m,
  54. /^\[tabindex='-1'\]:focus\s*{[\s\S]*?}/m,
  55. /^hr\s*{[\s\S]*?}/m,
  56. /^h1,\s*h2,\s*h3,\s*h4,\s*h5,\s*h6\s*{[\s\S]*?}/m,
  57. /^p\s*{[\s\S]*?}/m,
  58. /^abbr\[title\],\s*abbr\[data-original-title\]\s*{[\s\S]*?}/m,
  59. /^address\s*{[\s\S]*?}/m,
  60. /^input\[type='text'\],[\s\S]*?textarea\s*{[\s\S]*?}/m,
  61. /^ol,\s*ul,\s*dl\s*{[\s\S]*?}/m,
  62. /^ol ol,[\s\S]*?ul ol\s*{[\s\S]*?}/m,
  63. /^dt\s*{[\s\S]*?}/m,
  64. /^dd\s*{[\s\S]*?}/m,
  65. /^blockquote\s*{[\s\S]*?}/m,
  66. /^dfn\s*{[\s\S]*?}/m,
  67. /^b,\s*strong\s*{[\s\S]*?}/m,
  68. /^small\s*{[\s\S]*?}/m,
  69. /^sub,\s*sup\s*{[\s\S]*?}/m,
  70. /^sub\s*{[\s\S]*?}/m,
  71. /^sup\s*{[\s\S]*?}/m,
  72. /^a\s*{[\s\S]*?}/m,
  73. /^a:hover\s*{[\s\S]*?}/m,
  74. /^a:active\s*{[\s\S]*?}/m,
  75. /^a:active,\s*a:hover\s*{[\s\S]*?}/m,
  76. /^a:focus\s*{[\s\S]*?}/m,
  77. /^a\[disabled\]\s*{[\s\S]*?}/m,
  78. /^pre,\s*code,\s*kbd,\s*samp\s*{[\s\S]*?}/m,
  79. /^pre\s*{[\s\S]*?}/m,
  80. /^figure\s*{[\s\S]*?}/m,
  81. /^img\s*{[\s\S]*?}/m,
  82. /^a,\s*area,[\s\S]*?textarea\s*{[\s\S]*?}/m,
  83. /^table\s*{[\s\S]*?}/m,
  84. /^caption\s*{[\s\S]*?}/m,
  85. /^input,\s*button,[\s\S]*?textarea\s*{[\s\S]*?}/m,
  86. /^button,\s*input\s*{[\s\S]*?}/m,
  87. /^button,\s*select\s*{[\s\S]*?}/m,
  88. /^button,\s*html \[type="button"\],[\s\S]*?\[type="submit"\]\s*{[\s\S]*?}/m,
  89. /^button::-moz-focus-inner,[\s\S]*?\[type='submit'\]::-moz-focus-inner\s*{[\s\S]*?}/m,
  90. /^input\[type='radio'\],\s*input\[type='checkbox'\]\s*{[\s\S]*?}/m,
  91. /^input\[type='date'\],[\s\S]*?input\[type='month'\]\s*{[\s\S]*?}/m,
  92. /^textarea\s*{[\s\S]*?}/m,
  93. /^fieldset\s*{[\s\S]*?}/m,
  94. /^legend\s*{[\s\S]*?}/m,
  95. /^progress\s*{[\s\S]*?}/m,
  96. /^\[type='number'\]::-webkit-inner-spin-button,\s*\[type='number'\]::-webkit-outer-spin-button\s*{[\s\S]*?}/m,
  97. /^\[type='search'\]\s*{[\s\S]*?}/m,
  98. /^\[type='search'\]::-webkit-search-cancel-button,\s*\[type='search'\]::-webkit-search-decoration\s*{[\s\S]*?}/m,
  99. /^::-webkit-file-upload-button\s*{[\s\S]*?}/m,
  100. /^output\s*{[\s\S]*?}/m,
  101. /^summary\s*{[\s\S]*?}/m,
  102. /^template\s*{[\s\S]*?}/m,
  103. /^\[hidden\]\s*{[\s\S]*?}/m,
  104. /^mark\s*{[\s\S]*?}/m,
  105. /^::-moz-selection\s*{[\s\S]*?}/m,
  106. /^::selection\s*{[\s\S]*?}/m,
  107. ];
  108. // Process the content
  109. let modifiedContent = sourceContent;
  110. // Remove reset selectors one by one
  111. resetSelectors.forEach((selector) => {
  112. modifiedContent = modifiedContent.replace(selector, "");
  113. });
  114. // Clean up multiple blank lines
  115. modifiedContent = modifiedContent.replace(/\n{3,}/g, "\n\n");
  116. // Replace the original header with our custom header
  117. modifiedContent = modifiedContent.replace(/\/\*![\s\S]*?Alipay[\s\S]*?\*\//, header);
  118. // Remove any other stylelint-disable comments (except the one in our header)
  119. const headerEndPos = modifiedContent.indexOf("/* This is a modified version");
  120. const contentAfterHeader = modifiedContent.substring(headerEndPos);
  121. const contentWithoutStylelintComments = contentAfterHeader.replace(/\/\* stylelint-disable[\s\S]*?\*\//g, "");
  122. // Combine our header with the cleaned content
  123. modifiedContent = modifiedContent.substring(0, headerEndPos) + contentWithoutStylelintComments;
  124. // Clean up multiple blank lines again after removing comments
  125. modifiedContent = modifiedContent.replace(/\n{3,}/g, "\n\n");
  126. // Write the result to the target file
  127. fs.writeFileSync(targetPath, modifiedContent, "utf8");
  128. console.log("Generated antd-no-reset.css successfully!");