Skip to content

Commit 333edbd

Browse files
committed
Add support for parsing media query theme-vars
1 parent daf9a49 commit 333edbd

2 files changed

Lines changed: 53 additions & 32 deletions

File tree

lib/loader.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ const postCssPlugins = require('./postcss-plugins');
99
const extractThemeRulesPlugin = postCssPlugins.extractThemeRulesPlugin;
1010
const removeCssModuleExports = postCssPlugins.removeCssModuleExports;
1111

12-
const stringifyRuleMap = (ruleMap) => {
13-
return Object.keys(ruleMap).map(selector => {
14-
const declsMap = ruleMap[selector];
15-
const decls = Object.keys(declsMap).map(prop => `${prop}: ${declsMap[prop]};`);
16-
17-
return `${selector} {\n\t${decls.join('\n\t')}\n}`;
18-
}).join('\n');
19-
};
20-
2112
// copied from css-loader/lib/getLocalIdent.js
2213
function getLocalIdent(loaderContext, localIdentName, localName, options) {
2314
if(!options.context)
@@ -46,8 +37,8 @@ module.exports = function (source, map) {
4637
loader.emitWarning(msg.toString());
4738
});
4839

49-
if (result.ruleMap) {
50-
const content = stringifyRuleMap(result.ruleMap);
40+
if (result.themeRoot) {
41+
const content = result.themeRoot.toString();
5142

5243
return {
5344
css: result.css,

lib/postcss-plugins.js

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,65 @@ module.exports.extractThemeRulesPlugin = postcss.plugin('postcss-extract-theme-r
88
const prefix = options.prefix || THEME_VAR_PREFIX;
99

1010
return (root, results) => {
11-
/*
12-
ruleMap = {
13-
'.example': {
14-
color: 'theme-var(--color-primary);',
15-
'font-weight': 'theme-var(--font-weight-medium);'
16-
}
17-
}
18-
*/
19-
let ruleMap;
11+
let themeRoot = new postcss.root();
2012

21-
root.walkRules(rule => {
13+
const extractThemeRule = rule => {
14+
let hasThemeVar = false
15+
let decls = [];
2216
rule.walkDecls(decl => {
2317
if (decl.value.indexOf(prefix) > -1) {
24-
ruleMap = ruleMap || (ruleMap = {});
25-
let declMap = ruleMap[rule.selector] || (ruleMap[rule.selector] = {});
26-
27-
let value = decl.value;
28-
29-
if (decl.important) {
30-
value += ' !important';
31-
}
32-
33-
declMap[decl.prop] = value;
18+
hasThemeVar = true
19+
let themeDecl = decl.clone();
20+
decls.push(themeDecl);
3421
decl.remove();
3522
}
3623
});
24+
if (decls.length > 0) {
25+
let themeRule = new postcss.rule();
26+
themeRule.selector = rule.selector;
27+
themeRule.nodes = decls;
28+
themeRule.raws = rule.raws;
29+
return themeRule
30+
}
31+
}
32+
33+
root.each(node => {
34+
let nodes = [];
35+
switch (node.type) {
36+
case 'rule':
37+
let themeRule = extractThemeRule(node);
38+
if (themeRule) {
39+
nodes.push(themeRule);
40+
}
41+
break;
42+
case 'atrule':
43+
let rules = []
44+
node.each(atnode => {
45+
if (atnode.type === 'rule') {
46+
let themeRule = extractThemeRule(atnode);
47+
if (themeRule) {
48+
rules.push(themeRule);
49+
}
50+
}
51+
})
52+
if (rules.length > 0) {
53+
let themeAtRule = new postcss.atRule()
54+
themeAtRule.params = node.params;
55+
themeAtRule.name = node.name;
56+
themeAtRule.nodes = rules;
57+
themeAtRule.raws = node.raws;
58+
nodes.push(themeAtRule);
59+
}
60+
break;
61+
}
62+
if (nodes.length > 0) {
63+
themeRoot.nodes = themeRoot.nodes.concat(nodes);
64+
}
3765
});
3866

39-
results.ruleMap = ruleMap;
67+
if (themeRoot.nodes.length > 0) {
68+
results.themeRoot = themeRoot
69+
}
4070
};
4171
});
4272

0 commit comments

Comments
 (0)