mirror of
https://github.com/primer/css.git
synced 2024-12-26 15:43:59 +03:00
Upgrade analyze-variables plugin
This commit is contained in:
parent
266ca1f13d
commit
63764f7edd
@ -63,54 +63,57 @@ function analyzeVariables(file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function variablePlugin(variables) {
|
function variablePlugin(variables) {
|
||||||
return postcss.plugin('analyze-variables', (options = {}) => {
|
return (options = {}) => {
|
||||||
const {cwd = process.cwd()} = options
|
const {cwd = process.cwd()} = options
|
||||||
return root => {
|
return {
|
||||||
const decls = new Map()
|
postcssPlugin: 'analyze-variables',
|
||||||
|
Once(root) {
|
||||||
|
const decls = new Map()
|
||||||
|
|
||||||
root.walkDecls(/^\$/, decl => {
|
root.walkDecls(/^\$/, decl => {
|
||||||
const {prop, value} = decl
|
const {prop, value} = decl
|
||||||
if (decl.parent === root && !value.startsWith('(')) {
|
if (decl.parent === root && !value.startsWith('(')) {
|
||||||
decl.value = value.replace(/ *!default$/, '')
|
decl.value = value.replace(/ *!default$/, '')
|
||||||
decls.set(prop, decl)
|
decls.set(prop, decl)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
for (const [prop, decl] of decls.entries()) {
|
for (const [prop, decl] of decls.entries()) {
|
||||||
const {nodes} = valueParser(decl.value)
|
const {nodes} = valueParser(decl.value)
|
||||||
const values = [valueParser.stringify(nodes)]
|
const values = [valueParser.stringify(nodes)]
|
||||||
while (nodes.some(node => decls.has(node.value))) {
|
while (nodes.some(node => decls.has(node.value))) {
|
||||||
for (const node of nodes) {
|
for (const node of nodes) {
|
||||||
const {value} = node
|
const {value} = node
|
||||||
if (decls.has(value)) {
|
if (decls.has(value)) {
|
||||||
node.value = decls.get(value).value
|
node.value = decls.get(value).value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
values.push(valueParser.stringify(nodes))
|
||||||
|
}
|
||||||
|
|
||||||
|
const {source} = decl
|
||||||
|
variables[prop] = {
|
||||||
|
values,
|
||||||
|
source: {
|
||||||
|
path: source.input.file.replace(`${cwd}/`, ''),
|
||||||
|
line: source.start.line
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
values.push(valueParser.stringify(nodes))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const {source} = decl
|
const container = postcss.rule({selector: ':root'})
|
||||||
variables[prop] = {
|
for (const [prop, decl] of decls.entries()) {
|
||||||
values,
|
container.append(
|
||||||
source: {
|
postcss.decl({
|
||||||
path: source.input.file.replace(`${cwd}/`, ''),
|
prop: `--${prop.substr(1)}`,
|
||||||
line: source.start.line
|
value: `#{${decl.value}}`
|
||||||
}
|
})
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
root.append(container)
|
||||||
}
|
}
|
||||||
|
|
||||||
const container = postcss.rule({selector: ':root'})
|
|
||||||
for (const [prop, decl] of decls.entries()) {
|
|
||||||
container.append(
|
|
||||||
postcss.decl({
|
|
||||||
prop: `--${prop.substr(1)}`,
|
|
||||||
value: `#{${decl.value}}`
|
|
||||||
})
|
|
||||||
)
|
|
||||||
}
|
|
||||||
root.append(container)
|
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sortObject(obj, cmp) {
|
function sortObject(obj, cmp) {
|
||||||
|
Loading…
Reference in New Issue
Block a user