|
1 | | -import { DEV } from 'esm-env'; |
2 | | -import { effect } from '../reactivity/effects.js'; |
3 | | - |
4 | | -var css_counter = new Map(); |
5 | | - |
6 | 1 | /** |
7 | 2 | * @param {Node} anchor |
8 | 3 | * @param {{ hash: string, code: string }} css |
9 | 4 | */ |
10 | 5 | export function append_styles(anchor, css) { |
11 | | - const maybe_append_styles = () => { |
12 | | - var root = anchor.getRootNode(); |
13 | | - |
14 | | - var target = /** @type {ShadowRoot} */ (root).host |
15 | | - ? /** @type {ShadowRoot} */ (root) |
16 | | - : /** @type {Document} */ (root).head ?? /** @type {Document} */ (root.ownerDocument).head; |
17 | | - |
18 | | - if (!target.querySelector('#' + css.hash)) { |
19 | | - const style = document.createElement('style'); |
20 | | - style.id = css.hash; |
21 | | - style.textContent = css.code; |
22 | | - |
23 | | - target.appendChild(style); |
24 | | - } |
25 | | - }; |
26 | | - |
27 | | - // Use an effect to ensure `anchor` is in the DOM, otherwise getRootNode() will yield wrong results |
28 | | - effect(() => { |
29 | | - // In dev, always check the DOM, so that styles can be replaced with HMR |
30 | | - if (DEV) { |
31 | | - maybe_append_styles(); |
32 | | - return; |
33 | | - } |
34 | | - // Otherwise, for prod we can use the css object as a key and count the usage to skip the lookup |
35 | | - var count = css_counter.get(css) ?? 0; |
36 | | - |
37 | | - css_counter.set(css, count + 1); |
| 6 | + var root = anchor.getRootNode(); |
38 | 7 |
|
39 | | - if (count > 0) return; |
| 8 | + var target = /** @type {ShadowRoot} */ (root).host |
| 9 | + ? /** @type {ShadowRoot} */ (root) |
| 10 | + : /** @type {Document} */ (root).head ?? /** @type {Document} */ (root.ownerDocument).head; |
40 | 11 |
|
41 | | - maybe_append_styles(); |
| 12 | + if (!target.querySelector('#' + css.hash)) { |
| 13 | + const style = document.createElement('style'); |
| 14 | + style.id = css.hash; |
| 15 | + style.textContent = css.code; |
42 | 16 |
|
43 | | - return () => { |
44 | | - var count = css_counter.get(css) - 1; |
45 | | - css_counter.set(css, count); |
46 | | - if (count === 0) css_counter.delete(css); |
47 | | - }; |
48 | | - }); |
| 17 | + target.appendChild(style); |
| 18 | + } |
49 | 19 | } |
0 commit comments