Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make css vars fallback script work with embedded vars #251

Merged
merged 1 commit into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/main/config/postcss.bundles/postcss.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const postcssImport = require('postcss-import');
const postcssAddFallback = require('../../lib/postcss-add-fallback/index.js');

module.exports = {
plugins: [
postcssImport()
postcssImport(),
postcssAddFallback(),
]
}
};

2 changes: 1 addition & 1 deletion packages/main/config/postcss.components/postcss.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ module.exports = {
postcssNesting(),
postcssAddFallback({importFrom: "./dist/themes-next/sap_fiori_3/parameters-bundle.css"}),
]
}
};
30 changes: 14 additions & 16 deletions packages/main/lib/postcss-add-fallback/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const postcss = require('postcss')
const postcss = require('postcss');
const fs = require('fs');

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

return function (root, result) {
if (!opts.importFrom) {
return;
// If importFrom was given, parse all CSS variables from there
if (opts.importFrom) {
const sourceParams = fs.readFileSync(opts.importFrom).toString();
params = findCSSVars(sourceParams);
}

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

// add the importFrom file as dependency so this file is processed again on changes
result.messages.push({
type: "dependency",
file: opts.importFrom,
parent: root.source.input.file,
});
if (opts.importFrom) {
result.messages.push({
type: "dependency",
file: opts.importFrom,
parent: root.source.input.file,
});
}
}
});
});