-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
80 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,49 @@ | ||
import postcss from 'postcss'; | ||
|
||
function hasVar(str) { | ||
return str.includes('var('); | ||
function hasVar(string) { | ||
return string.includes('var('); | ||
} | ||
|
||
function resolveValue(value, maps) { | ||
return hasVar(value) ? value.replace(/var\(--.*?\)/g, match => maps[match.slice(4, -1)] || match) : value; | ||
return hasVar(value) ? value.replace(/var\(--.*?\)/g, match => maps[match.slice(4, -1)] || match) : value; | ||
} | ||
|
||
function getProperty(nodes) { | ||
let propertys = {}; | ||
|
||
nodes.walkRules(rule => { | ||
if (rule.selector !== ':root') { | ||
return; | ||
} | ||
|
||
rule.each(({type, prop, value}) => { | ||
if (type === 'decl') { | ||
propertys[prop] = value; | ||
} | ||
}); | ||
let propertys = {}; | ||
|
||
nodes.walkRules(rule => { | ||
if (rule.selector !== ':root') { | ||
return; | ||
} | ||
|
||
rule.each(({type, prop: property, value}) => { | ||
if (type === 'decl') { | ||
propertys[property] = value; | ||
} | ||
}); | ||
}); | ||
|
||
return propertys; | ||
return propertys; | ||
} | ||
|
||
function circularReference(maps) { | ||
return Object.keys(maps).reduce((prevMaps, property) => { | ||
prevMaps[property] = resolveValue(maps[property], maps); | ||
return prevMaps; | ||
}, maps); | ||
return Object.keys(maps).reduce((previousMaps, property) => { | ||
previousMaps[property] = resolveValue(maps[property], maps); | ||
return previousMaps; | ||
}, maps); | ||
} | ||
|
||
export default postcss.plugin('postcss-at-rules-variables', (options = {}) => { | ||
options = { | ||
atRules: [...new Set(['for', 'if', 'else', 'each', 'mixin', 'custom-media', ...options.atRules || ''])], | ||
variables: options.variables || {} | ||
}; | ||
|
||
return nodes => { | ||
const maps = circularReference(Object.assign(getProperty(nodes), options.variables)); | ||
|
||
nodes.walkAtRules(new RegExp(options.atRules.join('|')), rules => { | ||
rules.params = resolveValue(rules.params, maps); | ||
}); | ||
}; | ||
options = { | ||
atRules: [...new Set(['for', 'if', 'else', 'each', 'mixin', 'custom-media', ...options.atRules || ''])], | ||
variables: options.variables || {} | ||
}; | ||
|
||
return nodes => { | ||
const maps = circularReference(Object.assign(getProperty(nodes), options.variables)); | ||
|
||
nodes.walkAtRules(new RegExp(options.atRules.join('|')), rules => { | ||
rules.params = resolveValue(rules.params, maps); | ||
}); | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters