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

ffe-core: follow less-imports when building css tokens #2436

Closed
wants to merge 1 commit into from
Closed
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
33 changes: 27 additions & 6 deletions packages/ffe-core/scripts/lib/renderLessVarsToCSSProps.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
const fs = require('fs').promises;
const less = require('less');

const readLessRulesFromFile = async inputFile => {
const data = await fs.readFile(inputFile, 'utf8');
const { rules } = await less.parse(data, { filename: inputFile });

return rules;
};

const getVariableNames = rules =>
rules.filter(r => r.variable === true).map(r => r.name.slice(1));

const applyCallbackOnImports = (rules, cb) =>
rules.filter(r => r.importedFilename).map(r => cb(r.importedFilename));

/**
* Read less variables recursively from a given file. This function follows import-statements.
*/
const readPropertyNames = async inputFile => {
const rules = await readLessRulesFromFile(inputFile);

return getVariableNames(rules).concat(
(
await Promise.all(applyCallbackOnImports(rules, readPropertyNames))
).flat(),
);
};

/**
* Takes less variables from a given file and generates a css stylesheet with
* these variables as custom properties on the :root pseudo-class.
*/
module.exports = async inputFile => {
const data = await fs.readFile(inputFile, 'utf8');
const root = await less.parse(data, { filename: inputFile });

const propertyNames = root.rules
.filter(r => r.variable)
.map(n => n.name.slice(1));
const propertyNames = await readPropertyNames(inputFile);

const { css } = await less.render(
`@import '${inputFile}';` +
Expand Down
Loading