Dashboard sipadu mbip
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cache.esm.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import { StyleSheet } from '@emotion/sheet';
  2. import Stylis from '@emotion/stylis';
  3. import weakMemoize from '@emotion/weak-memoize';
  4. // https://github.com/thysultan/stylis.js/tree/master/plugins/rule-sheet
  5. // inlined to avoid umd wrapper and peerDep warnings/installing stylis
  6. // since we use stylis after closure compiler
  7. var delimiter = '/*|*/';
  8. var needle = delimiter + '}';
  9. function toSheet(block) {
  10. if (block) {
  11. Sheet.current.insert(block + '}');
  12. }
  13. }
  14. var Sheet = {
  15. current: null
  16. };
  17. var ruleSheet = function ruleSheet(context, content, selectors, parents, line, column, length, ns, depth, at) {
  18. switch (context) {
  19. // property
  20. case 1:
  21. {
  22. switch (content.charCodeAt(0)) {
  23. case 64:
  24. {
  25. // @import
  26. Sheet.current.insert(content + ';');
  27. return '';
  28. }
  29. // charcode for l
  30. case 108:
  31. {
  32. // charcode for b
  33. // this ignores label
  34. if (content.charCodeAt(2) === 98) {
  35. return '';
  36. }
  37. }
  38. }
  39. break;
  40. }
  41. // selector
  42. case 2:
  43. {
  44. if (ns === 0) return content + delimiter;
  45. break;
  46. }
  47. // at-rule
  48. case 3:
  49. {
  50. switch (ns) {
  51. // @font-face, @page
  52. case 102:
  53. case 112:
  54. {
  55. Sheet.current.insert(selectors[0] + content);
  56. return '';
  57. }
  58. default:
  59. {
  60. return content + (at === 0 ? delimiter : '');
  61. }
  62. }
  63. }
  64. case -2:
  65. {
  66. content.split(needle).forEach(toSheet);
  67. }
  68. }
  69. };
  70. var removeLabel = function removeLabel(context, content) {
  71. if (context === 1 && // charcode for l
  72. content.charCodeAt(0) === 108 && // charcode for b
  73. content.charCodeAt(2) === 98 // this ignores label
  74. ) {
  75. return '';
  76. }
  77. };
  78. var isBrowser = typeof document !== 'undefined';
  79. var rootServerStylisCache = {};
  80. var getServerStylisCache = isBrowser ? undefined : weakMemoize(function () {
  81. var getCache = weakMemoize(function () {
  82. return {};
  83. });
  84. var prefixTrueCache = {};
  85. var prefixFalseCache = {};
  86. return function (prefix) {
  87. if (prefix === undefined || prefix === true) {
  88. return prefixTrueCache;
  89. }
  90. if (prefix === false) {
  91. return prefixFalseCache;
  92. }
  93. return getCache(prefix);
  94. };
  95. });
  96. var createCache = function createCache(options) {
  97. if (options === undefined) options = {};
  98. var key = options.key || 'css';
  99. var stylisOptions;
  100. if (options.prefix !== undefined) {
  101. stylisOptions = {
  102. prefix: options.prefix
  103. };
  104. }
  105. var stylis = new Stylis(stylisOptions);
  106. if (process.env.NODE_ENV !== 'production') {
  107. // $FlowFixMe
  108. if (/[^a-z-]/.test(key)) {
  109. throw new Error("Emotion key must only contain lower case alphabetical characters and - but \"" + key + "\" was passed");
  110. }
  111. }
  112. var inserted = {}; // $FlowFixMe
  113. var container;
  114. if (isBrowser) {
  115. container = options.container || document.head;
  116. var nodes = document.querySelectorAll("style[data-emotion-" + key + "]");
  117. Array.prototype.forEach.call(nodes, function (node) {
  118. var attrib = node.getAttribute("data-emotion-" + key); // $FlowFixMe
  119. attrib.split(' ').forEach(function (id) {
  120. inserted[id] = true;
  121. });
  122. if (node.parentNode !== container) {
  123. container.appendChild(node);
  124. }
  125. });
  126. }
  127. var _insert;
  128. if (isBrowser) {
  129. stylis.use(options.stylisPlugins)(ruleSheet);
  130. _insert = function insert(selector, serialized, sheet, shouldCache) {
  131. var name = serialized.name;
  132. Sheet.current = sheet;
  133. if (process.env.NODE_ENV !== 'production' && serialized.map !== undefined) {
  134. var map = serialized.map;
  135. Sheet.current = {
  136. insert: function insert(rule) {
  137. sheet.insert(rule + map);
  138. }
  139. };
  140. }
  141. stylis(selector, serialized.styles);
  142. if (shouldCache) {
  143. cache.inserted[name] = true;
  144. }
  145. };
  146. } else {
  147. stylis.use(removeLabel);
  148. var serverStylisCache = rootServerStylisCache;
  149. if (options.stylisPlugins || options.prefix !== undefined) {
  150. stylis.use(options.stylisPlugins); // $FlowFixMe
  151. serverStylisCache = getServerStylisCache(options.stylisPlugins || rootServerStylisCache)(options.prefix);
  152. }
  153. var getRules = function getRules(selector, serialized) {
  154. var name = serialized.name;
  155. if (serverStylisCache[name] === undefined) {
  156. serverStylisCache[name] = stylis(selector, serialized.styles);
  157. }
  158. return serverStylisCache[name];
  159. };
  160. _insert = function _insert(selector, serialized, sheet, shouldCache) {
  161. var name = serialized.name;
  162. var rules = getRules(selector, serialized);
  163. if (cache.compat === undefined) {
  164. // in regular mode, we don't set the styles on the inserted cache
  165. // since we don't need to and that would be wasting memory
  166. // we return them so that they are rendered in a style tag
  167. if (shouldCache) {
  168. cache.inserted[name] = true;
  169. }
  170. if ( // using === development instead of !== production
  171. // because if people do ssr in tests, the source maps showing up would be annoying
  172. process.env.NODE_ENV === 'development' && serialized.map !== undefined) {
  173. return rules + serialized.map;
  174. }
  175. return rules;
  176. } else {
  177. // in compat mode, we put the styles on the inserted cache so
  178. // that emotion-server can pull out the styles
  179. // except when we don't want to cache it which was in Global but now
  180. // is nowhere but we don't want to do a major right now
  181. // and just in case we're going to leave the case here
  182. // it's also not affecting client side bundle size
  183. // so it's really not a big deal
  184. if (shouldCache) {
  185. cache.inserted[name] = rules;
  186. } else {
  187. return rules;
  188. }
  189. }
  190. };
  191. }
  192. if (process.env.NODE_ENV !== 'production') {
  193. // https://esbench.com/bench/5bf7371a4cd7e6009ef61d0a
  194. var commentStart = /\/\*/g;
  195. var commentEnd = /\*\//g;
  196. stylis.use(function (context, content) {
  197. switch (context) {
  198. case -1:
  199. {
  200. while (commentStart.test(content)) {
  201. commentEnd.lastIndex = commentStart.lastIndex;
  202. if (commentEnd.test(content)) {
  203. commentStart.lastIndex = commentEnd.lastIndex;
  204. continue;
  205. }
  206. throw new Error('Your styles have an unterminated comment ("/*" without corresponding "*/").');
  207. }
  208. commentStart.lastIndex = 0;
  209. break;
  210. }
  211. }
  212. });
  213. stylis.use(function (context, content, selectors) {
  214. switch (context) {
  215. case -1:
  216. {
  217. var flag = 'emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason';
  218. var unsafePseudoClasses = content.match(/(:first|:nth|:nth-last)-child/g);
  219. if (unsafePseudoClasses && cache.compat !== true) {
  220. unsafePseudoClasses.forEach(function (unsafePseudoClass) {
  221. var ignoreRegExp = new RegExp(unsafePseudoClass + ".*\\/\\* " + flag + " \\*\\/");
  222. var ignore = ignoreRegExp.test(content);
  223. if (unsafePseudoClass && !ignore) {
  224. console.error("The pseudo class \"" + unsafePseudoClass + "\" is potentially unsafe when doing server-side rendering. Try changing it to \"" + unsafePseudoClass.split('-child')[0] + "-of-type\".");
  225. }
  226. });
  227. }
  228. break;
  229. }
  230. }
  231. });
  232. }
  233. var cache = {
  234. key: key,
  235. sheet: new StyleSheet({
  236. key: key,
  237. container: container,
  238. nonce: options.nonce,
  239. speedy: options.speedy
  240. }),
  241. nonce: options.nonce,
  242. inserted: inserted,
  243. registered: {},
  244. insert: _insert
  245. };
  246. return cache;
  247. };
  248. export default createCache;