UNPKG

build-plugin-fusion

Version:

plugin for build scripts while use fusion component

52 lines 2.21 kB
/* eslint no-useless-escape:0 */ var fs = require('fs'); var colorNames = require('./colorNames'); module.exports = function (themeFile, themeConfig, enableColorNames) { var themeVars = {}; try { var themeStr = fs.readFileSync(themeFile, 'utf8'); var themeArr = themeStr.match(/\$[\w\-]+?:.+?;/g); themeArr.forEach(function (item) { var _a = item.split(':'), key = _a[0], value = _a[1]; themeVars[key] = value.replace(';', '').trim(); }); } catch (e) { console.log(e); throw (e); } // make a copy var originTheme = {}; // 过滤颜色相关内容 Object.keys(themeVars).forEach(function (themeKey) { originTheme[themeKey.slice(1)] = themeVars[themeKey]; var themeValue = themeVars[themeKey]; var isNotColorValue = themeValue && themeValue.indexOf('#') !== 0 && !themeValue.match(/^rgb/); var isInBlackList = ['$color-white', '$color-black'].indexOf(themeKey) > -1; if (themeValue && (isNotColorValue || isInBlackList)) { // delete theme key if it is not a color variable var removeThemeVar = enableColorNames ? !themeValue.match(/^transparent/) && !colorNames[themeValue] : true; if (removeThemeVar) { delete themeVars[themeKey]; } } }); var scssVars = {}; var cssVars = {}; // $color-brand1-1: #000 -> scssVar: {color-brand1-1: var(--color-brand1-1)} cssVar: {--color-brand1-1: #000} Object.keys(themeVars).forEach(function (themeKey) { scssVars[themeKey.slice(1)] = "var(--".concat(themeKey.slice(1), ")"); cssVars[themeKey.replace('$', '--')] = themeVars[themeKey]; }); // custom-theme-config: #000 -> scssVar: {custom-theme-config: var(--color-brand1-1)} cssVar: {--custom-theme-config: #000} Object.keys(themeConfig).forEach(function (themeKey) { scssVars[themeKey] = "var(--".concat(themeKey, ")"); cssVars["--".concat(themeKey)] = themeConfig[themeKey]; }); return { originTheme: originTheme, scssVars: scssVars, cssVars: cssVars, }; }; //# sourceMappingURL=getThemeVars.js.map