Skip to content

Commit

Permalink
Merge #304
Browse files Browse the repository at this point in the history
  • Loading branch information
1aron committed Nov 8, 2023
2 parents cb5e5cf + e8f697f commit e4d1e99
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
49 changes: 30 additions & 19 deletions packages/css/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class MasterCSS {
if (!customConfig?.override) {
this.config = this.getExtendedConfig(defaultConfig, customConfig)
} else {
this.config = this.getExtendedConfig(this.config)
this.config = this.getExtendedConfig(customConfig)
}
this.resolve()
globalThis.masterCSSs.push(this)
Expand Down Expand Up @@ -366,8 +366,8 @@ export class MasterCSS {
})
}

const rulesEntries = Object.entries(rules)
.sort((a: any, b: any) => {
if (rules) {
const rulesEntries = Object.entries(rules).sort((a: any, b: any) => {
if (a[1].layer !== b[1].layer) {
return (b[1].layer || 0) - (a[1].layer || 0)
}
Expand All @@ -386,14 +386,21 @@ export class MasterCSS {
Object.assign(
eachRuleOptions.resolvedVariables,
Object.keys(this.variables)
.filter(eachVariableName => eachVariableName.startsWith(prefix + '-') || eachVariableName.startsWith('-' + prefix + '-'))
.filter((eachVariableName) =>
eachVariableName.startsWith(prefix + '-') ||
eachVariableName.startsWith('-' + prefix + '-'),
)
.reduce((newResolvedVariables, eachVariableName) => {
newResolvedVariables[eachVariableName.slice(prefix.length + (prefix.startsWith('-') ? 0 : 1))] = {
newResolvedVariables[
eachVariableName.slice(
prefix.length + (prefix.startsWith('-') ? 0 : 1),
)
] = {
...this.variables[eachVariableName],
name: eachVariableName
name: eachVariableName,
}
return newResolvedVariables
}, {})
}, {}),
)
}
// 1. custom `config.rules[id].variables`
Expand All @@ -413,13 +420,15 @@ export class MasterCSS {
valueMatches.push(`(?:${values.join('|')})(?![a-zA-Z0-9-])`)
}
if (Object.keys(eachRuleOptions.resolvedVariables).length) {
valueMatches.push(`(?:${Object.keys(eachRuleOptions.resolvedVariables).join('|')})(?![a-zA-Z0-9-])`)
valueMatches.push(
`(?:${Object.keys(eachRuleOptions.resolvedVariables).join('|')})(?![a-zA-Z0-9-])`,
)
}
if (eachRuleOptions.colored) {
valueMatches.push(
'#',
'(?:color|color-contrast|color-mix|hwb|lab|lch|oklab|oklch|rgb|rgba|hsl|hsla)\\(.*\\)',
`(?:${colorNames.join('|')})(?![a-zA-Z0-9-])`
`(?:${colorNames.join('|')})(?![a-zA-Z0-9-])`,
)
}
if (eachRuleOptions.numeric) {
Expand All @@ -431,7 +440,9 @@ export class MasterCSS {
eachRuleOptions.resolvedMatch = match as RegExp
}
}
})
},
)
}
}

/**
Expand Down Expand Up @@ -483,7 +494,7 @@ export class MasterCSS {
if (result) {
const firstCSSRule = (eachCSSRule as CSSMediaRule).cssRules[0]
if (
firstCSSRule?.constructor.name === 'CSSStyleRule'
firstCSSRule?.constructor.name === 'CSSStyleRule'
&& (firstCSSRule as CSSStyleRule).selectorText === ':root'
) {
this.pushVariableNativeRule(result[1], firstCSSRule as CSSStyleRule)
Expand Down Expand Up @@ -1535,7 +1546,7 @@ export class MasterCSS {
} else {
selectorText = ':root'
}

if (sheet) {
const newCSSRuleIndex = this.variablesNativeRules
? this.rules[0].natives.length
Expand Down Expand Up @@ -1581,10 +1592,10 @@ export class MasterCSS {
}
}
)
cssRule = {
cssRule = {
selectorText,
style,
styleMap
style,
styleMap
} as any as CSSStyleRule
if (mediaConditionText) {
// @ts-ignore
Expand Down Expand Up @@ -1619,10 +1630,10 @@ export class MasterCSS {
if (!this.variablesNativeRules) {
this.variablesNativeRules = {}
this.rules.splice(
0,
0,
{
natives: [],
0,
0,
{
natives: [],
get text() {
return this.natives.map((eachNative) => eachNative.text).join('')
}
Expand Down
20 changes: 20 additions & 0 deletions packages/css/tests/config/override/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Config, CoreLayer } from '../../../src'
import { testCSS } from '../../css'

const customConfig: Config = {
override: true,
rootSize: 10,
rules: {
fontSize: {
match: ['custom'],
numeric: true,
unit: 'rem',
layer: CoreLayer.Native,
},
},
}

test('override', () => {
testCSS('font:16', '', customConfig)
testCSS('custom:16', '.custom\\:16{font-size:1.6rem}', customConfig)
})

0 comments on commit e4d1e99

Please sign in to comment.