vite-plugin-theme_es_client.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import {
  2. __async,
  3. init_define_APP_INFO
  4. } from "./chunk-XY75H3MP.js";
  5. // dep:vite-plugin-theme_es_client
  6. init_define_APP_INFO();
  7. // node_modules/vite-plugin-theme/es/client.js
  8. init_define_APP_INFO();
  9. var globalField = "__VITE_THEME__";
  10. var styleTagId = "__VITE_PLUGIN_THEME__";
  11. var darkStyleTagId = "__VITE_PLUGIN_DARK_THEME__";
  12. var linkID = "__VITE_PLUGIN_THEME-ANTD_DARK_THEME_LINK__";
  13. var colorPluginOutputFileName = __COLOR_PLUGIN_OUTPUT_FILE_NAME__;
  14. var isProd = __PROD__;
  15. var colorPluginOptions = __COLOR_PLUGIN_OPTIONS__;
  16. var injectTo = colorPluginOptions.injectTo;
  17. var debounceThemeRender = debounce(200, renderTheme);
  18. var darkCssIsReady = false;
  19. (() => {
  20. if (!window[globalField]) {
  21. window[globalField] = {
  22. styleIdMap: /* @__PURE__ */ new Map(),
  23. styleRenderQueueMap: /* @__PURE__ */ new Map()
  24. };
  25. }
  26. setGlobalOptions("replaceStyleVariables", replaceStyleVariables);
  27. if (!getGlobalOptions("defaultOptions")) {
  28. setGlobalOptions("defaultOptions", colorPluginOptions);
  29. }
  30. })();
  31. function addCssToQueue(id, styleString) {
  32. const styleIdMap = getGlobalOptions("styleIdMap");
  33. if (!styleIdMap.get(id)) {
  34. window[globalField].styleRenderQueueMap.set(id, styleString);
  35. debounceThemeRender();
  36. }
  37. }
  38. function renderTheme() {
  39. const variables = getGlobalOptions("colorVariables");
  40. if (!variables) {
  41. return;
  42. }
  43. const styleRenderQueueMap = getGlobalOptions("styleRenderQueueMap");
  44. const styleDom = getStyleDom(styleTagId);
  45. let html = styleDom.innerHTML;
  46. for (const [id, css] of styleRenderQueueMap.entries()) {
  47. html += css;
  48. window[globalField].styleRenderQueueMap.delete(id);
  49. window[globalField].styleIdMap.set(id, css);
  50. }
  51. replaceCssColors(html, variables).then((processCss) => {
  52. appendCssToDom(styleDom, processCss, injectTo);
  53. });
  54. }
  55. function replaceStyleVariables(_0) {
  56. return __async(this, arguments, function* ({ colorVariables, customCssHandler }) {
  57. setGlobalOptions("colorVariables", colorVariables);
  58. const styleIdMap = getGlobalOptions("styleIdMap");
  59. const styleRenderQueueMap = getGlobalOptions("styleRenderQueueMap");
  60. if (!isProd) {
  61. for (const [id, css] of styleIdMap.entries()) {
  62. styleRenderQueueMap.set(id, css);
  63. }
  64. renderTheme();
  65. } else {
  66. try {
  67. const cssText = yield fetchCss(colorPluginOutputFileName);
  68. const styleDom = getStyleDom(styleTagId);
  69. const processCss = yield replaceCssColors(cssText, colorVariables, customCssHandler);
  70. appendCssToDom(styleDom, processCss, injectTo);
  71. } catch (error) {
  72. throw new Error(error);
  73. }
  74. }
  75. });
  76. }
  77. function loadDarkThemeCss() {
  78. return __async(this, null, function* () {
  79. const extractCss = __ANTD_DARK_PLUGIN_EXTRACT_CSS__;
  80. const isLoadLink = __ANTD_DARK_PLUGIN_LOAD_LINK__;
  81. if (darkCssIsReady || !extractCss) {
  82. return;
  83. }
  84. if (isLoadLink) {
  85. const linkTag = document.getElementById(linkID);
  86. if (linkTag) {
  87. linkTag.removeAttribute("disabled");
  88. linkTag.setAttribute("rel", "stylesheet");
  89. }
  90. } else {
  91. const colorPluginOutputFileName2 = __ANTD_DARK_PLUGIN_OUTPUT_FILE_NAME__;
  92. const cssText = yield fetchCss(colorPluginOutputFileName2);
  93. const styleDom = getStyleDom(darkStyleTagId);
  94. appendCssToDom(styleDom, cssText, injectTo);
  95. }
  96. darkCssIsReady = true;
  97. });
  98. }
  99. function replaceCssColors(css, colors, customCssHandler) {
  100. return __async(this, null, function* () {
  101. let retCss = css;
  102. const defaultOptions = getGlobalOptions("defaultOptions");
  103. const colorVariables = defaultOptions ? defaultOptions.colorVariables || [] : [];
  104. colorVariables.forEach(function(color, index) {
  105. const reg = new RegExp(color.replace(/,/g, ",\\s*").replace(/\s/g, "").replace("(", `\\(`).replace(")", `\\)`) + "([\\da-f]{2})?(\\b|\\)|,|\\s)?", "ig");
  106. retCss = retCss.replace(reg, colors[index] + "$1$2").replace("$1$2", "");
  107. if (customCssHandler && typeof customCssHandler === "function") {
  108. retCss = customCssHandler(retCss) || retCss;
  109. }
  110. });
  111. return retCss;
  112. });
  113. }
  114. function setGlobalOptions(key, value) {
  115. window[globalField][key] = value;
  116. }
  117. function getGlobalOptions(key) {
  118. return window[globalField][key];
  119. }
  120. function getStyleDom(id) {
  121. let style = document.getElementById(id);
  122. if (!style) {
  123. style = document.createElement("style");
  124. style.setAttribute("id", id);
  125. }
  126. return style;
  127. }
  128. function appendCssToDom(styleDom, cssText, appendTo = "body") {
  129. return __async(this, null, function* () {
  130. styleDom.innerHTML = cssText;
  131. if (appendTo === "head") {
  132. document.head.appendChild(styleDom);
  133. } else if (appendTo === "body") {
  134. document.body.appendChild(styleDom);
  135. } else if (appendTo === "body-prepend") {
  136. const firstChildren = document.body.firstChild;
  137. document.body.insertBefore(styleDom, firstChildren);
  138. }
  139. });
  140. }
  141. function fetchCss(fileName) {
  142. return new Promise((resolve, reject) => {
  143. const append = getGlobalOptions("appended");
  144. if (append) {
  145. setGlobalOptions("appended", false);
  146. resolve("");
  147. return;
  148. }
  149. const xhr = new XMLHttpRequest();
  150. xhr.onload = function() {
  151. if (xhr.readyState === 4) {
  152. if (xhr.status === 200) {
  153. resolve(xhr.responseText);
  154. } else {
  155. reject(xhr.status);
  156. }
  157. }
  158. };
  159. xhr.onerror = function(e) {
  160. reject(e);
  161. };
  162. xhr.ontimeout = function(e) {
  163. reject(e);
  164. };
  165. xhr.open("GET", fileName, true);
  166. xhr.send();
  167. });
  168. }
  169. function debounce(delay, fn) {
  170. let timer;
  171. return function(...args) {
  172. const ctx = this;
  173. clearTimeout(timer);
  174. timer = setTimeout(function() {
  175. fn.apply(ctx, args);
  176. }, delay);
  177. };
  178. }
  179. export {
  180. addCssToQueue,
  181. appendCssToDom,
  182. darkCssIsReady,
  183. darkStyleTagId,
  184. getGlobalOptions,
  185. getStyleDom,
  186. globalField,
  187. linkID,
  188. loadDarkThemeCss,
  189. replaceCssColors,
  190. replaceStyleVariables,
  191. setGlobalOptions,
  192. styleTagId
  193. };
  194. //# sourceMappingURL=vite-plugin-theme_es_client.js.map