@master/css-runtime 2.0.0-rc.69 → 2.0.0-rc.70

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/dist/layer.d.ts CHANGED
@@ -1,14 +1,15 @@
1
- import { Layer, Rule, VariableRule } from '@master/css';
1
+ import { Layer, Rule, VariableRule } from '@master/css-engine';
2
2
  import CSSRuntime from './core';
3
3
  export default class RuntimeLayer extends Layer {
4
4
  name: string;
5
5
  cssRuntime: CSSRuntime;
6
- readonly rules: Rule[];
6
+ readonly rules: (Rule | VariableRule)[];
7
7
  readonly tokenCounts: Map<string, number>;
8
8
  native: CSSLayerBlockRule | null;
9
9
  constructor(name: string, cssRuntime: CSSRuntime);
10
10
  attach(): void;
11
11
  insert(rule: Rule | VariableRule, index?: number): number | undefined;
12
+ getNativeInsertIndex(index: number): number;
12
13
  detach(): void;
13
14
  delete(key: string): Rule | VariableRule | undefined;
14
15
  reset(): void;
@@ -0,0 +1,2 @@
1
+ import type { NativeCSSDeclarationMatcher } from '@master/css-engine';
2
+ export declare const browserNativeDeclarationMatcher: NativeCSSDeclarationMatcher;
@@ -1,9 +1,6 @@
1
- import type { Config } from '@master/css';
2
1
  import type Core from './core';
3
2
  declare global {
4
- var CSSRuntime: typeof Core;
5
- var cssRuntime: Core;
6
- var masterCSSConfig: Config;
7
- var masterCSSConfigs: Config[];
3
+ var MasterCSSRuntime: typeof Core;
4
+ var masterCSSRuntime: Core;
8
5
  }
9
6
  export default function registerGlobal(CSSRuntime: typeof Core): void;
@@ -0,0 +1,27 @@
1
+ import { ThemeLayer, VariableRule } from '@master/css-engine';
2
+ import CSSRuntime from './core';
3
+ interface NativeBucket {
4
+ key: string;
5
+ mediaText: string;
6
+ selectorText: string;
7
+ nativeRule: CSSRule;
8
+ styleRule: CSSStyleRule;
9
+ }
10
+ export default class RuntimeThemeLayer extends ThemeLayer {
11
+ name: string;
12
+ cssRuntime: CSSRuntime;
13
+ native: CSSLayerBlockRule | null;
14
+ private nativeBuckets;
15
+ constructor(name: string, cssRuntime: CSSRuntime);
16
+ attach(): void;
17
+ getBucketKey(mediaText: string, selectorText: string): string;
18
+ getStyleRule(nativeRule: CSSRule): CSSStyleRule | undefined;
19
+ syncNativeBuckets(): void;
20
+ ensureNativeBucket(mediaText: string, selectorText: string): NativeBucket | undefined;
21
+ getNativeBucketInsertIndex(mediaText: string, selectorText: string): number;
22
+ insert(rule: VariableRule, index?: number): number | undefined;
23
+ delete(key: string): VariableRule | undefined;
24
+ detach(): void;
25
+ reset(): void;
26
+ }
27
+ export {};
@@ -1,4 +1,5 @@
1
- import type { SyntaxRule } from '@master/css';
1
+ import type { GeneratedRule } from '@master/css-engine';
2
+ import type HydratedGeneratedRule from '../generated-rule';
2
3
  export interface HydrateResult {
3
- allSyntaxRules: SyntaxRule[];
4
+ allUtilities: (GeneratedRule | HydratedGeneratedRule)[];
4
5
  }
@@ -0,0 +1,13 @@
1
+ import { Rule, type GeneratedRule } from '@master/css-engine';
2
+ import RuntimeLayer from './layer';
3
+ import type HydratedGeneratedRule from './generated-rule';
4
+ type RuntimeGeneratedRule = GeneratedRule | HydratedGeneratedRule;
5
+ export default class RuntimeUtilityLayer extends RuntimeLayer {
6
+ rules: (RuntimeGeneratedRule | Rule)[];
7
+ insert(utility: RuntimeGeneratedRule | Rule): number | undefined;
8
+ delete(key: string): Rule | RuntimeGeneratedRule | undefined;
9
+ insertVariables(utility: RuntimeGeneratedRule | Rule): void;
10
+ insertAnimations(utility: RuntimeGeneratedRule | Rule): void;
11
+ }
12
+ export declare type RuntimeUtilityLayerInstance = RuntimeUtilityLayer;
13
+ export {};
@@ -0,0 +1 @@
1
+ export default function findNativeCSSRuleIndex(cssRules: CSSRuleList, nativeRule: CSSRule): number;
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@master/css-runtime","type":"module","scripts":{"build":"techor build","dev":"pnpm build --watch","e2e":"playwright test","e2e:watch":"nodemon -e js,mjs,ts,html,css --watch dist --watch e2e --exec playwright test","type-check":"tsc --noEmit","lint":"eslint"},"license":"MIT","description":"Run Master CSS right in the browser","author":"Aoyue Design LLC.","funding":"https://rc.css.master.co/sponsor","homepage":"https://css.master.co","bugs":{"url":"https://github.com/master-co/css/issues"},"repository":{"type":"git","url":"https://github.com/master-co/css.git","directory":"packages/runtime"},"keywords":["utility","css","lightweight","mastercss","master","runtime","cdn"],"sideEffects":false,"main":"./dist/index.cjs","jsnext:main":"./dist/index.mjs","esnext":"./dist/index.mjs","module":"./dist/index.mjs","browser":"./dist/global.min.js","types":"./dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","require":{"development":"./dist/index.development.cjs","default":"./dist/index.cjs"},"import":{"development":"./dist/index.development.mjs","default":"./dist/index.mjs"}}},"files":["dist"],"publishConfig":{"access":"public","provenance":true},"dependencies":{"@master/css":"^2.0.0-rc.69","@master/css-devtools-hook":"^2.0.0-rc.69"},"devDependencies":{"@playwright/test":"^1.49.1","shared":"^2.0.0-rc.69"},"version":"2.0.0-rc.69"}
1
+ {"name":"@master/css-runtime","type":"module","scripts":{"build":"techor build && node scripts/copy-default-manifest-json.mjs","bench":"node scripts/benchmark-runtime.mjs","dev":"pnpm build --watch","e2e":"playwright test","e2e:watch":"nodemon -e js,mjs,ts,html,css --watch dist --watch e2e --watch tests --exec playwright test","type-check":"tsc --noEmit","lint":"eslint"},"license":"MIT","description":"Run Master CSS right in the browser","author":"Aoyue Design LLC.","funding":"https://rc.css.master.co/sponsor","homepage":"https://css.master.co","bugs":{"url":"https://github.com/master-co/css/issues"},"repository":{"type":"git","url":"https://github.com/master-co/css.git","directory":"packages/runtime"},"keywords":["utility","css","lightweight","mastercss","master","runtime","cdn"],"sideEffects":false,"main":"./dist/index.mjs","jsnext:main":"./dist/index.mjs","esnext":"./dist/index.mjs","module":"./dist/index.mjs","browser":"./dist/global.min.js","types":"./dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","import":{"development":"./dist/index.development.mjs","default":"./dist/index.mjs"},"default":"./dist/index.mjs"}},"files":["dist"],"publishConfig":{"access":"public","provenance":true},"dependencies":{"@master/css-engine":"^2.0.0-rc.70","@master/css-preset":"^2.0.0-rc.70","@master/css-devtools-hook":"^2.0.0-rc.70","@master/css-schema":"^2.0.0-rc.70"},"devDependencies":{"@master/css":"^2.0.0-rc.70","@master/css-compiler":"^2.0.0-rc.70","@master/css-integration":"^2.0.0-rc.70","@playwright/test":"1.61.0"},"version":"2.0.0-rc.70"}
package/dist/index.cjs DELETED
@@ -1,481 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var css = require('@master/css');
6
- var cssDevtoolsHook = require('@master/css-devtools-hook');
7
-
8
- function registerGlobal(CSSRuntime) {
9
- if (!globalThis.CSSRuntime) globalThis.CSSRuntime = CSSRuntime;
10
- cssDevtoolsHook.installHook();
11
- }
12
-
13
- function findNativeCSSRuleIndex(cssRules, nativeRule) {
14
- for(let i = 0; i < cssRules.length; i++){
15
- if (cssRules[i] === nativeRule) {
16
- return i;
17
- }
18
- }
19
- return -1;
20
- }
21
-
22
- class RuntimeLayer extends css.Layer {
23
- name;
24
- cssRuntime;
25
- rules = [];
26
- tokenCounts = new Map();
27
- native = null;
28
- constructor(name, cssRuntime){
29
- super(name, cssRuntime), this.name = name, this.cssRuntime = cssRuntime;
30
- }
31
- attach() {
32
- super.attach();
33
- const nativeSheet = this.cssRuntime.style?.sheet;
34
- if (nativeSheet && !this.native?.parentStyleSheet) {
35
- const insertedIndex = nativeSheet.insertRule(this.text, nativeSheet.cssRules.length);
36
- this.native = nativeSheet.cssRules.item(insertedIndex);
37
- }
38
- }
39
- insert(rule, index = this.rules.length) {
40
- const insertedIndex = super.insert(rule, index);
41
- if (insertedIndex === undefined || !this.native) return;
42
- const insertRuleSafely = (text, position)=>{
43
- // Checks if the rule is inserted in a native CSS rule with this.attach()
44
- if (this.rules.length === 1) {
45
- return this.native.cssRules.item(position);
46
- } else {
47
- try {
48
- const insertedIndex = this.native.insertRule(text, position);
49
- return this.native.cssRules.item(insertedIndex);
50
- } catch (error) {
51
- console.error(error, rule);
52
- /**
53
- * If the rule is invalid, remove it from the rules array.
54
- * It's important to note that the rule may break the entire CSS runtime,
55
- */ this.rules.splice(insertedIndex, 1);
56
- return;
57
- }
58
- }
59
- };
60
- if ('nodes' in rule) {
61
- let currentIndex = insertedIndex;
62
- rule.nodes.forEach((node)=>{
63
- node.native = insertRuleSafely(node.text, currentIndex);
64
- if (node.native) currentIndex++;
65
- });
66
- } else {
67
- rule.native = insertRuleSafely(rule.text, insertedIndex);
68
- }
69
- return insertedIndex;
70
- }
71
- detach() {
72
- super.detach();
73
- const nativeSheet = this.cssRuntime.style?.sheet;
74
- if (nativeSheet && this.native?.parentStyleSheet) {
75
- const foundIndex = findNativeCSSRuleIndex(nativeSheet.cssRules, this.native);
76
- if (foundIndex !== -1) {
77
- nativeSheet.deleteRule(foundIndex);
78
- }
79
- }
80
- }
81
- delete(key) {
82
- const deletedRule = super.delete(key);
83
- if (!deletedRule || !this.native) return;
84
- const deleteRuleSafely = (rule)=>{
85
- if (!rule) return;
86
- const foundIndex = findNativeCSSRuleIndex(this.native.cssRules, rule);
87
- if (foundIndex !== -1) {
88
- try {
89
- this.native.deleteRule(foundIndex);
90
- } catch (error) {
91
- console.error(error, rule);
92
- }
93
- }
94
- };
95
- if ('nodes' in deletedRule) {
96
- deletedRule.nodes.forEach((node)=>deleteRuleSafely(node.native));
97
- } else {
98
- deleteRuleSafely(deletedRule.native);
99
- }
100
- return deletedRule;
101
- }
102
- reset() {
103
- super.reset();
104
- const nativeSheet = this.cssRuntime.style?.sheet;
105
- if (this.native && nativeSheet) {
106
- const foundIndex = findNativeCSSRuleIndex(nativeSheet.cssRules, this.native);
107
- if (foundIndex !== -1) {
108
- nativeSheet.deleteRule(foundIndex);
109
- }
110
- this.native = null;
111
- }
112
- }
113
- }
114
-
115
- const RuntimeSyntaxLayer = css.withSyntaxLayer(RuntimeLayer);
116
-
117
- class CSSRuntime extends css.MasterCSS {
118
- root;
119
- customConfig;
120
- baseConfig;
121
- static instances = new WeakMap();
122
- host;
123
- container;
124
- baseLayer = new RuntimeSyntaxLayer('base', this);
125
- themeLayer = new RuntimeLayer('theme', this);
126
- presetLayer = new RuntimeSyntaxLayer('preset', this);
127
- componentsLayer = new RuntimeSyntaxLayer('components', this);
128
- generalLayer = new RuntimeSyntaxLayer('general', this);
129
- classCounts = new Map();
130
- observer;
131
- progressive = false;
132
- observing = false;
133
- constructor(root = document, customConfig = css.config, baseConfig){
134
- super(customConfig, baseConfig), this.root = root, this.customConfig = customConfig, this.baseConfig = baseConfig;
135
- // Do not use instanceof here, because it will not work
136
- const rootConstructorName = root?.constructor.name;
137
- if (rootConstructorName === 'HTMLDocument' || rootConstructorName === 'Document') {
138
- this.root.defaultView.globalThis.cssRuntime = this;
139
- this.container = this.root.head;
140
- this.host = this.root.documentElement;
141
- } else {
142
- this.container = this.root;
143
- this.host = this.root.host;
144
- }
145
- globalThis.CSSRuntime.instances.set(this.root, this);
146
- __MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:created', {
147
- cssRuntime: this
148
- });
149
- }
150
- /**
151
- * Observe the DOM for changes and update the running stylesheet. (browser only)
152
- * @param options mutation observer options
153
- * @returns this
154
- */ observe() {
155
- if (this.observing) return this;
156
- // Detect prerendered stylesheet
157
- if (this.root.styleSheets) {
158
- for (const sheet of this.root.styleSheets){
159
- const { ownerNode } = sheet;
160
- if (ownerNode instanceof HTMLStyleElement && ownerNode.id === 'master') {
161
- this.style = ownerNode;
162
- if (this.style.sheet?.cssRules.length) {
163
- this.progressive = true;
164
- }
165
- break;
166
- }
167
- }
168
- }
169
- // Prepare snapshot map
170
- const elementClasses = new WeakMap();
171
- // Initial scan and populate counts + snapshot
172
- const connectedNames = new Set();
173
- const increaseClassCount = (className)=>{
174
- const count = this.classCounts.get(className) || 0;
175
- if (!count) connectedNames.add(className);
176
- this.classCounts.set(className, count + 1);
177
- };
178
- const elementsWithClass = this.root.querySelectorAll('[class]');
179
- elementsWithClass.forEach((el)=>{
180
- const clsList = el.classList;
181
- if (clsList) {
182
- el.classList.forEach(increaseClassCount);
183
- }
184
- elementClasses.set(el, new Set(clsList));
185
- });
186
- // Hydration or style creation
187
- if (this.progressive) {
188
- const hydrateResult = this.hydrate(this.style.sheet.cssRules);
189
- for (const cls of connectedNames){
190
- if (!hydrateResult.allSyntaxRules.find((r)=>(r.fixedClass || r.name) === cls)) {
191
- this.add(cls);
192
- }
193
- }
194
- } else {
195
- this.style = document.createElement('style');
196
- this.style.id = 'master';
197
- this.style.setAttribute('blocking', 'render');
198
- this.container.append(this.style);
199
- this.style.sheet.insertRule(this.layerStatementRule.text);
200
- this.layerStatementRule.native = this.style.sheet.cssRules.item(0);
201
- connectedNames.forEach((cls)=>this.add(cls));
202
- }
203
- this.observer = new MutationObserver((records)=>{
204
- const deltaCounts = new Map();
205
- const nodeMap = new Map();
206
- const attrRecords = new Set();
207
- const visited = new WeakSet();
208
- const updateDelta = (cls, delta)=>deltaCounts.set(cls, (deltaCounts.get(cls) || 0) + delta);
209
- const diffAndSnapshot = (el)=>{
210
- const prev = new Set(elementClasses.get(el) || []);
211
- const next = new Set(el.classList);
212
- for (const c of next)if (!prev.has(c)) updateDelta(c, 1);
213
- for (const c of prev)if (!next.has(c)) updateDelta(c, -1);
214
- elementClasses.set(el, next);
215
- };
216
- const removeSnapshot = (el)=>{
217
- for (const c of elementClasses.get(el) || [])updateDelta(c, -1);
218
- elementClasses.delete(el);
219
- };
220
- for (const record of records){
221
- if (record.type === 'childList') {
222
- for (const node of record.addedNodes)if (node instanceof Element && node.isConnected) nodeMap.set(node, (nodeMap.get(node) || 0) + 1);
223
- for (const node of record.removedNodes)if (node instanceof Element && !node.isConnected) nodeMap.set(node, (nodeMap.get(node) || 0) - 1);
224
- } else if (record.type === 'attributes' && record.attributeName === 'class') {
225
- attrRecords.add(record.target);
226
- }
227
- }
228
- const traverseIncludingSelf = (el, fn, visited)=>{
229
- if (!visited.has(el)) {
230
- visited.add(el);
231
- fn(el);
232
- for (const child of el.children){
233
- traverseIncludingSelf(child, fn, visited);
234
- }
235
- } else {
236
- nodeMap.delete(el);
237
- }
238
- };
239
- for (const [node, count] of nodeMap){
240
- if (count > 0) {
241
- traverseIncludingSelf(node, diffAndSnapshot, visited);
242
- } else if (count < 0 && !node.isConnected) {
243
- traverseIncludingSelf(node, removeSnapshot, visited);
244
- }
245
- }
246
- for (const el of attrRecords){
247
- if (!visited.has(el)) diffAndSnapshot(el);
248
- }
249
- for (const [cls, change] of deltaCounts){
250
- const current = this.classCounts.get(cls) || 0;
251
- const next = current + change;
252
- if (next > 0) {
253
- this.classCounts.set(cls, next);
254
- if (current === 0) this.add(cls);
255
- } else {
256
- this.classCounts.delete(cls);
257
- this.remove(cls);
258
- }
259
- }
260
- globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:mutated', {
261
- records,
262
- classCounts: deltaCounts,
263
- cssRuntime: this
264
- });
265
- });
266
- this.observer.observe(this.root, {
267
- childList: true,
268
- attributes: true,
269
- attributeFilter: [
270
- 'class'
271
- ],
272
- attributeOldValue: true,
273
- subtree: true
274
- });
275
- if (!this.progressive) this.host.removeAttribute('hidden');
276
- this.observing = true;
277
- globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:observed', {
278
- cssRuntime: this
279
- });
280
- return this;
281
- }
282
- hydrate(nativeLayerRules) {
283
- const cssLayerRules = [];
284
- const checkSheet = new CSSStyleSheet();
285
- const result = {
286
- allSyntaxRules: []
287
- };
288
- for(let i = 0; i < nativeLayerRules.length; i++){
289
- const eachNativeCSSRule = nativeLayerRules[i];
290
- if (eachNativeCSSRule.constructor.name === 'CSSLayerBlockRule') {
291
- const eachCSSLayerRule = eachNativeCSSRule;
292
- if (eachNativeCSSRule.name === 'theme') {
293
- this.themeLayer.native = eachCSSLayerRule;
294
- let variableRule;
295
- const unresolvedCSSRules = new Map();
296
- for (const rule of eachCSSLayerRule.cssRules){
297
- // trim() for fix the firefox bug that the cssText ends with \n\n
298
- unresolvedCSSRules.set(rule.cssText.trim(), rule);
299
- }
300
- for (const cssRule of eachCSSLayerRule.cssRules){
301
- if (!unresolvedCSSRules.has(cssRule.cssText)) continue;
302
- const variableCSSRule = cssRule.constructor.name === 'CSSMediaRule' ? cssRule.cssRules[0] : cssRule;
303
- const variableName = variableCSSRule.style[0].slice(2);
304
- const variable = this.variables.get(variableName);
305
- if (!variable) continue;
306
- variableRule = new css.VariableRule(variableName, variable, this);
307
- this.themeLayer.rules.push(variableRule);
308
- this.themeLayer.tokenCounts.set(variableRule.name, 0);
309
- variableRule.nodes.forEach((node)=>{
310
- const checkRuleIndex = checkSheet.insertRule(node.text);
311
- const checkNodeNativeRule = checkSheet.cssRules.item(checkRuleIndex);
312
- if (checkNodeNativeRule) {
313
- const checkNodeNativeRuleText = checkNodeNativeRule.cssText.trim();
314
- const match = unresolvedCSSRules.get(checkNodeNativeRuleText);
315
- if (match) {
316
- node.native = match;
317
- unresolvedCSSRules.delete(checkNodeNativeRuleText);
318
- return;
319
- }
320
- }
321
- });
322
- }
323
- if (this.themeLayer.rules.length) this.rules.push(this.themeLayer);
324
- } else {
325
- cssLayerRules.push(eachCSSLayerRule);
326
- }
327
- } else if (eachNativeCSSRule.constructor.name === 'CSSLayerStatementRule') {
328
- this.layerStatementRule.native = eachNativeCSSRule;
329
- } else if (eachNativeCSSRule.constructor.name === 'CSSKeyframesRule') {
330
- const nativeKeyframsRule = eachNativeCSSRule;
331
- const keyframes = this.animations.get(nativeKeyframsRule.name);
332
- if (!keyframes) continue;
333
- const animationRule = new css.AnimationRule(nativeKeyframsRule.name, keyframes, this);
334
- animationRule.native = nativeKeyframsRule;
335
- this.animationsNonLayer.rules.push(animationRule);
336
- this.rules.push(animationRule);
337
- this.animationsNonLayer.tokenCounts.set(animationRule.name, 0);
338
- }
339
- }
340
- for (const eachCSSLayerRule of cssLayerRules){
341
- let layer;
342
- switch(eachCSSLayerRule.name){
343
- case 'base':
344
- layer = this.baseLayer;
345
- break;
346
- case 'preset':
347
- layer = this.presetLayer;
348
- break;
349
- case 'components':
350
- layer = this.componentsLayer;
351
- break;
352
- case 'general':
353
- layer = this.generalLayer;
354
- break;
355
- default:
356
- console.error(`Cannot recognize the layer \`${eachCSSLayerRule.name}\`. (https://rc.css.master.co/messages/hydration-errors)`);
357
- continue;
358
- }
359
- layer.native = eachCSSLayerRule;
360
- const unresolvedCSSRules = new Map();
361
- for (const rule of eachCSSLayerRule.cssRules){
362
- // trim() for fix the firefox bug that the cssText ends with \n\n
363
- unresolvedCSSRules.set(rule.cssText.trim(), rule);
364
- }
365
- for (const eachNativeLayerRule of eachCSSLayerRule.cssRules){
366
- if (!unresolvedCSSRules.has(eachNativeLayerRule.cssText)) continue;
367
- const selectorText = this.getSelectorText(eachNativeLayerRule);
368
- if (!selectorText) {
369
- console.error(`Cannot get the selector text from \`${eachNativeLayerRule.cssText}\`. (${layer.name}) (https://rc.css.master.co/messages/hydration-errors)`);
370
- continue;
371
- }
372
- const createdRules = this.createFromSelectorText(selectorText);
373
- if (createdRules) {
374
- for (const createdRule of createdRules){
375
- layer.rules.push(createdRule);
376
- layer.insertVariables(createdRule);
377
- layer.insertAnimations(createdRule);
378
- result.allSyntaxRules.push(createdRule);
379
- try {
380
- const checkRuleIndex = checkSheet.insertRule(createdRule.text);
381
- const checkNodeNativeRule = checkSheet.cssRules.item(checkRuleIndex);
382
- if (checkNodeNativeRule) {
383
- const checkNodeNativeRuleText = checkNodeNativeRule.cssText.trim();
384
- const match = unresolvedCSSRules.get(checkNodeNativeRuleText);
385
- if (match) {
386
- createdRule.native = match;
387
- unresolvedCSSRules.delete(checkNodeNativeRuleText);
388
- continue;
389
- }
390
- }
391
- console.error(`Cannot retrieve CSS rule for \`${createdRule.text}\`. (${layer.name}) (https://rc.css.master.co/messages/hydration-errors)`);
392
- } catch (error) {
393
- }
394
- }
395
- } else {
396
- console.error(`Cannot recognize \`${eachNativeLayerRule.cssText}\`. (${layer.name}) (https://rc.css.master.co/messages/hydration-errors)`);
397
- }
398
- }
399
- if (layer.rules.length) this.rules.push(layer);
400
- }
401
- globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:hydrated', {
402
- cssRuntime: this,
403
- result
404
- });
405
- return result;
406
- }
407
- getSelectorText(cssRule) {
408
- if (cssRule instanceof CSSStyleRule) {
409
- return cssRule.selectorText;
410
- } else if (cssRule instanceof CSSGroupingRule) {
411
- return this.getSelectorText(cssRule.cssRules.item(0));
412
- }
413
- }
414
- disconnect() {
415
- if (!this.observing) return;
416
- if (this.observer) {
417
- this.observer.disconnect();
418
- this.observer = undefined;
419
- }
420
- // @ts-ignore
421
- this.observing = false;
422
- this.reset();
423
- this.classCounts.clear();
424
- if (!this.progressive) {
425
- this.style?.remove();
426
- this.style = null;
427
- }
428
- globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:disconnected', {
429
- cssRuntime: this
430
- });
431
- return this;
432
- }
433
- refresh(customConfig = this.customConfig) {
434
- if (!this.observing || !this.style.sheet) return this;
435
- for(let i = 1; i <= this.style.sheet.cssRules.length - 1; i++){
436
- this.style.sheet.deleteRule(i);
437
- }
438
- super.refresh(customConfig);
439
- /**
440
- * 拿當前所有的 classNames 按照最新的 colors, config.rules 匹配並生成新的 style
441
- * 所以 refresh 過後 rules 可能會變多也可能會變少
442
- */ this.classCounts.forEach((_, className)=>{
443
- this.add(className);
444
- });
445
- globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:refreshed', {
446
- cssRuntime: this,
447
- customConfig
448
- });
449
- return this;
450
- }
451
- destroy() {
452
- this.disconnect();
453
- globalThis.CSSRuntime.instances.delete(this.root);
454
- globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:destroyed', {
455
- cssRuntime: this
456
- });
457
- return this;
458
- }
459
- }
460
- (function(CSSRuntime) {
461
- registerGlobal(CSSRuntime);
462
- })(CSSRuntime);
463
-
464
- /**
465
- * Initialize a new CSSRuntime instance and observe the target root
466
- * @param config master css config
467
- * @param root target root to observe
468
- * @param autoObserve auto observe the target root
469
- * @returns master css instance
470
- */ function initCSSRuntime(config, root = document, autoObserve = true) {
471
- let cssRuntime = globalThis.CSSRuntime.instances.get(root);
472
- if (cssRuntime) return cssRuntime;
473
- cssRuntime = new CSSRuntime(root, config, css.config);
474
- if (autoObserve) cssRuntime.observe();
475
- return cssRuntime;
476
- }
477
-
478
- exports.CSSRuntime = CSSRuntime;
479
- exports.RuntimeSyntaxLayer = RuntimeSyntaxLayer;
480
- exports.default = CSSRuntime;
481
- exports.initCSSRuntime = initCSSRuntime;