Skip to content

Commit 3609920

Browse files
committed
atrule fix
1 parent 7fc9418 commit 3609920

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/getSelectorName.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
let newSel = 1
22
let parentNum = 1
33
const parentParamsSuffixs = {}
4-
module.exports = function getSelectorName(decl, parentParams, opt = {}) {
5-
let name = []
6-
const { ruleType, prefix = '' } = opt
4+
module.exports = function getSelectorName(decl, opt = {}) {
5+
const { ruleType, prefix = '', parentParams, parentName, atRulesConfig } = opt
6+
const name = [prefix]
77
if (ruleType === 'atomic') {
88
// eslint-disable-line no-warning-comments TODO: 暂定使用atomic规则
99
} else {
10-
name = [prefix, newSel]
10+
name[1] = newSel
1111
newSel++
1212
}
1313
if (parentParams) {
14-
parentParamsSuffixs[parentParams] = parentParamsSuffixs[parentParams] || parentNum++
15-
name.push(parentParamsSuffixs[parentParams])
14+
const atRulesConfigKey = ('@' + parentName + parentParams).replace(/ /g, '')
15+
const atRuleSuffix = (atRulesConfig[atRulesConfigKey] || {}).suffix
16+
parentParamsSuffixs[parentParams] = parentParamsSuffixs[parentParams] || atRuleSuffix || parentNum++
17+
// name.push(parentParamsSuffixs[parentParams])
18+
name[1] = name[1] + '_' + parentParamsSuffixs[parentParams]
1619
}
1720
return name.join('-')
1821
}

src/processCss.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const _ = require('lodash')
44
const cssnano = require('cssnano')
55
const getSelectorName = require('./getSelectorName')
66
const getSelectorType = require('./getSelectorType')
7+
const pseudoMapDefault = require('./pseudoMap')
78

89
const cacheLocalRuleInfo = {}
910
const parserPlugin = postcss.plugin('postcss-flat', (options) => {
@@ -13,6 +14,7 @@ const parserPlugin = postcss.plugin('postcss-flat', (options) => {
1314
rules,
1415
atRulesConfig,
1516
htmlClass,
17+
pseudoMap,
1618
} = options
1719
const localsMap = _.invert(locals)
1820
const localRuleMark = { normal: {} }
@@ -21,9 +23,10 @@ const parserPlugin = postcss.plugin('postcss-flat', (options) => {
2123
const globalRule = []
2224
css.walkRules((rule) => {
2325
let parentParams = ''
26+
let parentName = ''
2427
let keySuffix = '@'
2528
if (rule.parent.type === 'atrule') {
26-
const parentName = rule.parent.name
29+
parentName = rule.parent.name
2730
if (parentName === 'supports' || parentName === 'media') {
2831
parentParams = rule.parent.params
2932
keySuffix = keySuffix + parentName + parentParams
@@ -47,18 +50,14 @@ const parserPlugin = postcss.plugin('postcss-flat', (options) => {
4750
const value = decl.value
4851
let key = prop + ':' + value + ';' + selectorHalf + keySuffix
4952
if (!cacheLocalRuleInfo[key]) {
50-
const newClassName = getSelectorName(decl, parentParams, { rules, prefix, atRulesConfig })
53+
const newClassName = getSelectorName(decl, { parentName, parentParams, rules, prefix, atRulesConfig, selectorHalf, pseudoMap })
5154
let propLen = 0
5255
let priority = ''
5356
if (prop[0] !== '-') {
5457
propLen = prop.split('-').length
5558
}
5659
for (let i = 1; i < propLen; i++ ) {
57-
if (i === 1) {
58-
priority += 'html'
59-
} else {
60-
priority += '.' + htmlClass
61-
}
60+
priority += '.' + htmlClass
6261
}
6362
cacheLocalRuleInfo[key] = {
6463
newClassName,
@@ -82,8 +81,11 @@ const parserPlugin = postcss.plugin('postcss-flat', (options) => {
8281
rule.remove()
8382
})
8483
css.walkAtRules(/media|supports/, rule => {
84+
const atRulesConfigKey = ('@' + rule.name + rule.params).replace(/ /g, '')
8585
for (let key in localRuleMark[rule.params]) {
86-
const { newClassName, selectorHalf = '', priority, keySuffix } = cacheLocalRuleInfo[key]
86+
const { newClassName, selectorHalf = '', priority: tempP, keySuffix } = cacheLocalRuleInfo[key]
87+
const atRulePriority = (atRulesConfig[atRulesConfigKey] || {}).priority || ''
88+
const priority = _.trim(atRulePriority + tempP) + ' '
8789
rule.append(priority + '.' + newClassName + selectorHalf + '{' + key.replace(';' + selectorHalf + keySuffix, '') + '}')
8890
}
8991
})
@@ -106,16 +108,19 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
106108
rules = {},
107109
atRules = [],
108110
htmlClass = 'css-flat',
111+
pseudoMap = pseudoMapDefault,
109112
} = options.params || {}
110113

111114
const atRulesConfig = {}
112115
atRules.forEach((atRule, i) => {
113-
for (let [key, value] of Object.entries(atRule)) {
114-
atRulesConfig[key] = {
116+
for (let key in atRule) {
117+
const value = atRule[key]
118+
atRulesConfig[key.replace(/ /g, '')] = {
115119
suffix: value,
116120
priority: Array(i + 1).fill('.' + htmlClass).join(''),
117121
}
118122
}
123+
119124
})
120125

121126
const parserOptions = {
@@ -124,6 +129,7 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
124129
atRulesConfig,
125130
htmlClass,
126131
locals: options.locals || {},
132+
pseudoMap,
127133
}
128134

129135
const pipeline = postcss([
@@ -157,6 +163,7 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
157163
annotation: false,
158164
},
159165
}).then(function (result) {
166+
console.log(result.css)
160167
callback(null, {
161168
source: result.css,
162169
map: result.map && result.map.toJSON(),

0 commit comments

Comments
 (0)