@master/css-runtime 2.0.0-rc.6 → 2.0.0-rc.60
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/README.md +4 -4
- package/dist/core.d.ts +71 -10
- package/dist/debuggers/class-count.d.ts +1 -0
- package/dist/debuggers/index.d.ts +1 -0
- package/dist/debuggers/lifecycle.d.ts +1 -0
- package/dist/global.min.js +1 -0
- package/dist/index.cjs +505 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.development.cjs +598 -0
- package/dist/index.development.d.ts +1 -0
- package/dist/index.development.mjs +595 -0
- package/dist/index.mjs +499 -0
- package/dist/init.d.ts +10 -0
- package/dist/layer.d.ts +15 -0
- package/dist/register-global.d.ts +9 -0
- package/dist/syntax-layer.d.ts +19 -0
- package/dist/types/hydrate-result.d.ts +4 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +1 -1
- package/dist/iife.bundle.js +0 -1
- package/dist/index.bundle.js +0 -1
- package/dist/index.bundle.mjs +0 -1
- package/dist/init-css-runtime.d.ts +0 -9
- package/dist/types/global.d.ts +0 -8
- /package/dist/{iife.d.ts → global.min.d.ts} +0 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
import { Layer, withSyntaxLayer, MasterCSS, config, Rule } from '@master/css';
|
|
2
|
+
import { installHook } from '@master/css-devtools-hook';
|
|
3
|
+
|
|
4
|
+
function registerGlobal(CSSRuntime) {
|
|
5
|
+
if (!globalThis.CSSRuntime) globalThis.CSSRuntime = CSSRuntime;
|
|
6
|
+
installHook();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function findNativeCSSRuleIndex(cssRules, nativeRule) {
|
|
10
|
+
for(let i = 0; i < cssRules.length; i++){
|
|
11
|
+
if (cssRules[i] === nativeRule) {
|
|
12
|
+
return i;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return -1;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
class RuntimeLayer extends Layer {
|
|
19
|
+
name;
|
|
20
|
+
cssRuntime;
|
|
21
|
+
rules;
|
|
22
|
+
tokenCounts;
|
|
23
|
+
native;
|
|
24
|
+
constructor(name, cssRuntime){
|
|
25
|
+
super(name, cssRuntime), this.name = name, this.cssRuntime = cssRuntime, this.rules = [], this.tokenCounts = new Map(), this.native = null;
|
|
26
|
+
}
|
|
27
|
+
attach() {
|
|
28
|
+
super.attach();
|
|
29
|
+
const nativeSheet = this.cssRuntime.style?.sheet;
|
|
30
|
+
if (nativeSheet && !this.native?.parentStyleSheet) {
|
|
31
|
+
const insertedIndex = nativeSheet.insertRule(this.text, nativeSheet.cssRules.length);
|
|
32
|
+
this.native = nativeSheet.cssRules.item(insertedIndex);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
insert(rule, index = this.rules.length) {
|
|
36
|
+
const insertedIndex = super.insert(rule, index);
|
|
37
|
+
if (insertedIndex === undefined) return;
|
|
38
|
+
if (this.native) {
|
|
39
|
+
let cssRuleIndex = 0;
|
|
40
|
+
const lastCssRule = function getLastCssRule(layer, index) {
|
|
41
|
+
let lastCssRule;
|
|
42
|
+
const previouRule = layer.rules[index];
|
|
43
|
+
if (previouRule && 'nodes' in previouRule) {
|
|
44
|
+
if (!previouRule.nodes.length) return getLastCssRule(layer, index - 1);
|
|
45
|
+
const lastNativeRule = previouRule.nodes[previouRule.nodes.length - 1];
|
|
46
|
+
lastCssRule = lastNativeRule.native;
|
|
47
|
+
}
|
|
48
|
+
return lastCssRule;
|
|
49
|
+
}(this, insertedIndex - 1);
|
|
50
|
+
if (lastCssRule) {
|
|
51
|
+
for(let i = 0; i < this.native.cssRules.length; i++){
|
|
52
|
+
if (this.native.cssRules[i] === lastCssRule) {
|
|
53
|
+
cssRuleIndex = i + 1;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
for(let i = 0; i < rule.nodes.length;){
|
|
59
|
+
try {
|
|
60
|
+
const node = rule.nodes[i];
|
|
61
|
+
const insertedIndex = this.native.insertRule(node.text, cssRuleIndex);
|
|
62
|
+
node.native = this.native.cssRules.item(insertedIndex);
|
|
63
|
+
cssRuleIndex++;
|
|
64
|
+
i++;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
console.error(error, rule.nodes[i]);
|
|
67
|
+
rule.nodes.splice(i, 1);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return insertedIndex;
|
|
72
|
+
}
|
|
73
|
+
detach() {
|
|
74
|
+
const nativeSheet = this.cssRuntime.style?.sheet;
|
|
75
|
+
if (nativeSheet && this.native?.parentStyleSheet) {
|
|
76
|
+
const foundIndex = findNativeCSSRuleIndex(nativeSheet.cssRules, this.native);
|
|
77
|
+
if (foundIndex !== -1) {
|
|
78
|
+
nativeSheet.deleteRule(foundIndex);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
delete(key) {
|
|
83
|
+
const deletedRule = super.delete(key);
|
|
84
|
+
if (!deletedRule) return;
|
|
85
|
+
if (this.native?.cssRules && 'nodes' in deletedRule) {
|
|
86
|
+
for (const node of deletedRule.nodes){
|
|
87
|
+
if (node.native) {
|
|
88
|
+
const foundIndex = findNativeCSSRuleIndex(this.native.cssRules, node.native);
|
|
89
|
+
if (foundIndex !== -1) {
|
|
90
|
+
// todo: Firefox throw "Uncaught NS_ERROR_FAILURE". Reproduce: Add '@fade|1s @fade|2s' and remove '@fade|1s @fade|2s'
|
|
91
|
+
this.native.deleteRule(foundIndex);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return deletedRule;
|
|
97
|
+
}
|
|
98
|
+
reset() {
|
|
99
|
+
super.reset();
|
|
100
|
+
const nativeSheet = this.cssRuntime.style?.sheet;
|
|
101
|
+
if (this.native && nativeSheet) {
|
|
102
|
+
const foundIndex = findNativeCSSRuleIndex(nativeSheet.cssRules, this.native);
|
|
103
|
+
if (foundIndex !== -1) {
|
|
104
|
+
nativeSheet.deleteRule(foundIndex);
|
|
105
|
+
}
|
|
106
|
+
this.native = null;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const RuntimeSyntaxLayer = withSyntaxLayer(RuntimeLayer);
|
|
112
|
+
|
|
113
|
+
class CSSRuntime extends MasterCSS {
|
|
114
|
+
root;
|
|
115
|
+
customConfig;
|
|
116
|
+
static instances = new WeakMap();
|
|
117
|
+
host;
|
|
118
|
+
observing;
|
|
119
|
+
progressive;
|
|
120
|
+
container;
|
|
121
|
+
observer;
|
|
122
|
+
baseLayer;
|
|
123
|
+
themeLayer;
|
|
124
|
+
presetLayer;
|
|
125
|
+
componentsLayer;
|
|
126
|
+
generalLayer;
|
|
127
|
+
classCounts;
|
|
128
|
+
constructor(root = document, customConfig = config){
|
|
129
|
+
super(customConfig), this.root = root, this.customConfig = customConfig, this.observing = false, this.progressive = false, this.baseLayer = new RuntimeSyntaxLayer('base', this), this.themeLayer = new RuntimeLayer('theme', this), this.presetLayer = new RuntimeSyntaxLayer('preset', this), this.componentsLayer = new RuntimeSyntaxLayer('components', this), this.generalLayer = new RuntimeSyntaxLayer('general', this), this.classCounts = new Map();
|
|
130
|
+
const rootConstructorName = root?.constructor.name;
|
|
131
|
+
if (rootConstructorName === 'HTMLDocument' || rootConstructorName === 'Document') {
|
|
132
|
+
this.root.defaultView.globalThis.cssRuntime = this;
|
|
133
|
+
this.container = this.root.head;
|
|
134
|
+
this.host = this.root.documentElement;
|
|
135
|
+
} else {
|
|
136
|
+
this.container = this.root;
|
|
137
|
+
this.host = this.root.host;
|
|
138
|
+
}
|
|
139
|
+
this.supportVendors = new Set();
|
|
140
|
+
const styleDeclaration = document.documentElement.style;
|
|
141
|
+
if ('webkitTransform' in styleDeclaration) this.supportVendors.add('webkit');
|
|
142
|
+
if ('MozTransform' in styleDeclaration) this.supportVendors.add('moz');
|
|
143
|
+
if ('msTransform' in styleDeclaration) this.supportVendors.add('ms');
|
|
144
|
+
if ('OTransform' in styleDeclaration) this.supportVendors.add('o');
|
|
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
|
+
// 1. Check if the stylesheet is progressive.
|
|
157
|
+
if (this.root.styleSheets) for (const sheet of this.root.styleSheets){
|
|
158
|
+
const { ownerNode } = sheet;
|
|
159
|
+
if (ownerNode && ownerNode.id === 'master') {
|
|
160
|
+
this.style = ownerNode;
|
|
161
|
+
// @ts-ignore
|
|
162
|
+
this.progressive = true;
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// 2. Count the classes in the host and its children.
|
|
167
|
+
const connectedNames = new Set();
|
|
168
|
+
const increaseClassCount = (className)=>{
|
|
169
|
+
const count = this.classCounts.get(className) || 0;
|
|
170
|
+
if (!count) {
|
|
171
|
+
connectedNames.add(className);
|
|
172
|
+
}
|
|
173
|
+
this.classCounts.set(className, count + 1);
|
|
174
|
+
};
|
|
175
|
+
this.host.classList.forEach(increaseClassCount);
|
|
176
|
+
(this.root.constructor.name === 'HTMLDocument' ? this.host : this.container).querySelectorAll('[class]').forEach((element)=>element.classList.forEach(increaseClassCount));
|
|
177
|
+
// 3. Hydrate the existing CSS rules or create a new one.
|
|
178
|
+
if (this.progressive) {
|
|
179
|
+
const hydrateResult = this.hydrate(this.style.sheet.cssRules);
|
|
180
|
+
// Add the connected class names that are not hydrated yet.
|
|
181
|
+
for (const eachConnectedName of connectedNames){
|
|
182
|
+
if (hydrateResult.allSyntaxRules.find((rule)=>(rule.fixedClass || rule.name) === eachConnectedName)) continue;
|
|
183
|
+
this.add(eachConnectedName);
|
|
184
|
+
}
|
|
185
|
+
} else {
|
|
186
|
+
this.style = document.createElement('style');
|
|
187
|
+
this.style.id = 'master';
|
|
188
|
+
this.container.append(this.style);
|
|
189
|
+
this.style.sheet.insertRule(this.layerStatementRule.text);
|
|
190
|
+
this.layerStatementRule.nodes[0].native = this.style.sheet.cssRules.item(0);
|
|
191
|
+
for (const eachConnectedName of connectedNames){
|
|
192
|
+
this.add(eachConnectedName);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// @ts-expect-error readonly
|
|
196
|
+
this.observer = new MutationObserver((mutationRecords)=>{
|
|
197
|
+
const eachClassCounts = new Map();
|
|
198
|
+
const targetFirstAttrMutationRecord = new Map();
|
|
199
|
+
const updateClassCount = (classes, isAdding = false)=>{
|
|
200
|
+
const usage = isAdding ? 1 : -1;
|
|
201
|
+
classes.forEach((className)=>{
|
|
202
|
+
eachClassCounts.set(className, (eachClassCounts.get(className) || 0) + usage);
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
const connectedStatusMap = new Map();
|
|
206
|
+
const disconnectedStatusMap = new Map();
|
|
207
|
+
const recordStatus = (target, mutationRecord, map, adding)=>{
|
|
208
|
+
const status = map.get(target);
|
|
209
|
+
if (status) {
|
|
210
|
+
status.change += adding ? 1 : -1;
|
|
211
|
+
status.mutationRecord = mutationRecord;
|
|
212
|
+
} else {
|
|
213
|
+
map.set(target, {
|
|
214
|
+
change: adding ? 1 : -1,
|
|
215
|
+
mutationRecord
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
for (const child of target.children){
|
|
219
|
+
if ('classList' in child) {
|
|
220
|
+
recordStatus(child, mutationRecord, map, adding);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
mutationRecords.forEach((mutationRecord)=>{
|
|
225
|
+
const target = mutationRecord.target;
|
|
226
|
+
switch(mutationRecord.type){
|
|
227
|
+
case 'attributes':
|
|
228
|
+
if (!targetFirstAttrMutationRecord.has(target)) {
|
|
229
|
+
targetFirstAttrMutationRecord.set(target, mutationRecord);
|
|
230
|
+
}
|
|
231
|
+
break;
|
|
232
|
+
case 'childList':
|
|
233
|
+
const targetStatusMap = target.isConnected ? connectedStatusMap : disconnectedStatusMap;
|
|
234
|
+
mutationRecord.addedNodes.forEach((node)=>'classList' in node && recordStatus(node, mutationRecord, targetStatusMap, true));
|
|
235
|
+
mutationRecord.removedNodes.forEach((node)=>'classList' in node && recordStatus(node, mutationRecord, targetStatusMap, false));
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
const updateTarget = (target, adding)=>{
|
|
240
|
+
const firstAttrMutationRecord = targetFirstAttrMutationRecord.get(target);
|
|
241
|
+
if (firstAttrMutationRecord) {
|
|
242
|
+
targetFirstAttrMutationRecord.delete(target);
|
|
243
|
+
}
|
|
244
|
+
if (adding) {
|
|
245
|
+
updateClassCount(target.classList, adding);
|
|
246
|
+
} else {
|
|
247
|
+
if (firstAttrMutationRecord) {
|
|
248
|
+
updateClassCount(firstAttrMutationRecord.oldValue ? firstAttrMutationRecord.oldValue.split(/\s+/) : [], adding);
|
|
249
|
+
} else {
|
|
250
|
+
updateClassCount(target.classList, adding);
|
|
251
|
+
}
|
|
252
|
+
disconnectedStatusMap.forEach((disconnectedTargetStatus, disconnectedTarget)=>{
|
|
253
|
+
if (disconnectedTargetStatus.mutationRecord.target === target && disconnectedTargetStatus.change !== 0) {
|
|
254
|
+
updateTarget(disconnectedTarget, disconnectedTargetStatus.change > 0);
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
connectedStatusMap.forEach(({ change }, target)=>change !== 0 && updateTarget(target, change > 0));
|
|
260
|
+
targetFirstAttrMutationRecord.forEach((mutation, target)=>{
|
|
261
|
+
if (!target.isConnected) return;
|
|
262
|
+
const oldClassList = mutation.oldValue ? mutation.oldValue.split(/\s+/) : [];
|
|
263
|
+
const newClassList = target.classList;
|
|
264
|
+
const addedClasses = [];
|
|
265
|
+
newClassList.forEach((c)=>{
|
|
266
|
+
if (!oldClassList.includes(c)) addedClasses.push(c);
|
|
267
|
+
});
|
|
268
|
+
const removedClasses = oldClassList.filter((c)=>!newClassList.contains(c));
|
|
269
|
+
if (addedClasses.length) {
|
|
270
|
+
updateClassCount(addedClasses, true);
|
|
271
|
+
}
|
|
272
|
+
if (removedClasses.length) {
|
|
273
|
+
updateClassCount(removedClasses, false);
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
/**
|
|
277
|
+
* Merge the class usage changes into the current class usage map.
|
|
278
|
+
*/ eachClassCounts.forEach((countChange, className)=>{
|
|
279
|
+
let currentCount = this.classCounts.get(className) || 0;
|
|
280
|
+
let newCount = currentCount + countChange;
|
|
281
|
+
if (newCount > 0) {
|
|
282
|
+
this.classCounts.set(className, newCount);
|
|
283
|
+
if (currentCount === 0) {
|
|
284
|
+
this.add(className);
|
|
285
|
+
}
|
|
286
|
+
} else {
|
|
287
|
+
this.classCounts.delete(className);
|
|
288
|
+
this.remove(className);
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:mutated', {
|
|
292
|
+
records: mutationRecords,
|
|
293
|
+
classCounts: eachClassCounts,
|
|
294
|
+
cssRuntime: this
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
this.observer.observe(this.root, {
|
|
298
|
+
attributes: true,
|
|
299
|
+
attributeOldValue: true,
|
|
300
|
+
attributeFilter: [
|
|
301
|
+
'class'
|
|
302
|
+
],
|
|
303
|
+
childList: true,
|
|
304
|
+
subtree: true
|
|
305
|
+
});
|
|
306
|
+
if (!this.progressive) {
|
|
307
|
+
this.host.removeAttribute('hidden');
|
|
308
|
+
}
|
|
309
|
+
// @ts-ignore
|
|
310
|
+
this.observing = true;
|
|
311
|
+
globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:observed', {
|
|
312
|
+
cssRuntime: this
|
|
313
|
+
});
|
|
314
|
+
return this;
|
|
315
|
+
}
|
|
316
|
+
hydrate(nativeLayerRules) {
|
|
317
|
+
const cssLayerRules = [];
|
|
318
|
+
const checkSheet = new CSSStyleSheet();
|
|
319
|
+
const result = {
|
|
320
|
+
allSyntaxRules: []
|
|
321
|
+
};
|
|
322
|
+
for(let i = 0; i < nativeLayerRules.length; i++){
|
|
323
|
+
const eachNativeCSSRule = nativeLayerRules[i];
|
|
324
|
+
if (eachNativeCSSRule.constructor.name === 'CSSLayerBlockRule') {
|
|
325
|
+
const eachCSSLayerRule = eachNativeCSSRule;
|
|
326
|
+
if (eachNativeCSSRule.name === 'theme') {
|
|
327
|
+
this.themeLayer.native = eachCSSLayerRule;
|
|
328
|
+
let variableRule;
|
|
329
|
+
let lastVariableName;
|
|
330
|
+
for(let j = 0; j < eachCSSLayerRule.cssRules.length; j++){
|
|
331
|
+
const cssRule = eachCSSLayerRule.cssRules[j];
|
|
332
|
+
const variableCSSRule = cssRule.constructor.name === 'CSSMediaRule' ? cssRule.cssRules[0] : cssRule;
|
|
333
|
+
const variableName = variableCSSRule.style[0].slice(2);
|
|
334
|
+
if (variableName !== lastVariableName) {
|
|
335
|
+
lastVariableName = variableName;
|
|
336
|
+
variableRule = new Rule(variableName);
|
|
337
|
+
this.themeLayer.rules.push(variableRule);
|
|
338
|
+
this.themeLayer.tokenCounts.set(variableRule.name, 0);
|
|
339
|
+
}
|
|
340
|
+
variableRule?.nodes.push({
|
|
341
|
+
native: cssRule,
|
|
342
|
+
text: cssRule.cssText
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
if (this.themeLayer.rules.length) this.rules.push(this.themeLayer);
|
|
346
|
+
} else {
|
|
347
|
+
cssLayerRules.push(eachCSSLayerRule);
|
|
348
|
+
}
|
|
349
|
+
} else if (eachNativeCSSRule.constructor.name === 'CSSLayerStatementRule') {
|
|
350
|
+
this.layerStatementRule.nodes[0].native = eachNativeCSSRule;
|
|
351
|
+
} else if (eachNativeCSSRule.constructor.name === 'CSSKeyframesRule') {
|
|
352
|
+
const keyframsRule = eachNativeCSSRule;
|
|
353
|
+
const animationRule = new Rule(keyframsRule.name, [
|
|
354
|
+
{
|
|
355
|
+
native: keyframsRule,
|
|
356
|
+
text: keyframsRule.cssText
|
|
357
|
+
}
|
|
358
|
+
]);
|
|
359
|
+
this.animationsNonLayer.rules.push(animationRule);
|
|
360
|
+
this.rules.push(animationRule);
|
|
361
|
+
this.animationsNonLayer.tokenCounts.set(animationRule.name, 0);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
for (const eachCSSLayerRule of cssLayerRules){
|
|
365
|
+
let layer;
|
|
366
|
+
switch(eachCSSLayerRule.name){
|
|
367
|
+
case 'base':
|
|
368
|
+
layer = this.baseLayer;
|
|
369
|
+
break;
|
|
370
|
+
case 'preset':
|
|
371
|
+
layer = this.presetLayer;
|
|
372
|
+
break;
|
|
373
|
+
case 'components':
|
|
374
|
+
layer = this.componentsLayer;
|
|
375
|
+
break;
|
|
376
|
+
case 'general':
|
|
377
|
+
layer = this.generalLayer;
|
|
378
|
+
break;
|
|
379
|
+
default:
|
|
380
|
+
console.error(`Cannot recognize the layer \`${eachCSSLayerRule.name}\`. (https://rc.css.master.co/messages/hydration-errors)`);
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
layer.native = eachCSSLayerRule;
|
|
384
|
+
const unresolvedCSSRules = new Map();
|
|
385
|
+
for (const rule of eachCSSLayerRule.cssRules){
|
|
386
|
+
unresolvedCSSRules.set(rule.cssText, rule);
|
|
387
|
+
}
|
|
388
|
+
for (const eachNativeLayerRule of eachCSSLayerRule.cssRules){
|
|
389
|
+
if (!unresolvedCSSRules.has(eachNativeLayerRule.cssText)) continue;
|
|
390
|
+
const selectorText = this.getSelectorText(eachNativeLayerRule);
|
|
391
|
+
if (!selectorText) {
|
|
392
|
+
console.error(`Cannot get the selector text from \`${eachNativeLayerRule.cssText}\`. (${layer.name}) (https://rc.css.master.co/messages/hydration-errors)`);
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
const createdRules = this.createFromSelectorText(selectorText);
|
|
396
|
+
if (createdRules) {
|
|
397
|
+
for (const createdRule of createdRules){
|
|
398
|
+
layer.rules.push(createdRule);
|
|
399
|
+
layer.insertVariables(createdRule);
|
|
400
|
+
layer.insertAnimations(createdRule);
|
|
401
|
+
result.allSyntaxRules.push(createdRule);
|
|
402
|
+
createdRule.nodes.forEach((node)=>{
|
|
403
|
+
const createdNodeNativeRule = checkSheet.cssRules.item(checkSheet.insertRule(node.text));
|
|
404
|
+
if (createdNodeNativeRule) {
|
|
405
|
+
const match = unresolvedCSSRules.get(createdNodeNativeRule.cssText);
|
|
406
|
+
if (match) {
|
|
407
|
+
node.native = match;
|
|
408
|
+
unresolvedCSSRules.delete(createdNodeNativeRule.cssText);
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
console.error(`Cannot retrieve CSS rule for \`${node.selectorText}\`. (${layer.name}) (https://rc.css.master.co/messages/hydration-errors)`);
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
} else {
|
|
416
|
+
console.error(`Cannot recognize \`${eachNativeLayerRule.cssText}\`. (${layer.name}) (https://rc.css.master.co/messages/hydration-errors)`);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
if (layer.rules.length) this.rules.push(layer);
|
|
420
|
+
}
|
|
421
|
+
globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:hydrated', {
|
|
422
|
+
cssRuntime: this,
|
|
423
|
+
result
|
|
424
|
+
});
|
|
425
|
+
return result;
|
|
426
|
+
}
|
|
427
|
+
getSelectorText(cssRule) {
|
|
428
|
+
if (cssRule instanceof CSSStyleRule) {
|
|
429
|
+
return cssRule.selectorText;
|
|
430
|
+
} else if (cssRule instanceof CSSGroupingRule) {
|
|
431
|
+
return this.getSelectorText(cssRule.cssRules.item(0));
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
disconnect() {
|
|
435
|
+
if (!this.observing) return;
|
|
436
|
+
if (this.observer) {
|
|
437
|
+
this.observer.disconnect();
|
|
438
|
+
// @ts-expect-error readonly
|
|
439
|
+
this.observer = undefined;
|
|
440
|
+
}
|
|
441
|
+
// @ts-ignore
|
|
442
|
+
this.observing = false;
|
|
443
|
+
this.reset();
|
|
444
|
+
this.classCounts.clear();
|
|
445
|
+
if (!this.progressive) {
|
|
446
|
+
this.style?.remove();
|
|
447
|
+
this.style = null;
|
|
448
|
+
}
|
|
449
|
+
globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:disconnected', {
|
|
450
|
+
cssRuntime: this
|
|
451
|
+
});
|
|
452
|
+
return this;
|
|
453
|
+
}
|
|
454
|
+
refresh(customConfig = this.customConfig) {
|
|
455
|
+
if (!this.observing || !this.style.sheet) return this;
|
|
456
|
+
for(let i = 1; i <= this.style.sheet.cssRules.length - 1; i++){
|
|
457
|
+
this.style.sheet.deleteRule(i);
|
|
458
|
+
}
|
|
459
|
+
super.refresh(customConfig);
|
|
460
|
+
/**
|
|
461
|
+
* 拿當前所有的 classNames 按照最新的 colors, config.rules 匹配並生成新的 style
|
|
462
|
+
* 所以 refresh 過後 rules 可能會變多也可能會變少
|
|
463
|
+
*/ this.classCounts.forEach((_, className)=>{
|
|
464
|
+
this.add(className);
|
|
465
|
+
});
|
|
466
|
+
globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:refreshed', {
|
|
467
|
+
cssRuntime: this,
|
|
468
|
+
customConfig
|
|
469
|
+
});
|
|
470
|
+
return this;
|
|
471
|
+
}
|
|
472
|
+
destroy() {
|
|
473
|
+
this.disconnect();
|
|
474
|
+
globalThis.CSSRuntime.instances.delete(this.root);
|
|
475
|
+
globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:destroyed', {
|
|
476
|
+
cssRuntime: this
|
|
477
|
+
});
|
|
478
|
+
return this;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
(function(CSSRuntime) {
|
|
482
|
+
registerGlobal(CSSRuntime);
|
|
483
|
+
})(CSSRuntime);
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Initialize a new CSSRuntime instance and observe the target root
|
|
487
|
+
* @param config master css config
|
|
488
|
+
* @param root target root to observe
|
|
489
|
+
* @param autoObserve auto observe the target root
|
|
490
|
+
* @returns master css instance
|
|
491
|
+
*/ function initCSSRuntime(config, root = document, autoObserve = true) {
|
|
492
|
+
let cssRuntime = globalThis.CSSRuntime.instances.get(root);
|
|
493
|
+
if (cssRuntime) return cssRuntime;
|
|
494
|
+
cssRuntime = new CSSRuntime(root, config);
|
|
495
|
+
if (autoObserve) cssRuntime.observe();
|
|
496
|
+
return cssRuntime;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
export { CSSRuntime, CSSRuntime as default, initCSSRuntime };
|
package/dist/init.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import CSSRuntime from './core';
|
|
2
|
+
import type { Config } from '@master/css';
|
|
3
|
+
/**
|
|
4
|
+
* Initialize a new CSSRuntime instance and observe the target root
|
|
5
|
+
* @param config master css config
|
|
6
|
+
* @param root target root to observe
|
|
7
|
+
* @param autoObserve auto observe the target root
|
|
8
|
+
* @returns master css instance
|
|
9
|
+
*/
|
|
10
|
+
export default function initCSSRuntime(config?: Config, root?: Document | ShadowRoot, autoObserve?: boolean): CSSRuntime;
|
package/dist/layer.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Layer, Rule } from '@master/css';
|
|
2
|
+
import CSSRuntime from './core';
|
|
3
|
+
export default class RuntimeLayer extends Layer {
|
|
4
|
+
name: string;
|
|
5
|
+
cssRuntime: CSSRuntime;
|
|
6
|
+
readonly rules: Rule[];
|
|
7
|
+
readonly tokenCounts: Map<string, number>;
|
|
8
|
+
native: CSSLayerBlockRule | null;
|
|
9
|
+
constructor(name: string, cssRuntime: CSSRuntime);
|
|
10
|
+
attach(): void;
|
|
11
|
+
insert(rule: Rule, index?: number): number | undefined;
|
|
12
|
+
detach(): void;
|
|
13
|
+
delete(key: string): Rule | undefined;
|
|
14
|
+
reset(): void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Config } from '@master/css';
|
|
2
|
+
import type Core from './core';
|
|
3
|
+
declare global {
|
|
4
|
+
var CSSRuntime: typeof Core;
|
|
5
|
+
var cssRuntime: Core;
|
|
6
|
+
var masterCSSConfig: Config;
|
|
7
|
+
var masterCSSConfigs: Config[];
|
|
8
|
+
}
|
|
9
|
+
export default function registerGlobal(CSSRuntime: typeof Core): void;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import RuntimeLayer from './layer';
|
|
2
|
+
declare const RuntimeSyntaxLayer: {
|
|
3
|
+
new (...args: any[]): {
|
|
4
|
+
rules: import("@master/css").SyntaxRule[];
|
|
5
|
+
insert(syntaxRule: import("@master/css").SyntaxRule): number | undefined;
|
|
6
|
+
delete(key: string): import("@master/css").SyntaxRule | undefined;
|
|
7
|
+
insertVariables(syntaxRule: import("@master/css").SyntaxRule): void;
|
|
8
|
+
insertAnimations(syntaxRule: import("@master/css").SyntaxRule): void;
|
|
9
|
+
readonly tokenCounts: Map<string, number>;
|
|
10
|
+
name: string;
|
|
11
|
+
css: import("@master/css").MasterCSS;
|
|
12
|
+
attach(): void;
|
|
13
|
+
detach(): void;
|
|
14
|
+
reset(): void;
|
|
15
|
+
readonly text: string;
|
|
16
|
+
};
|
|
17
|
+
} & typeof RuntimeLayer;
|
|
18
|
+
export declare type RuntimeSyntaxLayerInstance = InstanceType<typeof RuntimeSyntaxLayer>;
|
|
19
|
+
export default RuntimeSyntaxLayer;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './hydrate-result';
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@master/css-runtime","scripts":{"build":"
|
|
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.60","@master/css-devtools-hook":"^2.0.0-rc.60"},"devDependencies":{"@playwright/test":"^1.49.1","shared":"^2.0.0-rc.60"},"version":"2.0.0-rc.60"}
|