Skip to content

Commit 496f767

Browse files
committed
Add function to get css variables declaration
1 parent 19b77da commit 496f767

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

lib/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ Builder.prototype.build = function(options) {
306306
}));
307307
const varsOverride = oVariableCollector.getAllVariables();
308308
const cssVariablesSource = oCSSVariablesCollector.toLessVariables(varsOverride);
309+
// eslint-disable-next-line no-unused-vars
310+
const cssVariablesOnly = oCSSVariablesCollector.getCssVariablesDeclaration();
311+
// TODO: export cssVariablesOnly so that it can be written as css_variables_only.less
309312

310313
let cssSkeletonRtl;
311314
if (oRTL) {

lib/plugin/css-variables-collector.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,25 @@ CSSVariablesCollectorPlugin.prototype = {
8787
const variableValue = this.calcVars[value].css;
8888
lessVariables += `@${variableName}: ${variableValue};\n`;
8989
});
90-
lessVariables += "\n:root {\n";
91-
Object.keys(vars).forEach((value, index) => {
92-
if (vars[value].export) {
93-
lessVariables += `--${value}: @${value};\n`;
94-
}
95-
});
96-
Object.keys(this.calcVars).forEach((value, index) => {
97-
if (this.calcVars[value].export) {
98-
lessVariables += `--${value}: @${value};\n`;
90+
lessVariables += "\n" + this.getCssVariablesDeclaration({includeCalcVars: true});
91+
return lessVariables;
92+
},
93+
getCssVariablesDeclaration({includeCalcVars = false} = {}) {
94+
let content = ":root {\n";
95+
Object.keys(this.vars).forEach((value) => {
96+
if (this.vars[value].export) {
97+
content += `--${value}: @${value};\n`;
9998
}
10099
});
101-
lessVariables += "}\n";
102-
return lessVariables;
100+
if (includeCalcVars) {
101+
Object.keys(this.calcVars).forEach((value) => {
102+
if (this.calcVars[value].export) {
103+
content += `--${value}: @${value};\n`;
104+
}
105+
});
106+
}
107+
content += "}\n";
108+
return content;
103109
},
104110

105111
_getCSS(node) {

0 commit comments

Comments
 (0)