build-plugin-fusion
Version:
plugin for build scripts while use fusion component
62 lines • 2.72 kB
JavaScript
var path = require('path');
var resolveSassImport = require('resolve-sass-import');
var getSassImplementation = require('@builder/app-helpers').getSassImplementation;
// regex for match sass variables like:
// $color-calculate-xxxx: transparentize($search-simple-dark-bg-color, 1 - $search-simple-dark-bg-opacity) !default;
var SASS_REGEX = /\$color-calculate[\w-]+?:[\s\S]+?;/g;
// regex for match css style like:
// .color-calculate-xxxx {color: rgba(0, 0, 0, 1);}
var CSS_REGEX = /\.color-calculate[\w\s-]+?\{[\s\S]+?\}/g;
// fix problem with importing absolute paths on windows.
function formatPathForWin(filepath) {
var isWin = process.platform === 'win32';
return isWin ? filepath.replace(/\\/g, '/') : filepath;
}
;
module.exports = function (varsPath, themePath, themeConfig) {
var variablesContent = '';
try {
variablesContent = resolveSassImport(varsPath, path.dirname(varsPath));
}
catch (err) {
console.log(err);
throw err;
}
if (variablesContent) {
// get all calculate colors by prefix color-calculate
var calcKeys_1 = [];
var calcSass = variablesContent.match(SASS_REGEX);
if (!calcSass)
return {};
// get calculate keys
calcSass.forEach(function (item) {
var key = item.split(':')[0];
calcKeys_1.push(key.slice(1).trim());
});
// create sass content
var sassContent = "@import '".concat(formatPathForWin(varsPath), "';\n@import '").concat(formatPathForWin(themePath), "';\n").concat(Object.keys(themeConfig).map(function (key) {
var value = themeConfig[key];
return "$".concat(key, ": ").concat(value, ";");
}).join('\n'), "\n").concat(calcSass.join('\n'), "\n").concat(calcKeys_1.map(function (key) {
return ".".concat(key, "{color: $").concat(key, ";}");
}).join('\n'));
var nodeSass = getSassImplementation();
// compile sass content to css
var cssContent = nodeSass.renderSync({
data: sassContent,
}).css.toString('utf8');
// get calculated css value
var calcVars_1 = {};
var calcCss = cssContent.match(CSS_REGEX);
if (calcCss) {
// parse `.color-calculate-mask-background{color: #000}` as `calcVars['calculate-color-mask-background'] = '#000'`
calcCss.forEach(function (item) {
var _a = item.split('{'), key = _a[0], value = _a[1];
calcVars_1[key.replace(/\.|\{/g, '').trim()] = value.replace(/;|\}/g, '').replace('color:', '').trim();
});
}
return calcVars_1;
}
return {};
};
//# sourceMappingURL=getCalcVars.js.map