Skip to content

Commit f81c117

Browse files
authored
feat: make css vars fallback script work with embedded vars (#251)
1 parent e1af8b7 commit f81c117

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
const postcssImport = require('postcss-import');
2+
const postcssAddFallback = require('../../lib/postcss-add-fallback/index.js');
3+
24
module.exports = {
35
plugins: [
4-
postcssImport()
6+
postcssImport(),
7+
postcssAddFallback(),
58
]
6-
}
9+
};
710

packages/main/config/postcss.components/postcss.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ module.exports = {
66
postcssNesting(),
77
postcssAddFallback({importFrom: "./dist/themes-next/sap_fiori_3/parameters-bundle.css"}),
88
]
9-
}
9+
};
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const postcss = require('postcss')
1+
const postcss = require('postcss');
22
const fs = require('fs');
33

44
const findCSSVars = styleString => {
@@ -14,17 +14,12 @@ const findCSSVars = styleString => {
1414
module.exports = postcss.plugin('add fallback plugin', function (opts) {
1515
let params = new Map();
1616
opts = opts || {};
17-
if (!opts.importFrom) {
18-
console.log("importFrom option not specified, plugin will add no fallback parameters");
19-
} else {
20-
// Work with options here
21-
const sourceParams = fs.readFileSync(opts.importFrom).toString();
22-
params = findCSSVars(sourceParams);
23-
}
2417

2518
return function (root, result) {
26-
if (!opts.importFrom) {
27-
return;
19+
// If importFrom was given, parse all CSS variables from there
20+
if (opts.importFrom) {
21+
const sourceParams = fs.readFileSync(opts.importFrom).toString();
22+
params = findCSSVars(sourceParams);
2823
}
2924

3025
root.walkDecls(decl => {
@@ -36,13 +31,16 @@ module.exports = postcss.plugin('add fallback plugin', function (opts) {
3631
decl.value = decl.value.replace(varName, `${varName}, ${params.get(varName)}`)
3732
}
3833
}
34+
params.set(decl.prop, decl.value);
3935
});
4036

4137
// add the importFrom file as dependency so this file is processed again on changes
42-
result.messages.push({
43-
type: "dependency",
44-
file: opts.importFrom,
45-
parent: root.source.input.file,
46-
});
38+
if (opts.importFrom) {
39+
result.messages.push({
40+
type: "dependency",
41+
file: opts.importFrom,
42+
parent: root.source.input.file,
43+
});
44+
}
4745
}
48-
});
46+
});

0 commit comments

Comments
 (0)