@fluentic/style 0.1.0-beta.14 → 0.1.0-beta.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/atomic/className.ts +4 -0
- package/atomic/rule.ts +15 -7
- package/atomic/utils/selector.ts +12 -3
- package/builder/classname/class_name_builder.ts +1 -1
- package/builder/data/const.ts +2 -0
- package/builder/data/data.ts +1 -1
- package/builder/data/merge/scope.ts +7 -0
- package/builder/data/merge/style.ts +12 -3
- package/builder/data/state.ts +1 -0
- package/builder/selector_override.ts +1 -1
- package/builder/selector_override_core.ts +17 -3
- package/builder/types/alias.ts +1 -1
- package/compiler/extract/extract.ts +21 -75
- package/compiler/extract/types.ts +4 -1
- package/compiler/transform/debug/plugin.ts +22 -0
- package/compiler/transform/evaluator/evaluator.ts +4 -1
- package/compiler/transform/extract/chain/chain.ts +82 -22
- package/compiler/transform/extract/chain/extract_chain.ts +2 -0
- package/compiler/transform/extract/plugin.ts +51 -25
- package/compiler/transform/extract/utils/imports.ts +4 -1
- package/compiler/transform/extract/utils/replacement.ts +2 -2
- package/compiler/transform/utils/babel.ts +1 -1
- package/compiler/utils/constants.ts +1 -0
- package/config/config/css.ts +5 -3
- package/config/configure.ts +4 -0
- package/config/document.ts +23 -0
- package/css/globalSheet.ts +66 -0
- package/css/index.ts +4 -1
- package/css/sheet.ts +34 -14
- package/dist/{className-yhJCGT0F.js → className-CC5PHcXI.js} +9 -2
- package/dist/className-CC5PHcXI.js.map +1 -0
- package/dist/{combine-CC9bY-hu.js → combine-BpOQWCtM.js} +5 -5
- package/dist/combine-BpOQWCtM.js.map +1 -0
- package/dist/compiler/index.mjs +1 -1
- package/dist/{compiler-Ct5xGIeu.mjs → compiler-AOb_VaKQ.mjs} +464 -166
- package/dist/compiler-AOb_VaKQ.mjs.map +1 -0
- package/dist/config/index.js +2 -2
- package/dist/{config-CCgsVvyR.js → config-B70LIQhi.js} +2 -2
- package/dist/{config-CCgsVvyR.js.map → config-B70LIQhi.js.map} +1 -1
- package/dist/{config-DZKR6gH1.js → config-BF-mxuPk.js} +4 -4
- package/dist/{config-DZKR6gH1.js.map → config-BF-mxuPk.js.map} +1 -1
- package/dist/{config-BiiMS4rU.js → config-B_OQYZSm.js} +25 -4
- package/dist/config-B_OQYZSm.js.map +1 -0
- package/dist/{css-WaHZ6gxv.js → css-C7tcD3IO.js} +611 -213
- package/dist/css-C7tcD3IO.js.map +1 -0
- package/dist/css-Cdxkur4h.js +414 -0
- package/dist/css-Cdxkur4h.js.map +1 -0
- package/dist/{css-IecKRNxs.js → css-RohOR3Vq.js} +112 -73
- package/dist/css-RohOR3Vq.js.map +1 -0
- package/dist/css.js +2 -2
- package/dist/css_extra-BC1Z_hDy.js +8 -0
- package/dist/{css_extra-DDeJc2Cu.js.map → css_extra-BC1Z_hDy.js.map} +1 -1
- package/dist/{css_extra-DDeJc2Cu.js → css_extra-CuzJtAR_.js} +2 -2
- package/dist/css_extra-CuzJtAR_.js.map +1 -0
- package/dist/{data-BgZmL55Y.js → data-Bw3Xod5J.js} +28 -19
- package/dist/data-Bw3Xod5J.js.map +1 -0
- package/dist/{debug-D-1hhd0G.js → debug-CVO3xx4C.js} +6 -9
- package/dist/debug-CVO3xx4C.js.map +1 -0
- package/dist/dev/rsc.js +134 -25
- package/dist/dev/rsc.js.map +1 -1
- package/dist/{dev-CBRbVaeq.js → dev-BnQXPLQB.js} +6 -6
- package/dist/{dev-CBRbVaeq.js.map → dev-BnQXPLQB.js.map} +1 -1
- package/dist/dev.js +1 -1
- package/dist/dialect/extracted/index.js +1 -1
- package/dist/dialect/index.js +1 -1
- package/dist/{elementMarker-CJcfpKL1.js → elementMarker-BrHXg161.js} +334 -53
- package/dist/elementMarker-BrHXg161.js.map +1 -0
- package/dist/entry/dev/adapter/preact.js +1 -1
- package/dist/entry/dev/adapter/react.js +1 -1
- package/dist/entry/dev/adapter/solid.js +1 -1
- package/dist/entry/dev/css.js +3 -3
- package/dist/entry/dev/jsx-dev-runtime.js +2 -2
- package/dist/entry/dev/jsx-runtime.js +2 -2
- package/dist/entry/dev.js +11 -11
- package/dist/entry/prod/adapter/preact.js +1 -1
- package/dist/entry/prod/adapter/react.js +1 -1
- package/dist/entry/prod/adapter/solid.js +1 -1
- package/dist/entry/prod/css.js +3 -3
- package/dist/entry/prod/extract.js.map +1 -1
- package/dist/entry/prod.js +3 -3
- package/dist/entry/rsc-dev/adapter/preact.js +1 -1
- package/dist/entry/rsc-dev/adapter/react.js +1 -1
- package/dist/entry/rsc-dev/adapter/solid.js +1 -1
- package/dist/entry/rsc-dev/css.js +4 -3
- package/dist/entry/rsc-dev/dev.js +129 -24
- package/dist/entry/rsc-dev/dev.js.map +1 -1
- package/dist/entry/rsc-dev/jsx-dev-runtime.js +5 -4
- package/dist/entry/rsc-dev/jsx-dev-runtime.js.map +1 -1
- package/dist/entry/rsc-dev/jsx-runtime.js +2 -2
- package/dist/entry/rsc-dev.js +8 -7
- package/dist/entry/rsc-dev.js.map +1 -1
- package/dist/entry/rsc-prod/css.js +4 -4
- package/dist/entry/rsc-prod.js +4 -4
- package/dist/{rules-Dz-szpPu.js → env-D6daex-9.js} +64 -19
- package/dist/env-D6daex-9.js.map +1 -0
- package/dist/{getClassName-DtkplEIt.js → getClassName-5yYUibKw.js} +2 -2
- package/dist/{getClassName-DtkplEIt.js.map → getClassName-5yYUibKw.js.map} +1 -1
- package/dist/getClassName-9CsXbNls.js.map +1 -1
- package/dist/{getClassName-CeoxISNW.js → getClassName-B3azD1TO.js} +6 -4
- package/dist/getClassName-B3azD1TO.js.map +1 -0
- package/dist/{getClassName-BNe3Ycyy.js → getClassName-m5DOkMFQ.js} +4 -4
- package/dist/{getClassName-BNe3Ycyy.js.map → getClassName-m5DOkMFQ.js.map} +1 -1
- package/dist/{global-UOHFwqHN.js → global-DeIYtcdl.js} +5 -2
- package/dist/{global-UOHFwqHN.js.map → global-DeIYtcdl.js.map} +1 -1
- package/dist/{css-BhoclXaw.js → globalSheet-CdgVpvCb.js} +185 -426
- package/dist/globalSheet-CdgVpvCb.js.map +1 -0
- package/dist/{hash-DlXmmPPm.js → hash-DCJkLls_.js} +4 -4
- package/dist/hash-DCJkLls_.js.map +1 -0
- package/dist/index.js +9 -9
- package/dist/jsx/jsx-dev-runtime.js +1 -1
- package/dist/jsx/jsx-dev-runtime.server.js +3 -3
- package/dist/jsx/jsx-dev-runtime.server.js.map +1 -1
- package/dist/jsx/jsx-runtime.js +1 -1
- package/dist/jsx/jsx-runtime.server.js +4 -4
- package/dist/jsx/jsx-runtime.server.js.map +1 -1
- package/dist/{jsx-8TwYuvgM.js → jsx-CvloSSpb.js} +4 -4
- package/dist/{jsx-8TwYuvgM.js.map → jsx-CvloSSpb.js.map} +1 -1
- package/dist/jsx_runtime-pt7DOZOE.js +13 -0
- package/dist/jsx_runtime-pt7DOZOE.js.map +1 -0
- package/dist/{loader-BVebGteg.mjs → loader-fAuIjIik.mjs} +3 -3
- package/dist/{loader-BVebGteg.mjs.map → loader-fAuIjIik.mjs.map} +1 -1
- package/dist/{misc-Bs2DgdG7.mjs → misc-CyZgGtc9.mjs} +3 -3
- package/dist/{misc-Bs2DgdG7.mjs.map → misc-CyZgGtc9.mjs.map} +1 -1
- package/dist/{namedTokens-CwniouKk.js → namedTokens-DwgmUMBM.js} +2 -2
- package/dist/{namedTokens-CwniouKk.js.map → namedTokens-DwgmUMBM.js.map} +1 -1
- package/dist/plugin/farm/index.mjs +3 -3
- package/dist/plugin/nextjs/index.mjs +4 -4
- package/dist/plugin/nextjs/loader.mjs +7 -5
- package/dist/plugin/nextjs/loader.mjs.map +1 -1
- package/dist/plugin/parcel/index.cjs +1 -1
- package/dist/plugin/parcel/index.mjs +2 -2
- package/dist/plugin/parcel/optimizer.cjs +1 -1
- package/dist/plugin/parcel/optimizer.mjs +2 -2
- package/dist/plugin/parcel/resolver.cjs +1 -1
- package/dist/plugin/parcel/resolver.mjs +2 -2
- package/dist/plugin/parcel/runtime.cjs +1 -1
- package/dist/plugin/parcel/runtime.mjs +1 -1
- package/dist/plugin/parcel/transformer.cjs +1 -1
- package/dist/plugin/parcel/transformer.mjs +2 -2
- package/dist/plugin/rspack/index.mjs +2 -2
- package/dist/plugin/vite/index.mjs +3 -3
- package/dist/plugin/webpack/index.mjs +2 -2
- package/dist/plugin/webpack/loader.mjs +2 -2
- package/dist/presets/tailwind/index.js +3 -3
- package/dist/{registry-D1n78rd7.mjs → registry-BMijh1CM.mjs} +2 -2
- package/dist/{registry-D1n78rd7.mjs.map → registry-BMijh1CM.mjs.map} +1 -1
- package/dist/{resolveClassNameRuntime-BJcykzdl.js → resolveClassNameRuntime-B6vKNL5y.js} +2 -2
- package/dist/{resolveClassNameRuntime-BJcykzdl.js.map → resolveClassNameRuntime-B6vKNL5y.js.map} +1 -1
- package/dist/server.js +87 -17
- package/dist/server.js.map +1 -1
- package/dist/{shared-D4-AKGn6.mjs → shared-BfASWZo1.mjs} +4 -4
- package/dist/{shared-D4-AKGn6.mjs.map → shared-BfASWZo1.mjs.map} +1 -1
- package/dist/{shared-C9XQd8p_.cjs → shared-DY0kuBcN.cjs} +460 -162
- package/dist/shared-DY0kuBcN.cjs.map +1 -0
- package/dist/sheet-B-5idGtZ.js +9 -0
- package/dist/sheet-B-5idGtZ.js.map +1 -0
- package/dist/{config-TyjNpRrV.js → sheet-C6ymGhKV.js} +23 -2
- package/dist/sheet-C6ymGhKV.js.map +1 -0
- package/dist/sheet-D4f9KYPf.js +1 -0
- package/dist/{sidecar-CZTYX2vA.mjs → sidecar-cCzTDkzg.mjs} +3 -3
- package/dist/{sidecar-CZTYX2vA.mjs.map → sidecar-cCzTDkzg.mjs.map} +1 -1
- package/dist/{style-CmXHCPF4.js → style-a6ccye6R.js} +12 -12
- package/dist/style-a6ccye6R.js.map +1 -0
- package/dist/styleStore-Ceoaqa5U.js +45 -0
- package/dist/styleStore-Ceoaqa5U.js.map +1 -0
- package/dist/{env-C8TRVSyb.js → styleStore-D1IEmq1v.js} +435 -63
- package/dist/styleStore-D1IEmq1v.js.map +1 -0
- package/dist/{token-DMrBbat8.js → token-Ci73NE6h.js} +2 -2
- package/dist/{token-DMrBbat8.js.map → token-Ci73NE6h.js.map} +1 -1
- package/dist/{trace-C7EzEj2Q.js → trace-DDsvwdF0.js} +3 -3
- package/dist/{trace-C7EzEj2Q.js.map → trace-DDsvwdF0.js.map} +1 -1
- package/dist/{trace-DzCntvK4.js → trace-uHVvnYiw.js} +2 -2
- package/dist/{trace-DzCntvK4.js.map → trace-uHVvnYiw.js.map} +1 -1
- package/dist/{transform-CSZMklMb.js → transform-BkxIouPX.js} +2 -2
- package/dist/{transform-CSZMklMb.js.map → transform-BkxIouPX.js.map} +1 -1
- package/dist/types/atomic/className.d.ts +2 -2
- package/dist/types/atomic/utils/selector.d.ts +2 -1
- package/dist/types/builder/data/const.d.ts +1 -0
- package/dist/types/builder/data/data.d.ts +1 -1
- package/dist/types/builder/data/state.d.ts +1 -0
- package/dist/types/builder/selector_override.d.ts +1 -1
- package/dist/types/builder/selector_override_core.d.ts +2 -0
- package/dist/types/builder/types/alias.d.ts +2 -2
- package/dist/types/compiler/extract/types.d.ts +7 -1
- package/dist/types/compiler/transform/extract/chain/extract_chain.d.ts +2 -0
- package/dist/types/compiler/utils/constants.d.ts +1 -0
- package/dist/types/config/config/css.d.ts +2 -1
- package/dist/types/config/configure.d.ts +2 -0
- package/dist/types/config/document.d.ts +3 -0
- package/dist/types/css/globalSheet.d.ts +24 -0
- package/dist/types/css/index.d.ts +4 -1
- package/dist/types/css/sheet.d.ts +3 -2
- package/dist/types/entry/rsc_dev/css.d.ts +1 -0
- package/dist/types/runtime/rsc/constants.d.ts +2 -0
- package/dist/types/runtime/rsc/dev.d.ts +5 -0
- package/dist/types/runtime/rsc/getClassName.d.ts +3 -0
- package/dist/types/runtime/rsc/globalSheet.d.ts +22 -0
- package/dist/types/runtime/rsc/observer.d.ts +5 -0
- package/dist/types/runtime/rsc/styleStore.d.ts +2 -0
- package/dist/types/runtime/sheet/document.d.ts +3 -0
- package/dist/types/sheet/ordered.d.ts +4 -0
- package/dist/types/sheet/ordering.d.ts +15 -0
- package/dist/types/sheet/types.d.ts +7 -0
- package/dist/types/style/important.d.ts +2 -1
- package/dist/types/style/style.d.ts +2 -2
- package/dist/{utils-CoMwEfhG.mjs → utils-BO6Ix8t5.mjs} +4 -4
- package/dist/{utils-CoMwEfhG.mjs.map → utils-BO6Ix8t5.mjs.map} +1 -1
- package/dist/{utils-B7QcJ-Yw.js → utils-CB70kIh6.js} +3 -3
- package/dist/{utils-B7QcJ-Yw.js.map → utils-CB70kIh6.js.map} +1 -1
- package/dist/{utils-BYpkJ-uE.mjs → utils-EG9gh2g3.mjs} +3 -3
- package/dist/{utils-BYpkJ-uE.mjs.map → utils-EG9gh2g3.mjs.map} +1 -1
- package/dist/{utils-D4KMqCu2.js → utils-sG7IWYP6.js} +1089 -646
- package/dist/utils-sG7IWYP6.js.map +1 -0
- package/dist/utils.js +3 -3
- package/dist/{value-yd2SZ1XQ.js → value-B-ThmbY-.js} +16 -9
- package/dist/value-B-ThmbY-.js.map +1 -0
- package/dist/{webpack-B6z1UHh9.mjs → webpack-WXAJ3IYn.mjs} +5 -5
- package/dist/{webpack-B6z1UHh9.mjs.map → webpack-WXAJ3IYn.mjs.map} +1 -1
- package/entry/dev/sheet.ts +4 -1
- package/entry/rsc_dev/css.ts +2 -0
- package/jsx/rsc/jsx_dev_runtime.ts +11 -2
- package/jsx/rsc/jsx_runtime.ts +14 -3
- package/package.json +1 -1
- package/plugin/nextjs/loader.ts +18 -2
- package/runtime/core/elementMarker.ts +9 -3
- package/runtime/rsc/constants.ts +3 -0
- package/runtime/rsc/dev.ts +31 -15
- package/runtime/rsc/getClassName.ts +6 -0
- package/runtime/rsc/globalSheet.ts +130 -0
- package/runtime/rsc/observer.ts +118 -15
- package/runtime/rsc/styleStore.ts +12 -36
- package/runtime/sheet/document.ts +28 -0
- package/runtime/sheet/global-prod.ts +3 -5
- package/runtime/sheet/global.ts +3 -5
- package/runtime/sheet/rule.ts +4 -2
- package/runtime/sheet/rules.ts +3 -0
- package/sheet/dev/dev_sort.ts +5 -1
- package/sheet/dev/index.ts +23 -3
- package/sheet/ordered.ts +188 -0
- package/sheet/ordering.ts +138 -0
- package/sheet/prod.ts +17 -5
- package/sheet/types.ts +4 -0
- package/style/important.ts +3 -1
- package/style/style.ts +1 -1
- package/utils/trace.ts +1 -0
- package/dist/className-yhJCGT0F.js.map +0 -1
- package/dist/combine-CC9bY-hu.js.map +0 -1
- package/dist/compiler-Ct5xGIeu.mjs.map +0 -1
- package/dist/config-BiiMS4rU.js.map +0 -1
- package/dist/config-TyjNpRrV.js.map +0 -1
- package/dist/createTransformElement-BGznW-Mu.js +0 -34
- package/dist/createTransformElement-BGznW-Mu.js.map +0 -1
- package/dist/css-BhoclXaw.js.map +0 -1
- package/dist/css-IecKRNxs.js.map +0 -1
- package/dist/css-WaHZ6gxv.js.map +0 -1
- package/dist/data-BgZmL55Y.js.map +0 -1
- package/dist/debug-D-1hhd0G.js.map +0 -1
- package/dist/elementMarker-CJcfpKL1.js.map +0 -1
- package/dist/env-C8TRVSyb.js.map +0 -1
- package/dist/getClassName-CeoxISNW.js.map +0 -1
- package/dist/global-CWYOol4o.js +0 -14
- package/dist/global-CWYOol4o.js.map +0 -1
- package/dist/hash-DlXmmPPm.js.map +0 -1
- package/dist/jsx_runtime-CPojW0KD.js +0 -12
- package/dist/jsx_runtime-CPojW0KD.js.map +0 -1
- package/dist/rules-Dz-szpPu.js.map +0 -1
- package/dist/shared-C9XQd8p_.cjs.map +0 -1
- package/dist/sheet-LU89fcH_.js +0 -7
- package/dist/sheet-LU89fcH_.js.map +0 -1
- package/dist/style-CmXHCPF4.js.map +0 -1
- package/dist/styleStore-B0x4Ahpx.js +0 -60
- package/dist/styleStore-B0x4Ahpx.js.map +0 -1
- package/dist/styleStore-BrpgLNgc.js +0 -57
- package/dist/styleStore-BrpgLNgc.js.map +0 -1
- package/dist/utils-D4KMqCu2.js.map +0 -1
- package/dist/value-yd2SZ1XQ.js.map +0 -1
package/runtime/rsc/observer.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { LayerDefaultLayers } from '../../atomic/layer';
|
|
|
2
2
|
import { isDebugData } from '../../builder/data/debug';
|
|
3
3
|
import { CSS_CONFIG } from '../../config/config/css';
|
|
4
4
|
import { DEV_CONFIG } from '../../config/config/dev';
|
|
5
|
+
import { getRuntimeDocument, onRuntimeDocumentChange } from '../../config/document';
|
|
5
6
|
import type { SheetCallsite, SheetRule } from '../../sheet';
|
|
6
7
|
import {
|
|
7
8
|
getElementMarkerClassNameFromRule,
|
|
@@ -9,23 +10,43 @@ import {
|
|
|
9
10
|
isElementMarkerRule,
|
|
10
11
|
} from '../core/elementMarker';
|
|
11
12
|
import { getGlobalSheet } from '../sheet';
|
|
12
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
ELEMENT_CSS_DATA_ATTR,
|
|
15
|
+
GLOBAL_RULES_ATTR,
|
|
16
|
+
GLOBAL_SEED_ATTR,
|
|
17
|
+
PRECOLLECT_LINK_TAG_ATTR,
|
|
18
|
+
SEED_STYLE_TAG_ATTR,
|
|
19
|
+
SEED_STYLE_TAG_HREF,
|
|
20
|
+
} from './constants';
|
|
13
21
|
import type { RscStylePayloadRule } from './getClassName';
|
|
14
22
|
|
|
15
23
|
let observer: MutationObserver | null = null;
|
|
24
|
+
let observerDocument: Document | null = null;
|
|
25
|
+
let observerRequested = false;
|
|
16
26
|
let scheduled = false;
|
|
17
27
|
let flushId = 0;
|
|
18
28
|
|
|
19
29
|
const pending = new Set<Element>();
|
|
20
30
|
const inserted = new Set<string>();
|
|
31
|
+
const globalRevisions = new Map<string, string>();
|
|
32
|
+
const globalPayloads = new Set<Element>();
|
|
21
33
|
|
|
22
34
|
export function startRscDevObserver(root?: ParentNode) {
|
|
23
|
-
|
|
35
|
+
observerRequested = true;
|
|
36
|
+
const document = getRuntimeDocument();
|
|
37
|
+
if (!document) return;
|
|
38
|
+
if (observerDocument !== document) {
|
|
39
|
+
stopRscDevObserver();
|
|
40
|
+
observerRequested = true;
|
|
41
|
+
}
|
|
24
42
|
if (observer) return;
|
|
43
|
+
const Observer = document.defaultView?.MutationObserver;
|
|
44
|
+
if (!Observer) return;
|
|
45
|
+
observerDocument = document;
|
|
25
46
|
|
|
26
47
|
const targetRoot = root ?? document;
|
|
27
48
|
|
|
28
|
-
observer = new
|
|
49
|
+
observer = new Observer((mutations) => {
|
|
29
50
|
let changed = false;
|
|
30
51
|
|
|
31
52
|
for (const mutation of mutations) {
|
|
@@ -33,10 +54,11 @@ export function startRscDevObserver(root?: ParentNode) {
|
|
|
33
54
|
const target = mutation.target;
|
|
34
55
|
|
|
35
56
|
if (
|
|
36
|
-
target
|
|
37
|
-
target.hasAttribute(ELEMENT_CSS_DATA_ATTR)
|
|
57
|
+
target.nodeType === 1 &&
|
|
58
|
+
((target as Element).hasAttribute(ELEMENT_CSS_DATA_ATTR) ||
|
|
59
|
+
(target as Element).hasAttribute(GLOBAL_RULES_ATTR))
|
|
38
60
|
) {
|
|
39
|
-
|
|
61
|
+
collectPending(target as Element);
|
|
40
62
|
changed = true;
|
|
41
63
|
}
|
|
42
64
|
|
|
@@ -44,7 +66,14 @@ export function startRscDevObserver(root?: ParentNode) {
|
|
|
44
66
|
}
|
|
45
67
|
|
|
46
68
|
mutation.addedNodes.forEach((node) => {
|
|
47
|
-
if (node.nodeType !==
|
|
69
|
+
if (node.nodeType !== 1) {
|
|
70
|
+
const target = mutation.target;
|
|
71
|
+
if (target.nodeType === 1 && (target as Element).hasAttribute(GLOBAL_RULES_ATTR)) {
|
|
72
|
+
globalPayloads.add(target as Element);
|
|
73
|
+
changed = true;
|
|
74
|
+
}
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
48
77
|
|
|
49
78
|
if (collectPending(node as Element)) {
|
|
50
79
|
changed = true;
|
|
@@ -59,7 +88,7 @@ export function startRscDevObserver(root?: ParentNode) {
|
|
|
59
88
|
childList: true,
|
|
60
89
|
attributes: true,
|
|
61
90
|
subtree: true,
|
|
62
|
-
attributeFilter: [ELEMENT_CSS_DATA_ATTR],
|
|
91
|
+
attributeFilter: [ELEMENT_CSS_DATA_ATTR, GLOBAL_RULES_ATTR],
|
|
63
92
|
});
|
|
64
93
|
|
|
65
94
|
collectPending(targetRoot);
|
|
@@ -67,31 +96,43 @@ export function startRscDevObserver(root?: ParentNode) {
|
|
|
67
96
|
}
|
|
68
97
|
|
|
69
98
|
export function stopRscDevObserver() {
|
|
99
|
+
observerRequested = false;
|
|
70
100
|
observer?.disconnect();
|
|
71
101
|
observer = null;
|
|
102
|
+
observerDocument = null;
|
|
72
103
|
|
|
73
104
|
pending.clear();
|
|
105
|
+
inserted.clear();
|
|
106
|
+
globalRevisions.clear();
|
|
107
|
+
globalPayloads.clear();
|
|
74
108
|
scheduled = false;
|
|
75
109
|
}
|
|
76
110
|
|
|
111
|
+
onRuntimeDocumentChange(() => {
|
|
112
|
+
const active = observerRequested;
|
|
113
|
+
stopRscDevObserver();
|
|
114
|
+
if (active) startRscDevObserver();
|
|
115
|
+
});
|
|
116
|
+
|
|
77
117
|
function collectPending(root: ParentNode | Element): boolean {
|
|
78
|
-
const before = pending.size;
|
|
118
|
+
const before = pending.size + globalPayloads.size;
|
|
79
119
|
|
|
80
120
|
const collectElement = (element: Element) => {
|
|
121
|
+
if (element.hasAttribute(GLOBAL_RULES_ATTR)) globalPayloads.add(element);
|
|
81
122
|
if (!element.hasAttribute(ELEMENT_CSS_DATA_ATTR)) return;
|
|
82
123
|
|
|
83
124
|
pending.add(element);
|
|
84
125
|
};
|
|
85
126
|
|
|
86
|
-
if (root
|
|
87
|
-
collectElement(root);
|
|
127
|
+
if (root.nodeType === 1) {
|
|
128
|
+
collectElement(root as Element);
|
|
88
129
|
}
|
|
89
130
|
|
|
90
|
-
root.querySelectorAll?.(`[${ELEMENT_CSS_DATA_ATTR}]`).forEach((element) => {
|
|
131
|
+
root.querySelectorAll?.(`[${ELEMENT_CSS_DATA_ATTR}], [${GLOBAL_RULES_ATTR}]`).forEach((element) => {
|
|
91
132
|
collectElement(element);
|
|
92
133
|
});
|
|
93
134
|
|
|
94
|
-
return pending.size !== before;
|
|
135
|
+
return pending.size + globalPayloads.size !== before;
|
|
95
136
|
}
|
|
96
137
|
|
|
97
138
|
function scheduleFlush() {
|
|
@@ -106,11 +147,30 @@ function flush() {
|
|
|
106
147
|
|
|
107
148
|
scheduled = false;
|
|
108
149
|
|
|
109
|
-
if (!
|
|
150
|
+
if (!observerDocument || observerDocument !== getRuntimeDocument()) {
|
|
151
|
+
pending.clear();
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (!pending.size && !globalPayloads.size) return;
|
|
110
156
|
|
|
111
157
|
const sheet = getGlobalSheet();
|
|
112
158
|
sheet.updateLayers(CSS_CONFIG.layers || LayerDefaultLayers);
|
|
113
159
|
|
|
160
|
+
const adopted: Element[] = [];
|
|
161
|
+
for (const element of globalPayloads) {
|
|
162
|
+
const batches = parseGlobalRulePayload(element.textContent);
|
|
163
|
+
if (!batches) continue;
|
|
164
|
+
if (!sheet.replaceRules) continue;
|
|
165
|
+
for (const batch of batches) {
|
|
166
|
+
if (globalRevisions.get(batch.owner) === batch.revision) continue;
|
|
167
|
+
sheet.replaceRules('rsc-global:' + batch.owner, batch.rules);
|
|
168
|
+
globalRevisions.set(batch.owner, batch.revision);
|
|
169
|
+
}
|
|
170
|
+
adopted.push(element);
|
|
171
|
+
}
|
|
172
|
+
globalPayloads.clear();
|
|
173
|
+
|
|
114
174
|
const elements = Array.from(pending);
|
|
115
175
|
pending.clear();
|
|
116
176
|
|
|
@@ -138,13 +198,26 @@ function flush() {
|
|
|
138
198
|
}
|
|
139
199
|
|
|
140
200
|
sheet.flush();
|
|
201
|
+
for (const payload of adopted) {
|
|
202
|
+
const id = payload.getAttribute(GLOBAL_RULES_ATTR);
|
|
203
|
+
observerDocument.querySelectorAll(`[${GLOBAL_SEED_ATTR}]`).forEach((seed) => {
|
|
204
|
+
if (seed.getAttribute(GLOBAL_SEED_ATTR) === id) {
|
|
205
|
+
seed.removeAttribute(GLOBAL_SEED_ATTR);
|
|
206
|
+
seed.textContent = '';
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
// React owns these nodes in Flight. Keep the empty shells for reconciliation.
|
|
210
|
+
payload.removeAttribute(GLOBAL_RULES_ATTR);
|
|
211
|
+
payload.textContent = '';
|
|
212
|
+
}
|
|
141
213
|
cleanupInitialStyle();
|
|
142
214
|
|
|
143
215
|
void id;
|
|
144
216
|
}
|
|
145
217
|
|
|
146
218
|
function cleanupInitialStyle() {
|
|
147
|
-
|
|
219
|
+
const document = getRuntimeDocument();
|
|
220
|
+
if (!document) return;
|
|
148
221
|
if (document.querySelector(`[${ELEMENT_CSS_DATA_ATTR}]`)) return;
|
|
149
222
|
|
|
150
223
|
document.querySelectorAll(getRscDevInitialStyleSelector()).forEach(
|
|
@@ -184,6 +257,12 @@ function normalizePayloadItem(value: unknown): SheetRule | null {
|
|
|
184
257
|
const rule = value as RscStylePayloadRule;
|
|
185
258
|
|
|
186
259
|
if (typeof rule.key !== 'string' || typeof rule.css !== 'string') return null;
|
|
260
|
+
if (rule.importantNative !== undefined && typeof rule.importantNative !== 'boolean') return null;
|
|
261
|
+
if (rule.sheet !== undefined && typeof rule.sheet !== 'boolean') return null;
|
|
262
|
+
if (
|
|
263
|
+
rule.globalSheet !== undefined &&
|
|
264
|
+
(!rule.globalSheet || typeof rule.globalSheet.id !== 'string' || !Number.isFinite(rule.globalSheet.order))
|
|
265
|
+
) return null;
|
|
187
266
|
if (rule.important !== undefined && typeof rule.important !== 'boolean') return null;
|
|
188
267
|
if (
|
|
189
268
|
rule.layer !== undefined && rule.layer !== false && rule.layer !== 'style' && rule.layer !== 'sheet' &&
|
|
@@ -197,6 +276,9 @@ function normalizePayloadItem(value: unknown): SheetRule | null {
|
|
|
197
276
|
css: rule.css,
|
|
198
277
|
priority: rule.priority,
|
|
199
278
|
important: rule.important,
|
|
279
|
+
importantNative: rule.importantNative,
|
|
280
|
+
sheet: rule.sheet,
|
|
281
|
+
globalSheet: rule.globalSheet,
|
|
200
282
|
layer: rule.layer,
|
|
201
283
|
callsite: normalizePayloadCallsite(rule.callsite),
|
|
202
284
|
debug: rule.debug,
|
|
@@ -243,3 +325,24 @@ function removeElementMarkerClass(element: Element, rule: SheetRule) {
|
|
|
243
325
|
element.removeAttribute('class');
|
|
244
326
|
}
|
|
245
327
|
}
|
|
328
|
+
|
|
329
|
+
export function parseGlobalRulePayload(
|
|
330
|
+
value: string | null,
|
|
331
|
+
): Array<{ owner: string; revision: string; rules: SheetRule[]; }> | null {
|
|
332
|
+
try {
|
|
333
|
+
const batches: unknown = JSON.parse(value ?? '');
|
|
334
|
+
if (!Array.isArray(batches)) return null;
|
|
335
|
+
const result: Array<{ owner: string; revision: string; rules: SheetRule[]; }> = [];
|
|
336
|
+
for (const batch of batches) {
|
|
337
|
+
if (
|
|
338
|
+
!batch || typeof batch.owner !== 'string' || typeof batch.revision !== 'string' || !Array.isArray(batch.rules)
|
|
339
|
+
) return null;
|
|
340
|
+
const rules = batch.rules.map(normalizePayloadItem);
|
|
341
|
+
if (rules.some((rule: SheetRule | null) => !rule)) return null;
|
|
342
|
+
result.push({ owner: batch.owner, revision: batch.revision, rules });
|
|
343
|
+
}
|
|
344
|
+
return result;
|
|
345
|
+
} catch {
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { CSS_CONFIG } from '../../config/config/css';
|
|
2
|
+
import { DEV_CONFIG } from '../../config/config/dev';
|
|
3
3
|
import { RUNTIME_CONFIG } from '../../config/config/runtime';
|
|
4
4
|
import type { SheetRule } from '../../sheet';
|
|
5
|
-
import {
|
|
6
|
-
createSheetLayerState,
|
|
7
|
-
getSheetLayerName,
|
|
8
|
-
getSheetLayerNames,
|
|
9
|
-
getSheetRuleLayerPriority,
|
|
10
|
-
} from '../../sheet/utils';
|
|
5
|
+
import { serializeOrderedRules } from '../../sheet/ordering';
|
|
11
6
|
import { clearGlobalData, getGlobalData, globalData } from '../../utils/global';
|
|
12
7
|
import { IS_SERVER_RUNTIME } from '../env';
|
|
13
8
|
|
|
@@ -53,34 +48,15 @@ export function getRscStyleCss(): string {
|
|
|
53
48
|
if (!RUNTIME_CONFIG.isRSC) return '';
|
|
54
49
|
if (!IS_SERVER_RUNTIME) return '';
|
|
55
50
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if (ruleLayer === 'sheet') hasSheetLayer = true;
|
|
66
|
-
if (typeof ruleLayer === 'object') namedLayers.add(ruleLayer.name);
|
|
67
|
-
const layerName = typeof ruleLayer === 'object'
|
|
68
|
-
? ruleLayer.name
|
|
69
|
-
: ruleLayer === 'sheet'
|
|
70
|
-
? getSheetLayerName()
|
|
71
|
-
: ruleLayer === false || !isStyleLayerEnabled()
|
|
72
|
-
? null
|
|
73
|
-
: layerState.getName(getSheetRuleLayerPriority(rule.priority, rule.important));
|
|
74
|
-
|
|
75
|
-
return layerName ? getLayerBlockCss(layerName, rule.css) : rule.css;
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
const layerOrderCss = getLayerOrderCss(
|
|
79
|
-
CSS_CONFIG.layers ?? CSS_CONFIG_DEFAULT.layers ?? [],
|
|
80
|
-
getSheetLayerNames(layerState, hasSheetLayer, namedLayers),
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
return joinLayerCss(layerOrderCss, cssItems);
|
|
51
|
+
return serializeRscRules(getRscStyleRules());
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function getRscStyleRules(): readonly SheetRule[] {
|
|
55
|
+
return getGlobalData<RscStyleStore>(GLOBAL_KEY)?.rules ?? [];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function serializeRscRules(rules: readonly SheetRule[]): string {
|
|
59
|
+
return serializeOrderedRules(rules, CSS_CONFIG, DEV_CONFIG.stylePriorityMode);
|
|
84
60
|
}
|
|
85
61
|
|
|
86
62
|
function getRscStyleStore(): RscStyleStore {
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { getRuntimeDocument, onRuntimeDocumentChange } from '../../config/document';
|
|
2
|
+
import type { SheetOptions, StyleSheet } from '../../sheet/types';
|
|
3
|
+
import { createNoopSheet } from '../../sheet/utils/noop';
|
|
4
|
+
import { clearGlobalData, getGlobalData, globalData, setGlobalData } from '../../utils/global';
|
|
5
|
+
|
|
6
|
+
const GLOBAL_KEY = 'runtime.sheet.global';
|
|
7
|
+
const state = globalData<{ document: Document | null; }>('runtime.sheet.document', () => ({
|
|
8
|
+
document: getRuntimeDocument(),
|
|
9
|
+
}));
|
|
10
|
+
|
|
11
|
+
function resetSheet() {
|
|
12
|
+
getGlobalData<StyleSheet>(GLOBAL_KEY)?.destroy?.();
|
|
13
|
+
clearGlobalData(GLOBAL_KEY);
|
|
14
|
+
state.document = getRuntimeDocument();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
onRuntimeDocumentChange(resetSheet);
|
|
18
|
+
|
|
19
|
+
export function getDocumentSheet(create: (options: SheetOptions) => StyleSheet): StyleSheet {
|
|
20
|
+
const document = getRuntimeDocument();
|
|
21
|
+
if (state.document !== document) resetSheet();
|
|
22
|
+
return globalData(GLOBAL_KEY, () => document ? create({ document }) : createNoopSheet());
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function setDocumentSheet(sheet: StyleSheet) {
|
|
26
|
+
state.document = getRuntimeDocument();
|
|
27
|
+
setGlobalData(GLOBAL_KEY, sheet);
|
|
28
|
+
}
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import type { StyleSheet } from '../../sheet';
|
|
2
2
|
import { createProdSheet } from '../../sheet/prod';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
const GLOBAL_KEY = 'runtime.sheet.global';
|
|
3
|
+
import { getDocumentSheet, setDocumentSheet } from './document';
|
|
6
4
|
|
|
7
5
|
export function getGlobalSheet(): StyleSheet {
|
|
8
|
-
return
|
|
6
|
+
return getDocumentSheet(createProdSheet);
|
|
9
7
|
}
|
|
10
8
|
|
|
11
9
|
export function setGlobalSheet(sheet: StyleSheet): void {
|
|
12
|
-
|
|
10
|
+
setDocumentSheet(sheet);
|
|
13
11
|
}
|
package/runtime/sheet/global.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import { createStyleSheet } from '../../sheet';
|
|
2
2
|
import type { StyleSheet } from '../../sheet';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
const GLOBAL_KEY = 'runtime.sheet.global';
|
|
3
|
+
import { getDocumentSheet, setDocumentSheet } from './document';
|
|
6
4
|
|
|
7
5
|
export function getGlobalSheet(): StyleSheet {
|
|
8
|
-
return
|
|
6
|
+
return getDocumentSheet(createStyleSheet);
|
|
9
7
|
}
|
|
10
8
|
|
|
11
9
|
export function setGlobalSheet(sheet: StyleSheet): void {
|
|
12
|
-
|
|
10
|
+
setDocumentSheet(sheet);
|
|
13
11
|
}
|
package/runtime/sheet/rule.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { buildAtomicRule, getAtomicRuleLayerPriority } from '../../atomic/rule';
|
|
|
2
2
|
import type { CreateSheetOptions } from '../../builder/data';
|
|
3
3
|
import { BUILDER_TYPE_SCOPE } from '../../builder/data/const';
|
|
4
4
|
import type { RuntimeItem, StateItem } from '../../builder/data/state';
|
|
5
|
-
import { CSS_CONFIG,
|
|
5
|
+
import { CSS_CONFIG, resolveSheetLayerOption } from '../../config/config/css';
|
|
6
6
|
import type { SheetRule } from '../../sheet';
|
|
7
7
|
import { isStyleTokenOverrideData } from '../../style/token';
|
|
8
8
|
import { getStateItemAtRuleRef } from '../core/cache/stateItem';
|
|
@@ -49,6 +49,8 @@ export function createRuntimeSheetRules(
|
|
|
49
49
|
dedupe: runtimeItem.dedupe,
|
|
50
50
|
priority: layerPriority,
|
|
51
51
|
important: runtimeItem.important,
|
|
52
|
+
importantNative: runtimeItem.importantNative,
|
|
53
|
+
sheet: !!sheetOptions,
|
|
52
54
|
layer: getRuntimeRuleLayer(sheetOptions),
|
|
53
55
|
css: ruleCss,
|
|
54
56
|
callsite: runtimeItem.callsite,
|
|
@@ -77,5 +79,5 @@ export function createRuntimeSheetRule(item: StateItem): RuntimeSheetRule | null
|
|
|
77
79
|
|
|
78
80
|
function getRuntimeRuleLayer(sheetOptions: CreateSheetOptions | null): SheetRule['layer'] {
|
|
79
81
|
if (!sheetOptions) return 'style';
|
|
80
|
-
return resolveSheetLayerOption(sheetOptions.layer ??
|
|
82
|
+
return resolveSheetLayerOption(sheetOptions.layer ?? false, CSS_CONFIG.layerNamespace || 'css');
|
|
81
83
|
}
|
package/runtime/sheet/rules.ts
CHANGED
|
@@ -131,6 +131,9 @@ function createSheetRulePayload(rule: RuntimeSheetRule): SheetRule {
|
|
|
131
131
|
debugField: rule.debugField || null,
|
|
132
132
|
priority: rule.priority,
|
|
133
133
|
important: rule.important,
|
|
134
|
+
importantNative: rule.importantNative,
|
|
135
|
+
sheet: rule.sheet,
|
|
136
|
+
globalSheet: rule.globalSheet,
|
|
134
137
|
layer: rule.layer,
|
|
135
138
|
};
|
|
136
139
|
}
|
package/sheet/dev/dev_sort.ts
CHANGED
|
@@ -219,7 +219,11 @@ function ensureGroup(
|
|
|
219
219
|
}
|
|
220
220
|
|
|
221
221
|
function getLayerRank(layer: NonNullable<SheetRule['layer']>) {
|
|
222
|
-
return layer === 'style'
|
|
222
|
+
return layer === 'style' || (typeof layer === 'object' && layer.name === getStyleLayerName())
|
|
223
|
+
? 0
|
|
224
|
+
: layer === 'sheet' || typeof layer === 'object'
|
|
225
|
+
? 1
|
|
226
|
+
: 2;
|
|
223
227
|
}
|
|
224
228
|
|
|
225
229
|
function createGroupTag(
|
package/sheet/dev/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { CSS_CONFIG, CSS_CONFIG_DEFAULT } from '../../config/config/css';
|
|
2
2
|
import { DEV_CONFIG } from '../../config/config/dev';
|
|
3
3
|
import { globalData } from '../../utils/global';
|
|
4
|
+
import { createOrderedSheet, refreshOrderedSheets } from '../ordered';
|
|
4
5
|
import type { SheetOptions, SheetRule, StyleSheet } from '../types';
|
|
5
6
|
import { createDevLayerSheet } from './dev_layer';
|
|
6
7
|
import { createDevSortSheet } from './dev_sort';
|
|
@@ -22,10 +23,14 @@ export { createDevLayerSheet, getDevSourcemapTags, refreshDevSourcemapTags } fro
|
|
|
22
23
|
export { createDevSortSheet } from './dev_sort';
|
|
23
24
|
|
|
24
25
|
export function createDevSheet(options: SheetOptions = {}): StyleSheet {
|
|
26
|
+
return createOrderedSheet(createDevSheetBase, options, true);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function createDevSheetBase(options: SheetOptions = {}): StyleSheet {
|
|
25
30
|
let activeMode = DEV_CONFIG.stylePriorityMode;
|
|
26
31
|
let active = createDevSheetForMode(activeMode, options);
|
|
27
32
|
let activeLayers: readonly string[] = CSS_CONFIG.layers ?? CSS_CONFIG_DEFAULT.layers ?? [];
|
|
28
|
-
const rules: Array<SheetRule | string> = [];
|
|
33
|
+
const rules: Array<SheetRule | string | { owner: string; rules: SheetRule[]; }> = [];
|
|
29
34
|
const inserted = new Set<string>();
|
|
30
35
|
const state: DevSheetState = {
|
|
31
36
|
rebuild,
|
|
@@ -49,6 +54,13 @@ export function createDevSheet(options: SheetOptions = {}): StyleSheet {
|
|
|
49
54
|
active.insert(rule);
|
|
50
55
|
},
|
|
51
56
|
|
|
57
|
+
replaceRules(owner: string, next: SheetRule[]) {
|
|
58
|
+
const group = rules.find((rule) => typeof rule === 'object' && 'owner' in rule && rule.owner === owner);
|
|
59
|
+
if (group && typeof group === 'object' && 'owner' in group) group.rules = next;
|
|
60
|
+
else rules.push({ owner, rules: next });
|
|
61
|
+
rebuild();
|
|
62
|
+
},
|
|
63
|
+
|
|
52
64
|
flush() {
|
|
53
65
|
ensureActiveMode();
|
|
54
66
|
active.flush();
|
|
@@ -74,7 +86,10 @@ export function createDevSheet(options: SheetOptions = {}): StyleSheet {
|
|
|
74
86
|
active.updateLayers(activeLayers);
|
|
75
87
|
|
|
76
88
|
for (let i = 0, len = rules.length; i < len; i++) {
|
|
77
|
-
|
|
89
|
+
const rule = rules[i];
|
|
90
|
+
if (typeof rule === 'object' && 'owner' in rule) {
|
|
91
|
+
for (const item of rule.rules) active.insert(item);
|
|
92
|
+
} else active.insert(rule);
|
|
78
93
|
}
|
|
79
94
|
|
|
80
95
|
active.flush();
|
|
@@ -83,6 +98,7 @@ export function createDevSheet(options: SheetOptions = {}): StyleSheet {
|
|
|
83
98
|
|
|
84
99
|
export function refreshDevStyleTags() {
|
|
85
100
|
DEV_SHEETS.forEach((sheet) => sheet.rebuild());
|
|
101
|
+
refreshOrderedSheets();
|
|
86
102
|
}
|
|
87
103
|
|
|
88
104
|
function createDevSheetForMode(mode: DevPriorityMode, options: SheetOptions) {
|
|
@@ -123,7 +139,11 @@ function removeDevSheetTags(documentOverride?: Document | null, tagName?: string
|
|
|
123
139
|
}
|
|
124
140
|
|
|
125
141
|
function matchesDevSheetTag(sheetName: string, tagName?: string) {
|
|
126
|
-
if (tagName)
|
|
142
|
+
if (tagName) {
|
|
143
|
+
if (sheetName === tagName) return true;
|
|
144
|
+
if (!sheetName.startsWith(tagName + ' ')) return false;
|
|
145
|
+
sheetName = sheetName.slice(tagName.length + 1);
|
|
146
|
+
}
|
|
127
147
|
|
|
128
148
|
return sheetName === 'layers' || sheetName === 'rules' || sheetName.startsWith('rules ');
|
|
129
149
|
}
|
package/sheet/ordered.ts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { CSS_CONFIG } from '../config/config/css';
|
|
2
|
+
import { DEV_CONFIG } from '../config/config/dev';
|
|
3
|
+
import { globalData } from '../utils/global';
|
|
4
|
+
import {
|
|
5
|
+
compareSheetGroups,
|
|
6
|
+
compareSheetRules,
|
|
7
|
+
sheetOrderingPreamble,
|
|
8
|
+
sheetRuleGroup,
|
|
9
|
+
sheetRuleLayer,
|
|
10
|
+
} from './ordering';
|
|
11
|
+
import type { SheetOptions, SheetRule, StyleSheet } from './types';
|
|
12
|
+
import { createStyleTag, resolveDocument } from './utils/dom';
|
|
13
|
+
|
|
14
|
+
const refreshers = globalData<Set<() => void>>('sheet.ordered.refreshers', () => new Set());
|
|
15
|
+
export function refreshOrderedSheets() {
|
|
16
|
+
for (const refresh of refreshers) refresh();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type Group = { name: string; tagName?: string; rules: SheetRule[]; sheet: StyleSheet; };
|
|
20
|
+
|
|
21
|
+
/** Keep whole global sheets together; move DOM groups without rebuilding unrelated rules. */
|
|
22
|
+
export function createOrderedSheet(
|
|
23
|
+
create: (options: SheetOptions) => StyleSheet,
|
|
24
|
+
options: SheetOptions,
|
|
25
|
+
dev: boolean,
|
|
26
|
+
): StyleSheet {
|
|
27
|
+
const document = resolveDocument(options.document);
|
|
28
|
+
if (!document) return create(options);
|
|
29
|
+
const groups = new Map<string, Group>();
|
|
30
|
+
const owners = new Map<string, SheetRule[]>();
|
|
31
|
+
const direct: SheetRule[] = [];
|
|
32
|
+
const seen = new Set<string>();
|
|
33
|
+
let activeLayers: readonly string[] | undefined = CSS_CONFIG.layers;
|
|
34
|
+
let enabled = false;
|
|
35
|
+
let header: HTMLStyleElement | null = null;
|
|
36
|
+
let serial = 0;
|
|
37
|
+
let dirty = false;
|
|
38
|
+
let mode = DEV_CONFIG.stylePriorityMode;
|
|
39
|
+
const pending = new Set<Group>();
|
|
40
|
+
const regular = getGroup('regular');
|
|
41
|
+
const refresh = () => {
|
|
42
|
+
dirty = true;
|
|
43
|
+
flush();
|
|
44
|
+
};
|
|
45
|
+
if (dev) refreshers.add(refresh);
|
|
46
|
+
|
|
47
|
+
function getGroup(name: string): Group {
|
|
48
|
+
const previous = groups.get(name);
|
|
49
|
+
if (previous) return previous;
|
|
50
|
+
const tagName = name === 'regular'
|
|
51
|
+
? options.tagName
|
|
52
|
+
: (options.tagName ? options.tagName + ' ' : '') + 'ordered-' + ++serial;
|
|
53
|
+
const group = { name, tagName, rules: [], sheet: create({ ...options, tagName }) };
|
|
54
|
+
group.sheet.updateLayers(activeLayers ?? []);
|
|
55
|
+
groups.set(name, group);
|
|
56
|
+
return group;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function route(rule: SheetRule): SheetRule {
|
|
60
|
+
return { ...rule, layer: sheetRuleLayer(rule) };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function insert(rule: SheetRule | string) {
|
|
64
|
+
const item = typeof rule === 'string' ? { css: rule } : rule;
|
|
65
|
+
const groupName = sheetRuleGroup(item);
|
|
66
|
+
const key = groupName + '\0' + (item.key ?? item.css);
|
|
67
|
+
if (seen.has(key)) return;
|
|
68
|
+
seen.add(key);
|
|
69
|
+
direct.push(item);
|
|
70
|
+
add(item);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function add(item: SheetRule) {
|
|
74
|
+
enabled ||= !!(item.sheet || item.globalSheet);
|
|
75
|
+
const group = getGroup(sheetRuleGroup(item));
|
|
76
|
+
const previous = group.rules[group.rules.length - 1];
|
|
77
|
+
group.rules.push(item);
|
|
78
|
+
if (enabled && previous && compareSheetRules(previous, item) > 0) pending.add(group);
|
|
79
|
+
if (!pending.has(group)) group.sheet.insert(route(item));
|
|
80
|
+
dirty = true;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function rebuild(group: Group) {
|
|
84
|
+
pending.delete(group);
|
|
85
|
+
group.sheet.destroy?.();
|
|
86
|
+
group.sheet = create({ ...options, tagName: group.tagName });
|
|
87
|
+
group.sheet.updateLayers(activeLayers ?? []);
|
|
88
|
+
group.rules.sort((a, b) => compareSheetRules(a, b));
|
|
89
|
+
for (const rule of group.rules) group.sheet.insert(route(rule));
|
|
90
|
+
group.sheet.flush();
|
|
91
|
+
dirty = true;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function collectTags() {
|
|
95
|
+
const lookup = new Map([...groups.values()].map((group) => [group.tagName ?? '', group]));
|
|
96
|
+
const result = new Map<Group, HTMLStyleElement[]>();
|
|
97
|
+
const head = document!.head || document!.documentElement;
|
|
98
|
+
for (const node of Array.from(head.childNodes)) {
|
|
99
|
+
if (typeof (node as Element).getAttribute !== 'function') continue;
|
|
100
|
+
const name = (node as Element).getAttribute('data-css-sheet');
|
|
101
|
+
const match = name?.match(/^(?:(.*) )?(?:layers|rules(?: .*)?)$/);
|
|
102
|
+
const group = match && lookup.get(match[1] ?? '');
|
|
103
|
+
if (!group) continue;
|
|
104
|
+
const list = result.get(group) ?? [];
|
|
105
|
+
list.push(node as HTMLStyleElement);
|
|
106
|
+
result.set(group, list);
|
|
107
|
+
}
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function flush() {
|
|
112
|
+
if (dev && mode !== DEV_CONFIG.stylePriorityMode) {
|
|
113
|
+
mode = DEV_CONFIG.stylePriorityMode;
|
|
114
|
+
dirty = true;
|
|
115
|
+
}
|
|
116
|
+
for (const group of pending) rebuild(group);
|
|
117
|
+
pending.clear();
|
|
118
|
+
for (const group of groups.values()) group.sheet.flush();
|
|
119
|
+
if (!enabled || !dirty) return;
|
|
120
|
+
const config = { ...CSS_CONFIG, layers: activeLayers ? [...activeLayers] : undefined };
|
|
121
|
+
const all = [...groups.values()].flatMap((group) => group.rules);
|
|
122
|
+
const order = sheetOrderingPreamble(all, config, dev ? DEV_CONFIG.stylePriorityMode : 'layer');
|
|
123
|
+
const head = document!.head || document!.documentElement;
|
|
124
|
+
const groupTags = collectTags();
|
|
125
|
+
if (!header) {
|
|
126
|
+
header = createStyleTag(document!, (options.tagName ? options.tagName + ' ' : '') + 'ordering', options.nonce);
|
|
127
|
+
const first = groupTags.get(regular)?.[0];
|
|
128
|
+
if (first) head.insertBefore(header, first);
|
|
129
|
+
else head.appendChild(header);
|
|
130
|
+
}
|
|
131
|
+
if (header.textContent !== order) header.textContent = order;
|
|
132
|
+
const sorted = [...groups.values()].sort((a, b) => {
|
|
133
|
+
const ar = a.rules[0] ?? { css: '' };
|
|
134
|
+
const br = b.rules[0] ?? { css: '' };
|
|
135
|
+
return compareSheetGroups(ar, br) || (!ar.globalSheet && !br.globalSheet
|
|
136
|
+
? Number(a.name === 'local-unlayered') - Number(b.name === 'local-unlayered')
|
|
137
|
+
: 0);
|
|
138
|
+
});
|
|
139
|
+
let previous: Node = header;
|
|
140
|
+
for (const group of sorted) {
|
|
141
|
+
for (const tag of groupTags.get(group) ?? []) {
|
|
142
|
+
if (previous.nextSibling !== tag) {
|
|
143
|
+
const oldSheet = tag.sheet;
|
|
144
|
+
const css = oldSheet ? Array.from(oldSheet.cssRules).map((rule) => rule.cssText) : [];
|
|
145
|
+
head.insertBefore(tag, previous.nextSibling);
|
|
146
|
+
// Moving a style element can recreate its CSSOM and discard insertRule writes.
|
|
147
|
+
if (oldSheet && tag.sheet !== oldSheet && css.length && !tag.sheet?.cssRules.length) {
|
|
148
|
+
for (const rule of css) tag.sheet?.insertRule(rule, tag.sheet.cssRules.length);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
previous = tag;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
dirty = false;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
insert,
|
|
159
|
+
flush,
|
|
160
|
+
updateLayers(layers) {
|
|
161
|
+
if (activeLayers?.length === layers.length && layers.every((name, index) => activeLayers![index] === name)) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
activeLayers = [...layers];
|
|
165
|
+
for (const group of groups.values()) group.sheet.updateLayers(layers);
|
|
166
|
+
dirty = true;
|
|
167
|
+
},
|
|
168
|
+
replaceRules(owner, rules) {
|
|
169
|
+
const previous = owners.get(owner) ?? [];
|
|
170
|
+
owners.set(owner, rules);
|
|
171
|
+
enabled = true;
|
|
172
|
+
const affected = new Set([...previous, ...rules].map((rule) => sheetRuleGroup(rule)));
|
|
173
|
+
const all = [...direct, ...[...owners.values()].flat()];
|
|
174
|
+
for (const name of affected) {
|
|
175
|
+
const group = getGroup(name);
|
|
176
|
+
group.rules = all.filter((rule) => sheetRuleGroup(rule) === name);
|
|
177
|
+
rebuild(group);
|
|
178
|
+
}
|
|
179
|
+
flush();
|
|
180
|
+
},
|
|
181
|
+
destroy() {
|
|
182
|
+
refreshers.delete(refresh);
|
|
183
|
+
for (const group of groups.values()) group.sheet.destroy?.();
|
|
184
|
+
if (header?.parentNode) header.parentNode.removeChild(header);
|
|
185
|
+
groups.clear();
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
}
|