css-variable-sync.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * ZombieJ: Since we still need mainly maintain the `default.less`. Create a script that generate
  3. * `variable.less` from the `default.less`
  4. */
  5. const fse = require('fs-extra');
  6. const path = require('path');
  7. const chalk = require('chalk');
  8. const folderPath = path.resolve(__dirname, '..', 'components', 'style', 'themes');
  9. const targetPath = path.resolve(folderPath, 'variable.less');
  10. const defaultContent = fse.readFileSync(path.resolve(folderPath, 'default.less'), 'utf8');
  11. // const variableContent = fse.readFileSync(
  12. // path.resolve(__dirname, '..', 'components', 'style', 'themes', 'variable.less'),
  13. // 'utf8',
  14. // );
  15. let variableContent = defaultContent;
  16. function replaceVariable(key, value) {
  17. variableContent = variableContent.replace(new RegExp(`@${key}:[^;]*;`), `@${key}: ${value};`);
  18. }
  19. function replaceVariableContent(key, content) {
  20. const lines = variableContent.split(/\n/);
  21. const startIndex = lines.findIndex(line => line.includes(`[CSS-VARIABLE-REPLACE-BEGIN: ${key}]`));
  22. const endIndex = lines.findIndex(line => line.includes(`[CSS-VARIABLE-REPLACE-END: ${key}]`));
  23. if (startIndex !== -1 && endIndex !== -1) {
  24. variableContent = [...lines.slice(0, startIndex), content, ...lines.slice(endIndex + 1)].join(
  25. '\n',
  26. );
  27. }
  28. }
  29. replaceVariable('theme', 'variable');
  30. replaceVariableContent(
  31. 'html-variables',
  32. `
  33. html {
  34. @base-primary: @blue-6;
  35. // ========= Primary Color =========
  36. --@{ant-prefix}-primary-color: @base-primary;
  37. --@{ant-prefix}-primary-color-hover: color(~\`colorPalette('@{base-primary}', 5) \`);
  38. --@{ant-prefix}-primary-color-active: color(~\`colorPalette('@{base-primary}', 7) \`);
  39. --@{ant-prefix}-primary-color-outline: fade(@base-primary, @outline-fade);
  40. // Legacy
  41. @legacy-primary-1: color(~\`colorPalette('@{base-primary}', 1) \`);
  42. --@{ant-prefix}-primary-1: @legacy-primary-1;
  43. --@{ant-prefix}-primary-2: color(~\`colorPalette('@{base-primary}', 2) \`);
  44. --@{ant-prefix}-primary-3: color(~\`colorPalette('@{base-primary}', 3) \`);
  45. --@{ant-prefix}-primary-4: color(~\`colorPalette('@{base-primary}', 4) \`);
  46. --@{ant-prefix}-primary-5: color(~\`colorPalette('@{base-primary}', 5) \`);
  47. --@{ant-prefix}-primary-6: @base-primary;
  48. --@{ant-prefix}-primary-7: color(~\`colorPalette('@{base-primary}', 7) \`);
  49. // Deprecated
  50. --@{ant-prefix}-primary-color-deprecated-pure: ~'';
  51. --@{ant-prefix}-primary-color-deprecated-l-35: lighten(@base-primary, 35%);
  52. --@{ant-prefix}-primary-color-deprecated-l-20: lighten(@base-primary, 20%);
  53. --@{ant-prefix}-primary-color-deprecated-t-20: tint(@base-primary, 20%);
  54. --@{ant-prefix}-primary-color-deprecated-t-50: tint(@base-primary, 50%);
  55. --@{ant-prefix}-primary-color-deprecated-f-12: fade(@base-primary, 12%);
  56. --@{ant-prefix}-primary-color-active-deprecated-f-30: fade(@legacy-primary-1, 30%);
  57. --@{ant-prefix}-primary-color-active-deprecated-d-02: darken(@legacy-primary-1, 2%);
  58. // ========= Success Color =========
  59. --@{ant-prefix}-success-color: @green-6;
  60. --@{ant-prefix}-success-color-hover: color(~\`colorPalette('@{green-6}', 5) \`);
  61. --@{ant-prefix}-success-color-active: color(~\`colorPalette('@{green-6}', 7) \`);
  62. --@{ant-prefix}-success-color-outline: fade(@green-6, @outline-fade);
  63. --@{ant-prefix}-success-color-deprecated-bg: ~\`colorPalette('@{green-6}', 1) \`;
  64. --@{ant-prefix}-success-color-deprecated-border: ~\`colorPalette('@{green-6}', 3) \`;
  65. // ========== Error Color ==========
  66. --@{ant-prefix}-error-color: @red-5;
  67. --@{ant-prefix}-error-color-hover: color(~\`colorPalette('@{red-5}', 5) \`);
  68. --@{ant-prefix}-error-color-active: color(~\`colorPalette('@{red-5}', 7) \`);
  69. --@{ant-prefix}-error-color-outline: fade(@red-5, @outline-fade);
  70. --@{ant-prefix}-error-color-deprecated-bg: ~\`colorPalette('@{red-5}', 1) \`;
  71. --@{ant-prefix}-error-color-deprecated-border: ~\`colorPalette('@{red-5}', 3) \`;
  72. // ========= Warning Color =========
  73. --@{ant-prefix}-warning-color: @gold-6;
  74. --@{ant-prefix}-warning-color-hover: color(~\`colorPalette('@{gold-6}', 5) \`);
  75. --@{ant-prefix}-warning-color-active: color(~\`colorPalette('@{gold-6}', 7) \`);
  76. --@{ant-prefix}-warning-color-outline: fade(@gold-6, @outline-fade);
  77. --@{ant-prefix}-warning-color-deprecated-bg: ~\`colorPalette('@{gold-6}', 1) \`;
  78. --@{ant-prefix}-warning-color-deprecated-border: ~\`colorPalette('@{gold-6}', 3) \`;
  79. // ========== Info Color ===========
  80. --@{ant-prefix}-info-color: @base-primary;
  81. --@{ant-prefix}-info-color-deprecated-bg: ~\`colorPalette('@{base-primary}', 1) \`;
  82. --@{ant-prefix}-info-color-deprecated-border: ~\`colorPalette('@{base-primary}', 3) \`;
  83. }
  84. `.trim(),
  85. );
  86. // >>> Primary
  87. replaceVariable('primary-color', "~'var(--@{ant-prefix}-primary-color)'");
  88. replaceVariable('primary-color-hover', "~'var(--@{ant-prefix}-primary-color-hover)'");
  89. replaceVariable('primary-color-active', "~'var(--@{ant-prefix}-primary-color-active)'");
  90. replaceVariable('primary-color-outline', "~'var(--@{ant-prefix}-primary-color-outline)'");
  91. replaceVariable('processing-color', '@primary-color');
  92. // >>> Info
  93. replaceVariable('info-color', "~'var(--@{ant-prefix}-info-color)'");
  94. replaceVariable('info-color-deprecated-bg', "~'var(--@{ant-prefix}-info-color-deprecated-bg)'");
  95. replaceVariable(
  96. 'info-color-deprecated-border',
  97. "~'var(--@{ant-prefix}-info-color-deprecated-border)'",
  98. );
  99. // >>> Success
  100. replaceVariable('success-color', "~'var(--@{ant-prefix}-success-color)'");
  101. replaceVariable('success-color-hover', "~'var(--@{ant-prefix}-success-color-hover)'");
  102. replaceVariable('success-color-active', "~'var(--@{ant-prefix}-success-color-active)'");
  103. replaceVariable('success-color-outline', "~'var(--@{ant-prefix}-success-color-outline)'");
  104. replaceVariable(
  105. 'success-color-deprecated-bg',
  106. "~'var(--@{ant-prefix}-success-color-deprecated-bg)'",
  107. );
  108. replaceVariable(
  109. 'success-color-deprecated-border',
  110. "~'var(--@{ant-prefix}-success-color-deprecated-border)'",
  111. );
  112. // >>> Warning
  113. replaceVariable('warning-color', "~'var(--@{ant-prefix}-warning-color)'");
  114. replaceVariable('warning-color-hover', "~'var(--@{ant-prefix}-warning-color-hover)'");
  115. replaceVariable('warning-color-active', "~'var(--@{ant-prefix}-warning-color-active)'");
  116. replaceVariable('warning-color-outline', "~'var(--@{ant-prefix}-warning-color-outline)'");
  117. replaceVariable(
  118. 'warning-color-deprecated-bg',
  119. "~'var(--@{ant-prefix}-warning-color-deprecated-bg)'",
  120. );
  121. replaceVariable(
  122. 'warning-color-deprecated-border',
  123. "~'var(--@{ant-prefix}-warning-color-deprecated-border)'",
  124. );
  125. // >>> Error
  126. replaceVariable('error-color', "~'var(--@{ant-prefix}-error-color)'");
  127. replaceVariable('error-color-hover', "~'var(--@{ant-prefix}-error-color-hover)'");
  128. replaceVariable('error-color-active', "~'var(--@{ant-prefix}-error-color-active)'");
  129. replaceVariable('error-color-outline', "~'var(--@{ant-prefix}-error-color-outline)'");
  130. replaceVariable('error-color-deprecated-bg', "~'var(--@{ant-prefix}-error-color-deprecated-bg)'");
  131. replaceVariable(
  132. 'error-color-deprecated-border',
  133. "~'var(--@{ant-prefix}-error-color-deprecated-border)'",
  134. );
  135. // >>> Primary Level Color
  136. replaceVariable('primary-1', "~'var(--@{ant-prefix}-primary-1)'");
  137. replaceVariable('primary-2', "~'var(--@{ant-prefix}-primary-2)'");
  138. replaceVariable('primary-3', "~'var(--@{ant-prefix}-primary-3)'");
  139. replaceVariable('primary-4', "~'var(--@{ant-prefix}-primary-4)'");
  140. replaceVariable('primary-5', "~'var(--@{ant-prefix}-primary-5)'");
  141. replaceVariable('primary-6', "~'var(--@{ant-prefix}-primary-6)'");
  142. replaceVariable('primary-7', "~'var(--@{ant-prefix}-primary-7)'");
  143. // Link
  144. replaceVariable('link-hover-color', '@primary-color-hover');
  145. replaceVariable('link-active-color', '@primary-color-active');
  146. replaceVariable(
  147. 'table-selected-row-hover-bg',
  148. "~'var(--@{ant-prefix}-primary-color-active-deprecated-d-02)'",
  149. );
  150. replaceVariable(
  151. 'picker-basic-cell-hover-with-range-color',
  152. "~'var(--@{ant-prefix}-primary-color-deprecated-l-35)'",
  153. );
  154. replaceVariable(
  155. 'picker-date-hover-range-border-color',
  156. "~'var(--@{ant-prefix}-primary-color-deprecated-l-20)'",
  157. );
  158. replaceVariable(
  159. 'calendar-column-active-bg',
  160. "~'var(--@{ant-prefix}-primary-color-active-deprecated-f-30)'",
  161. );
  162. replaceVariable(
  163. 'slider-handle-color-focus',
  164. "~'var(--@{ant-prefix}-primary-color-deprecated-t-20)'",
  165. );
  166. replaceVariable(
  167. 'slider-handle-color-focus-shadow',
  168. "~'var(--@{ant-prefix}-primary-color-deprecated-f-12)'",
  169. );
  170. replaceVariable(
  171. 'slider-dot-border-color-active',
  172. "~'var(--@{ant-prefix}-primary-color-deprecated-t-50)'",
  173. );
  174. replaceVariable(
  175. 'transfer-item-selected-hover-bg',
  176. "~'var(--@{ant-prefix}-primary-color-active-deprecated-d-02)'",
  177. );
  178. replaceVariable('alert-success-border-color', '@success-color-deprecated-border');
  179. replaceVariable('alert-success-bg-color', '@success-color-deprecated-bg');
  180. replaceVariable('alert-info-border-color', '@info-color-deprecated-border');
  181. replaceVariable('alert-info-bg-color', '@info-color-deprecated-bg');
  182. replaceVariable('alert-warning-border-color', '@warning-color-deprecated-border');
  183. replaceVariable('alert-warning-bg-color', '@warning-color-deprecated-bg');
  184. replaceVariable('alert-error-border-color', '@error-color-deprecated-border');
  185. replaceVariable('alert-error-bg-color', '@error-color-deprecated-bg');
  186. fse.writeFileSync(targetPath, variableContent, 'utf8');
  187. // eslint-disable-next-line no-console
  188. console.log(chalk.green('Success! Replaced path:'), targetPath);