@master/css 2.0.0-rc.58 → 2.0.0-rc.59

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.
@@ -1,294 +1,6 @@
1
+ import withSyntaxLayer from './factories/with-syntax-layer.mjs';
1
2
  import Layer from './layer.mjs';
2
- import { Rule } from './rule.mjs';
3
3
 
4
- class SyntaxLayer extends Layer {
5
- rules = [];
6
- /**
7
- * normal
8
- * normal selectors
9
- * media normal
10
- * media selectors
11
- * media width
12
- * media width selectors
13
- */ insert(syntaxRule) {
14
- if (this.rules.includes(syntaxRule)) return;
15
- let index;
16
- /**
17
- * 必須按斷點值遞增,並透過索引插入,
18
- * 以實現響應式先後套用的規則
19
- * @example <1 <2 <3 ALL >=1 >=2 >=3
20
- * @description
21
- */ const endIndex = this.rules.length - 1;
22
- const { at, atToken, order, priority } = syntaxRule;
23
- const findIndex = (startIndex, stopCheck, matchCheck)=>{
24
- let i = startIndex;
25
- for(; i <= endIndex; i++){
26
- const eachSyntaxRule = this.rules[i];
27
- if (stopCheck?.(eachSyntaxRule)) return matchCheck ? -1 : i - 1;
28
- if (matchCheck?.(eachSyntaxRule)) return i;
29
- }
30
- return matchCheck ? -1 : i - 1;
31
- };
32
- let matchStartIndex;
33
- let matchEndIndex;
34
- if (atToken) {
35
- const mediaStartIndex = this.rules.findIndex((eachSyntaxRule)=>eachSyntaxRule.at?.media);
36
- if (mediaStartIndex === -1) {
37
- index = endIndex + 1;
38
- } else {
39
- const maxWidthFeature = at.media?.find(({ name })=>name === 'max-width');
40
- const minWidthFeature = at.media?.find(({ name })=>name === 'min-width');
41
- if (maxWidthFeature || minWidthFeature) {
42
- const mediaWidthStartIndex = this.rules.findIndex((eachSyntaxRule)=>eachSyntaxRule.at?.media?.find(({ name })=>name === 'max-width' || name === 'min-width'));
43
- if (mediaWidthStartIndex === -1) {
44
- index = endIndex + 1;
45
- } else {
46
- if (maxWidthFeature && minWidthFeature) {
47
- /**
48
- * 範圍越小 ( 越限定 越侷限 ) 越優先,
49
- * 按照範圍 max-width - min-width 遞減排序
50
- * find 第一個所遇到同樣 feature 且範圍值比自己大的 rule,
51
- * 並插入在該 rule 之後,讓自己優先被套用
52
- */ if (priority === -1) {
53
- matchStartIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.priority !== -1, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width'));
54
- matchEndIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.priority !== -1);
55
- } else {
56
- matchStartIndex = findIndex(mediaWidthStartIndex, undefined, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') && eachSyntaxRule.priority !== -1);
57
- matchEndIndex = endIndex;
58
- }
59
- if (matchStartIndex !== -1) {
60
- const range = maxWidthFeature.value - minWidthFeature.value;
61
- let i = matchEndIndex;
62
- const endI = matchStartIndex;
63
- matchStartIndex = matchEndIndex + 1;
64
- for(; i >= endI; i--){
65
- const eachSyntaxRule = this.rules[i];
66
- const eachMaxWidthFeature = eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width');
67
- const eachMinWidthFeature = eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width');
68
- if (!eachMaxWidthFeature || !eachMinWidthFeature) {
69
- break;
70
- } else {
71
- const eachRange = eachMaxWidthFeature.value - eachMinWidthFeature.value;
72
- if (eachRange < range) {
73
- matchEndIndex = i - 1;
74
- } else if (eachRange === range) {
75
- matchStartIndex = i;
76
- } else {
77
- break;
78
- }
79
- }
80
- }
81
- }
82
- } else if (minWidthFeature) {
83
- /**
84
- * find 第一個所遇到同樣 feature 且值比自己大的 rule,
85
- * 並插入在該 rule 之後,讓自己優先被套用
86
- */ if (priority === -1) {
87
- matchStartIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') || eachSyntaxRule.priority !== -1, (eachSyntaxRule)=>!eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width'));
88
- matchEndIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') || eachSyntaxRule.priority !== -1);
89
- } else {
90
- matchStartIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') && eachSyntaxRule.priority !== -1, (eachSyntaxRule)=>!eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') && eachSyntaxRule.priority !== -1);
91
- matchEndIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') && eachSyntaxRule.priority !== -1);
92
- }
93
- if (matchStartIndex !== -1) {
94
- for(let i = matchEndIndex; i >= matchStartIndex; i--){
95
- const feature = this.rules[i].at.media?.find(({ name })=>name === 'min-width');
96
- if (!feature) continue;
97
- if (feature.value > minWidthFeature.value) {
98
- matchEndIndex = i - 1;
99
- } else if (feature.value < minWidthFeature.value) {
100
- matchStartIndex = i + 1;
101
- break;
102
- }
103
- }
104
- }
105
- } else {
106
- /**
107
- * find 第一個所遇到同樣 feature 且值比自己大的 rule,
108
- * 並插入在該 rule 之後,讓自己優先被套用
109
- */ if (priority === -1) {
110
- matchStartIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') || eachSyntaxRule.priority !== -1, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width'));
111
- matchEndIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') || eachSyntaxRule.priority !== -1);
112
- } else {
113
- matchStartIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') && eachSyntaxRule.priority !== -1, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width') && eachSyntaxRule.priority !== -1);
114
- matchEndIndex = findIndex(mediaWidthStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'min-width') && eachSyntaxRule.priority !== -1);
115
- }
116
- if (matchStartIndex !== -1) {
117
- for(let i = matchEndIndex; i >= matchStartIndex; i--){
118
- const feature = this.rules[i].at.media?.find(({ name })=>name === 'max-width');
119
- if (!feature) continue;
120
- if (feature.value < maxWidthFeature.value) {
121
- matchEndIndex = i - 1;
122
- } else if (feature.value > maxWidthFeature.value) {
123
- matchStartIndex = i + 1;
124
- break;
125
- }
126
- }
127
- }
128
- }
129
- }
130
- } else {
131
- if (priority === -1) {
132
- matchStartIndex = mediaStartIndex;
133
- matchEndIndex = findIndex(mediaStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width' || name === 'min-width') || eachSyntaxRule.priority !== -1);
134
- } else {
135
- matchStartIndex = findIndex(mediaStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width' || name === 'min-width'), (eachSyntaxRule)=>eachSyntaxRule.priority !== -1);
136
- matchEndIndex = findIndex(mediaStartIndex, (eachSyntaxRule)=>eachSyntaxRule.at.media?.find(({ name })=>name === 'max-width' || name === 'min-width'));
137
- }
138
- }
139
- }
140
- } else {
141
- if (priority === -1) {
142
- matchStartIndex = 0;
143
- matchEndIndex = findIndex(0, (eachSyntax)=>eachSyntax.atToken || eachSyntax.priority !== -1);
144
- } else {
145
- matchStartIndex = findIndex(0, (eachSyntax)=>eachSyntax.atToken, (eachSyntax)=>eachSyntax.priority !== -1);
146
- matchEndIndex = findIndex(0, (eachSyntax)=>eachSyntax.atToken);
147
- }
148
- }
149
- if (index === undefined && matchEndIndex !== undefined && matchStartIndex !== undefined) {
150
- if (matchStartIndex === -1) {
151
- index = matchEndIndex + 1;
152
- } else {
153
- if (priority === -1) {
154
- for(let i = matchStartIndex; i <= matchEndIndex; i++){
155
- const currentSyntaxRule = this.rules[i];
156
- if (currentSyntaxRule.order >= order) {
157
- index = i;
158
- break;
159
- }
160
- }
161
- } else {
162
- for(let i = matchStartIndex; i <= matchEndIndex; i++){
163
- const currentSyntaxRule = this.rules[i];
164
- if (currentSyntaxRule.priority < priority) {
165
- index = i;
166
- break;
167
- } else if (currentSyntaxRule.priority === priority) {
168
- if (currentSyntaxRule.order >= order) {
169
- index = i;
170
- break;
171
- }
172
- } else {
173
- index = i + 1;
174
- }
175
- }
176
- }
177
- if (index === undefined) {
178
- index = matchEndIndex + 1;
179
- }
180
- }
181
- }
182
- super.insert(syntaxRule, index);
183
- this.insertVariables(syntaxRule);
184
- this.insertAnimations(syntaxRule);
185
- syntaxRule.definition.insert?.call(syntaxRule);
186
- return index;
187
- }
188
- delete(key) {
189
- const syntaxRule = super.delete(key);
190
- if (!syntaxRule) return;
191
- if (syntaxRule.variableNames) {
192
- for (const eachVariableName of syntaxRule.variableNames){
193
- if (!--this.css.themeLayer.usages[eachVariableName]) {
194
- this.css.themeLayer.delete(eachVariableName);
195
- delete this.css.themeLayer.usages[eachVariableName];
196
- }
197
- }
198
- }
199
- if (syntaxRule.animationNames) {
200
- for (const eachKeyframeName of syntaxRule.animationNames){
201
- if (!--this.css.animationsNonLayer.usages[eachKeyframeName]) {
202
- this.css.animationsNonLayer.delete(eachKeyframeName);
203
- delete this.css.animationsNonLayer.usages[eachKeyframeName];
204
- }
205
- }
206
- }
207
- syntaxRule.definition.delete?.call(syntaxRule, syntaxRule.name);
208
- return syntaxRule;
209
- }
210
- insertVariables(syntaxRule) {
211
- if (syntaxRule.variableNames) {
212
- for (const eachVariableName of syntaxRule.variableNames){
213
- const variable = this.css.variables[eachVariableName];
214
- if (this.css.themeLayer.rules.find(({ name })=>name === eachVariableName)) {
215
- this.css.themeLayer.usages[eachVariableName]++;
216
- } else {
217
- const newRule = new Rule(eachVariableName);
218
- const addNative = (mode, _variable)=>{
219
- let isDefaultMode = false;
220
- let preifxCssRuleText;
221
- let selectorText;
222
- let endCurlyBracketCount = 1;
223
- if (mode) {
224
- switch(this.css.config.modes?.[mode]){
225
- case 'media':
226
- selectorText = ':root';
227
- preifxCssRuleText = `@media(prefers-color-scheme:${mode}){${selectorText}`;
228
- endCurlyBracketCount++;
229
- break;
230
- case 'host':
231
- preifxCssRuleText = selectorText = `:host(.${mode})`;
232
- if (!variable.value && this.css.config.defaultMode === mode) {
233
- preifxCssRuleText += ',:host';
234
- isDefaultMode = true;
235
- }
236
- break;
237
- case 'class':
238
- preifxCssRuleText = selectorText = `.${mode}`;
239
- if (!variable.value && this.css.config.defaultMode === mode) {
240
- preifxCssRuleText += ',:root';
241
- isDefaultMode = true;
242
- }
243
- break;
244
- default:
245
- return;
246
- }
247
- } else {
248
- preifxCssRuleText = selectorText = ':root';
249
- }
250
- const cssRuleText = `${preifxCssRuleText}{--${eachVariableName}:${String(_variable.value)}${'}'.repeat(endCurlyBracketCount)}`;
251
- if (isDefaultMode) {
252
- newRule.nodes.unshift({
253
- selectorText,
254
- text: cssRuleText
255
- });
256
- } else {
257
- newRule.nodes.push({
258
- text: cssRuleText
259
- });
260
- }
261
- };
262
- if (variable.value) {
263
- addNative('', variable);
264
- }
265
- if (variable.modes) {
266
- for(const mode in variable.modes){
267
- addNative(mode, variable.modes[mode]);
268
- }
269
- }
270
- this.css.themeLayer.insert(newRule);
271
- this.css.themeLayer.usages[eachVariableName] = 1;
272
- }
273
- }
274
- }
275
- }
276
- insertAnimations(syntaxRule) {
277
- if (syntaxRule.animationNames) {
278
- for (const eachAnimationName of syntaxRule.animationNames){
279
- if (this.css.animationsNonLayer.rules.find(({ name })=>name === eachAnimationName)) {
280
- this.css.animationsNonLayer.usages[eachAnimationName]++;
281
- } else {
282
- this.css.animationsNonLayer.insert(new Rule(eachAnimationName, [
283
- {
284
- text: `@keyframes ${eachAnimationName}{` + Object.entries(this.css.animations[eachAnimationName]).map(([key, variables])=>`${key}{${Object.entries(variables).map(([name, value])=>name + ':' + value).join(';')}}`).join('') + '}'
285
- }
286
- ]));
287
- this.css.animationsNonLayer.usages[eachAnimationName] = 1;
288
- }
289
- }
290
- }
291
- }
292
- }
4
+ const SyntaxLayer = withSyntaxLayer(Layer);
293
5
 
294
6
  export { SyntaxLayer as default };
@@ -35,7 +35,7 @@ class SyntaxRule extends rule.Rule {
35
35
  break;
36
36
  // todo: 應挪到 parseValues 階段處理才能支援 variables: { x: 'calc(20vw-30px)' } 這種情況,並且解析上可能會比較合理、精簡
37
37
  case 'variable':
38
- const variable = this.css.variables[eachValueComponent.name];
38
+ const variable = this.css.variables.get(eachValueComponent.name);
39
39
  if (variable) {
40
40
  const handleVariable = (normalHandler, varHandler)=>{
41
41
  if (variable.modes) {
@@ -127,7 +127,8 @@ class SyntaxRule extends rule.Rule {
127
127
  let handled = false;
128
128
  if (!isVarFunction || currentValueComponents.length) {
129
129
  const handleVariable = (variableName, alpha)=>{
130
- const variable = Object.prototype.hasOwnProperty.call(this.variables, variableName) ? this.variables[variableName] : Object.prototype.hasOwnProperty.call(this.css.variables, variableName) ? this.css.variables[variableName] : undefined;
130
+ const globalVariableValue = this.css.variables.get(variableName);
131
+ const variable = Object.prototype.hasOwnProperty.call(this.variables, variableName) ? this.variables[variableName] : globalVariableValue;
131
132
  if (variable) {
132
133
  const name = variable.name ?? variableName;
133
134
  if (!bypassVariableNames.includes(name)) {
@@ -135,7 +136,7 @@ class SyntaxRule extends rule.Rule {
135
136
  const valueComponent = {
136
137
  type: 'variable',
137
138
  name,
138
- variable: this.css.variables[name],
139
+ variable: this.css.variables.get(name),
139
140
  token: currentValue
140
141
  };
141
142
  if (alpha) valueComponent.alpha = alpha;
@@ -274,7 +275,7 @@ class SyntaxRule extends rule.Rule {
274
275
  valueToken = name.slice(indexOfColon + 1);
275
276
  }
276
277
  this.valueComponents = [];
277
- const parsedValueIndex = this.parseValues(this.valueComponents, 0, valueToken, unit, '', undefined, false, definition.includeAnimations ? Object.keys(this.css.animations) : []);
278
+ const parsedValueIndex = this.parseValues(this.valueComponents, 0, valueToken, unit, '', undefined, false, definition.includeAnimations ? Array.from(this.css.animations.keys()) : []);
278
279
  this.valueToken = valueToken.slice(0, parsedValueIndex);
279
280
  stateToken = valueToken.slice(parsedValueIndex);
280
281
  }
@@ -373,8 +374,8 @@ class SyntaxRule extends rule.Rule {
373
374
  value: 'and'
374
375
  });
375
376
  } else if (atComponentToken.startsWith('')) {
376
- const targetAt = at[atComponentToken];
377
- if (targetAt && typeof targetAt === 'string') {
377
+ const targetAt = at.get(atComponentToken);
378
+ if (typeof targetAt === 'string') {
378
379
  const match = targetAt.match(queryTypeRegExp);
379
380
  queryType = match ? match[1] : '';
380
381
  if (!queryType) throw new Error(`Invalid query '${atComponentToken}': '${targetAt}'`);
@@ -404,7 +405,7 @@ class SyntaxRule extends rule.Rule {
404
405
  featureName = 'min-width';
405
406
  }
406
407
  const token = extremumOperator ? atComponentToken.replace(extremumOperator, '') : atComponentToken;
407
- const viewport = at[token];
408
+ const viewport = at.get(token);
408
409
  switch(featureName){
409
410
  case 'max-width':
410
411
  case 'min-width':
@@ -490,7 +491,7 @@ class SyntaxRule extends rule.Rule {
490
491
  const push = (propertyText)=>{
491
492
  // animations
492
493
  if (animations && (propertyText.startsWith('animation') || propertyText.startsWith('animation-name'))) {
493
- const animationNames = propertyText.split(':')[1].split('!important')[0].split(' ').filter((eachValue)=>eachValue in this.css.animations && (!this.animationNames || !this.animationNames.includes(eachValue)));
494
+ const animationNames = propertyText.split(':')[1].split('!important')[0].split(' ').filter((eachValue)=>this.css.animations.has(eachValue) && (!this.animationNames || !this.animationNames.includes(eachValue)));
494
495
  if (animationNames.length) {
495
496
  if (!this.animationNames) {
496
497
  this.animationNames = [];
@@ -33,7 +33,7 @@ class SyntaxRule extends Rule {
33
33
  break;
34
34
  // todo: 應挪到 parseValues 階段處理才能支援 variables: { x: 'calc(20vw-30px)' } 這種情況,並且解析上可能會比較合理、精簡
35
35
  case 'variable':
36
- const variable = this.css.variables[eachValueComponent.name];
36
+ const variable = this.css.variables.get(eachValueComponent.name);
37
37
  if (variable) {
38
38
  const handleVariable = (normalHandler, varHandler)=>{
39
39
  if (variable.modes) {
@@ -125,7 +125,8 @@ class SyntaxRule extends Rule {
125
125
  let handled = false;
126
126
  if (!isVarFunction || currentValueComponents.length) {
127
127
  const handleVariable = (variableName, alpha)=>{
128
- const variable = Object.prototype.hasOwnProperty.call(this.variables, variableName) ? this.variables[variableName] : Object.prototype.hasOwnProperty.call(this.css.variables, variableName) ? this.css.variables[variableName] : undefined;
128
+ const globalVariableValue = this.css.variables.get(variableName);
129
+ const variable = Object.prototype.hasOwnProperty.call(this.variables, variableName) ? this.variables[variableName] : globalVariableValue;
129
130
  if (variable) {
130
131
  const name = variable.name ?? variableName;
131
132
  if (!bypassVariableNames.includes(name)) {
@@ -133,7 +134,7 @@ class SyntaxRule extends Rule {
133
134
  const valueComponent = {
134
135
  type: 'variable',
135
136
  name,
136
- variable: this.css.variables[name],
137
+ variable: this.css.variables.get(name),
137
138
  token: currentValue
138
139
  };
139
140
  if (alpha) valueComponent.alpha = alpha;
@@ -272,7 +273,7 @@ class SyntaxRule extends Rule {
272
273
  valueToken = name.slice(indexOfColon + 1);
273
274
  }
274
275
  this.valueComponents = [];
275
- const parsedValueIndex = this.parseValues(this.valueComponents, 0, valueToken, unit, '', undefined, false, definition.includeAnimations ? Object.keys(this.css.animations) : []);
276
+ const parsedValueIndex = this.parseValues(this.valueComponents, 0, valueToken, unit, '', undefined, false, definition.includeAnimations ? Array.from(this.css.animations.keys()) : []);
276
277
  this.valueToken = valueToken.slice(0, parsedValueIndex);
277
278
  stateToken = valueToken.slice(parsedValueIndex);
278
279
  }
@@ -371,8 +372,8 @@ class SyntaxRule extends Rule {
371
372
  value: 'and'
372
373
  });
373
374
  } else if (atComponentToken.startsWith('')) {
374
- const targetAt = at[atComponentToken];
375
- if (targetAt && typeof targetAt === 'string') {
375
+ const targetAt = at.get(atComponentToken);
376
+ if (typeof targetAt === 'string') {
376
377
  const match = targetAt.match(queryTypeRegExp);
377
378
  queryType = match ? match[1] : '';
378
379
  if (!queryType) throw new Error(`Invalid query '${atComponentToken}': '${targetAt}'`);
@@ -402,7 +403,7 @@ class SyntaxRule extends Rule {
402
403
  featureName = 'min-width';
403
404
  }
404
405
  const token = extremumOperator ? atComponentToken.replace(extremumOperator, '') : atComponentToken;
405
- const viewport = at[token];
406
+ const viewport = at.get(token);
406
407
  switch(featureName){
407
408
  case 'max-width':
408
409
  case 'min-width':
@@ -488,7 +489,7 @@ class SyntaxRule extends Rule {
488
489
  const push = (propertyText)=>{
489
490
  // animations
490
491
  if (animations && (propertyText.startsWith('animation') || propertyText.startsWith('animation-name'))) {
491
- const animationNames = propertyText.split(':')[1].split('!important')[0].split(' ').filter((eachValue)=>eachValue in this.css.animations && (!this.animationNames || !this.animationNames.includes(eachValue)));
492
+ const animationNames = propertyText.split(':')[1].split('!important')[0].split(' ').filter((eachValue)=>this.css.animations.has(eachValue) && (!this.animationNames || !this.animationNames.includes(eachValue)));
492
493
  if (animationNames.length) {
493
494
  if (!this.animationNames) {
494
495
  this.animationNames = [];
@@ -7,8 +7,7 @@ function extendConfig(...configs) {
7
7
  const formatConfig = (config)=>{
8
8
  const clonedConfig = extend({}, config);
9
9
  const formatDeeply = (obj)=>{
10
- for(const key in obj){
11
- const value = obj[key];
10
+ Object.entries(obj).forEach(([key, value])=>{
12
11
  if (typeof value === 'object' && !Array.isArray(value)) {
13
12
  formatDeeply(value);
14
13
  } else if (key && !key.startsWith('@')) {
@@ -16,58 +15,45 @@ function extendConfig(...configs) {
16
15
  '': value
17
16
  };
18
17
  }
19
- }
18
+ });
20
19
  };
21
- if (clonedConfig.components) {
22
- formatDeeply(clonedConfig.components);
23
- }
24
- if (clonedConfig.at) {
25
- formatDeeply(clonedConfig.at);
26
- }
27
- if (clonedConfig.variables) {
28
- formatDeeply(clonedConfig.variables);
29
- }
20
+ [
21
+ 'components',
22
+ 'at',
23
+ 'variables'
24
+ ].forEach((key)=>{
25
+ if (clonedConfig[key]) {
26
+ formatDeeply(clonedConfig[key]);
27
+ }
28
+ });
30
29
  return clonedConfig;
31
30
  };
32
- const formattedConfigs = [];
33
- for (const eachConfig of configs){
34
- if (!eachConfig) continue;
35
- (function getConfigsDeeply(config) {
36
- if (config.extends?.length) {
37
- for (const eachExtend of config.extends){
38
- getConfigsDeeply('config' in eachExtend ? eachExtend.config : eachExtend);
39
- }
31
+ const collectConfigs = (config, result = [])=>{
32
+ if (!config) return result;
33
+ if (config.extends?.length) {
34
+ config.extends.forEach((ext)=>collectConfigs('config' in ext ? ext.config : ext, result));
35
+ }
36
+ result.push(formatConfig(config));
37
+ return result;
38
+ };
39
+ const formattedConfigs = configs.reduce((acc, config)=>collectConfigs(config, acc), []);
40
+ return formattedConfigs.reduce((extendedConfig, currentConfig)=>{
41
+ Object.entries(currentConfig).forEach(([key, value])=>{
42
+ if (key === 'animations' && value) {
43
+ Object.assign(extendedConfig.animations, value);
44
+ } else if (value) {
45
+ extendedConfig = extend(extendedConfig, {
46
+ [key]: value
47
+ });
40
48
  }
41
- formattedConfigs.push(formatConfig(config));
42
- })(eachConfig);
43
- }
44
- let extendedConfig = {
49
+ });
50
+ return extendedConfig;
51
+ }, {
45
52
  animations: {},
46
53
  components: {},
47
54
  at: {},
48
55
  variables: {}
49
- };
50
- for (const currentFormattedConfig of formattedConfigs){
51
- for(const key in currentFormattedConfig){
52
- switch(key){
53
- case 'animations':
54
- if (currentFormattedConfig.animations) {
55
- Object.assign(extendedConfig.animations, currentFormattedConfig.animations);
56
- }
57
- break;
58
- default:
59
- if (currentFormattedConfig[key]) {
60
- extendedConfig = extend(extendedConfig, {
61
- [key]: currentFormattedConfig[key]
62
- });
63
- }
64
- }
65
- // if (Object.prototype.hasOwnProperty.call(currentFormattedConfig.components, key)) {
66
- // Object.assign(extendedConfig.components[key], currentFormattedConfig.components[key])
67
- // }
68
- }
69
- }
70
- return extendedConfig;
56
+ });
71
57
  }
72
58
 
73
59
  module.exports = extendConfig;
@@ -5,8 +5,7 @@ function extendConfig(...configs) {
5
5
  const formatConfig = (config)=>{
6
6
  const clonedConfig = extend({}, config);
7
7
  const formatDeeply = (obj)=>{
8
- for(const key in obj){
9
- const value = obj[key];
8
+ Object.entries(obj).forEach(([key, value])=>{
10
9
  if (typeof value === 'object' && !Array.isArray(value)) {
11
10
  formatDeeply(value);
12
11
  } else if (key && !key.startsWith('@')) {
@@ -14,58 +13,45 @@ function extendConfig(...configs) {
14
13
  '': value
15
14
  };
16
15
  }
17
- }
16
+ });
18
17
  };
19
- if (clonedConfig.components) {
20
- formatDeeply(clonedConfig.components);
21
- }
22
- if (clonedConfig.at) {
23
- formatDeeply(clonedConfig.at);
24
- }
25
- if (clonedConfig.variables) {
26
- formatDeeply(clonedConfig.variables);
27
- }
18
+ [
19
+ 'components',
20
+ 'at',
21
+ 'variables'
22
+ ].forEach((key)=>{
23
+ if (clonedConfig[key]) {
24
+ formatDeeply(clonedConfig[key]);
25
+ }
26
+ });
28
27
  return clonedConfig;
29
28
  };
30
- const formattedConfigs = [];
31
- for (const eachConfig of configs){
32
- if (!eachConfig) continue;
33
- (function getConfigsDeeply(config) {
34
- if (config.extends?.length) {
35
- for (const eachExtend of config.extends){
36
- getConfigsDeeply('config' in eachExtend ? eachExtend.config : eachExtend);
37
- }
29
+ const collectConfigs = (config, result = [])=>{
30
+ if (!config) return result;
31
+ if (config.extends?.length) {
32
+ config.extends.forEach((ext)=>collectConfigs('config' in ext ? ext.config : ext, result));
33
+ }
34
+ result.push(formatConfig(config));
35
+ return result;
36
+ };
37
+ const formattedConfigs = configs.reduce((acc, config)=>collectConfigs(config, acc), []);
38
+ return formattedConfigs.reduce((extendedConfig, currentConfig)=>{
39
+ Object.entries(currentConfig).forEach(([key, value])=>{
40
+ if (key === 'animations' && value) {
41
+ Object.assign(extendedConfig.animations, value);
42
+ } else if (value) {
43
+ extendedConfig = extend(extendedConfig, {
44
+ [key]: value
45
+ });
38
46
  }
39
- formattedConfigs.push(formatConfig(config));
40
- })(eachConfig);
41
- }
42
- let extendedConfig = {
47
+ });
48
+ return extendedConfig;
49
+ }, {
43
50
  animations: {},
44
51
  components: {},
45
52
  at: {},
46
53
  variables: {}
47
- };
48
- for (const currentFormattedConfig of formattedConfigs){
49
- for(const key in currentFormattedConfig){
50
- switch(key){
51
- case 'animations':
52
- if (currentFormattedConfig.animations) {
53
- Object.assign(extendedConfig.animations, currentFormattedConfig.animations);
54
- }
55
- break;
56
- default:
57
- if (currentFormattedConfig[key]) {
58
- extendedConfig = extend(extendedConfig, {
59
- [key]: currentFormattedConfig[key]
60
- });
61
- }
62
- }
63
- // if (Object.prototype.hasOwnProperty.call(currentFormattedConfig.components, key)) {
64
- // Object.assign(extendedConfig.components[key], currentFormattedConfig.components[key])
65
- // }
66
- }
67
- }
68
- return extendedConfig;
54
+ });
69
55
  }
70
56
 
71
57
  export { extendConfig as default };
@@ -1 +1 @@
1
- export declare function flattenObject(obj: Record<string, any>, parentKey?: string): Record<string, any>;
1
+ export declare function flattenObject(obj: Map<string, any> | Record<string, any>, parentKey?: string): Record<string, any>;
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@master/css","type":"module","scripts":{"build":"techor build \"src/**/*.ts\"","dev":"pnpm build --watch","test":"vitest","type-check":"tsc --noEmit","lint":"eslint"},"license":"MIT","description":"The CSS language and framework for rapidly building modern and high-performance websites","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/core"},"keywords":["style","styles","html","variants","virtual","virtual-css","utility","css","ui","lightweight","class","webpack","plugin","integration","vite","parcel","build-tools","mastercss"],"sideEffects":false,"main":"./dist/index.cjs","jsnext:main":"./dist/index.mjs","esnext":"./dist/index.mjs","module":"./dist/index.mjs","types":"./dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","require":"./dist/index.cjs","import":"./dist/index.mjs"}},"bin":"./dist/bin/index.mjs","files":["dist"],"publishConfig":{"access":"public","provenance":true},"dependencies":{"@master/colors":"^2.1.0","@master/normal.css":"^3.1.0","@techor/extend":"^3.2.0","csstype":"^3.1.3"},"devDependencies":{"shared":"^2.0.0-rc.58"},"version":"2.0.0-rc.58"}
1
+ {"name":"@master/css","type":"module","scripts":{"build":"techor build \"src/**/*.ts\"","dev":"pnpm build --watch","test":"vitest","type-check":"tsc --noEmit","lint":"eslint"},"license":"MIT","description":"The CSS language and framework for rapidly building modern and high-performance websites","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/core"},"keywords":["style","styles","html","variants","virtual","virtual-css","utility","css","ui","lightweight","class","webpack","plugin","integration","vite","parcel","build-tools","mastercss"],"sideEffects":false,"main":"./dist/index.cjs","jsnext:main":"./dist/index.mjs","esnext":"./dist/index.mjs","module":"./dist/index.mjs","types":"./dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","require":"./dist/index.cjs","import":"./dist/index.mjs"}},"bin":"./dist/bin/index.mjs","files":["dist"],"publishConfig":{"access":"public","provenance":true},"dependencies":{"@master/colors":"^2.1.0","@master/normal.css":"^3.1.0","@techor/extend":"^3.2.0","csstype":"^3.1.3"},"devDependencies":{"shared":"^2.0.0-rc.59"},"version":"2.0.0-rc.59"}