uikit
Version:
UIkit is a lightweight and modular front-end framework for developing fast and powerful web interfaces.
317 lines (267 loc) • 12.7 kB
JavaScript
import NP from 'number-precision';
import { glob, read, write } from './util.js';
NP.enableBoundaryChecking(false);
const themeMixins = {};
const coreMixins = {};
const themeVar = {};
const coreVar = {};
/* template for the new components/mixins.scss file*/
const mixinTemplate = `//
// Component: Mixin
// Description: Defines mixins which are used across all components
//
// ========================================================================
// SVG
// ========================================================================
/// Replace \`$search\` with \`$replace\` in \`$string\`
/// @author Hugo Giraudel
/// @param {String} $string - Initial string
/// @param {String} $search - Substring to replace
/// @param {String} $replace ('') - New value
/// @return {String} - Updated string
str-replace($string, $search, $replace: '') {
$index: str-index($string, $search);
$index {
str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
}
$string;
}
svg-fill($src, $color-default, $color-new){
$replace-src: str-replace($src, $color-default, $color-new) !default;
$replace-src: str-replace($replace-src, "#", "%23");
background-image: url(quote($replace-src));
}`;
/* template for the inverse components */
const inverseTemplate = ` hook-inverse-component-base();
hook-inverse-component-link();
hook-inverse-component-heading();
hook-inverse-component-divider();
hook-inverse-component-list();
hook-inverse-component-icon();
hook-inverse-component-form();
hook-inverse-component-button();
hook-inverse-component-grid();
hook-inverse-component-close();
hook-inverse-component-totop();
hook-inverse-component-badge();
hook-inverse-component-label();
hook-inverse-component-article();
hook-inverse-component-search();
hook-inverse-component-nav();
hook-inverse-component-navbar();
hook-inverse-component-subnav();
hook-inverse-component-breadcrumb();
hook-inverse-component-pagination();
hook-inverse-component-tab();
hook-inverse-component-slidenav();
hook-inverse-component-dotnav();
hook-inverse-component-accordion();
hook-inverse-component-iconnav();
hook-inverse-component-text();
hook-inverse-component-column();
hook-inverse-component-utility();`;
/* First Step: Go through all files */
for (const file of await glob('src/less/**/*.less')) {
const data = await read(file);
/* replace all Less stuff with SCSS */
let scssData = data
.replace(/\/less\//g, '/scss/') // change less/ dir to scss/ on imports
.replace(/\.less/g, '.scss') // change .less extensions to .scss on imports
.replace(/@/g, '$') // convert variables
.replace(
/(:[^'"]*?\([^'"]+?)\s*\/\s*([0-9.-]+)\)/g,
(exp, m1, m2) => `${m1} * ${NP.round(1 / parseFloat(m2), 5)})`
)
.replace(/--uk-[^\s]+: (\$[^\s]+);/g, (exp, name) => exp.replace(name, `#{${name}}`))
.replace(/\\\$/g, '\\@') // revert classes using the @ symbol
.replace(/ e\(/g, ' unquote(') // convert escape function
.replace(/\.([\w-]*)\s*\((.*)\)\s*{/g, ' $1($2){') // hook -> mixins
.replace(/(\$[\w-]*)\s*:(.*);/g, '$1: $2 !default;') // make variables optional
.replace(/@mixin ([\w-]*)\s*\((.*)\)\s*{\s*}/g, '// @mixin $1($2){}') // comment empty mixins
.replace(/\.(hook[a-zA-Z\-\d]+)(\(\))?;/g, ' (mixin-exists($1)) { $1();}') // hook calls surrounded by a mixin-exists
.replace(/\$(import|supports|media|font-face|page|keyframes|-moz-document)/g, '@$1') // replace valid '@' statements
.replace(/tint\((\$[\w-]+),\s([^)]*)\)/g, 'mix(white, $1, $2)') // replace Less function tint with mix
.replace(/fade\((\$[\w-]*), ([0-9]+)%\)/g, (match, p1, p2) => {
return `rgba(${p1}, ${p2 / 100})`;
}) // replace Less function fade with rgba
.replace(/fade(in|out)\((\$[\w-]*), ([0-9]+)%\)/g, (match, p1, p2, p3) => {
return `fade-${p1}(${p2}, ${p3 / 100})`;
}) // replace Less function fadeout with fade-out
.replace(/\.svg-fill/g, ' svg-fill') // include svg-fill mixin
.replace(
/(.*):extend\((\.[\w-\\@]*) all\) when \((\$[\w-]*) = ([\w]+)\) {}/g,
' ( $3 == $4 ) { $1 { $2 !optional;} }'
) // update conditional extend and add !optional to ignore warnings
.replace(
/(\.[\w-\\@]+)\s*when\s*\((\$[\w-]*)\s*=\s*(\w+)\)\s*{\s*@if\(mixin-exists\(([\w-]*)\)\) {@include\s([\w-]*)\(\);\s*}\s*}/g,
' ($2 == $3) { $1 { (mixin-exists($4)) { $4();}}}'
) // update conditional hook
.replace(
/(\.[\w-\\@]+)\s*when\s*\((\$[\w-]*)\s*=\s*(\w+)\)\s*({\s*.*?\s*})/gs,
' ($2 == $3) {\n$1 $4\n}'
) // replace conditionals
.replace(/\${/g, '#{$') // string literals: from: /~"(.*)"/g, to: '#{"$1"}'
.replace(/[^(](-\$[\w-]*)/g, ' ($1)') // surround negative variables with brackets
.replace(/(--[\w-]+:\s*)~'([^']+)'/g, '$1$2') // string literals in custom properties
.replace(/~('[^']+')/g, 'unquote($1)'); // string literals: for real
/* File name of the current file */
const [filename] = file.split('/').pop().split('.less');
if (filename !== 'inverse') {
scssData = scssData.replace(/hook-inverse(?!-)/g, `hook-inverse-component-${filename}`);
} else {
const joinedHook = ` hook-inverse(){\n${inverseTemplate}\n}\n`;
scssData = scssData.replace(/\*\//, '*/\n' + joinedHook);
}
/* get all the mixins and remove them from the file */
scssData = getMixinsFromFile(file, scssData);
/* get all Variables but not from the mixin.less file */
if (filename !== 'mixin') {
scssData = await getVariablesFromFile(file, scssData);
}
if (filename === 'uikit.theme') {
/* remove the theme import first place */
scssData = scssData.replace(/\/\/\n\/\/ Theme\n\/\/\n\n@import "theme\/_import.scss";/, '');
/* add uikit-mixins and uikit-variables include to the uikit.scss file and change order, to load theme files first */
scssData = scssData.replace(
/\/\/ Core\n\/\//g,
'// Theme\n//\n\n@import "theme/_import.scss";'
);
}
/* mixin.less needs to be fully replaced by the new mixin file*/
if (filename === 'mixin') {
scssData = mixinTemplate;
}
await write(file.replace(/less/g, 'scss').replace('.theme.', '-theme.'), scssData);
}
/* Second Step write all new needed files for SASS */
/* write mixins into new file */
const mixins_theme = Object.keys(themeMixins).map(function (key) {
return themeMixins[key];
});
await write('src/scss/mixins-theme.scss', mixins_theme.join('\n'));
const mixins_core = Object.keys(coreMixins).map(function (key) {
return coreMixins[key];
});
await write('src/scss/mixins.scss', mixins_core.join('\n'));
/* write core variables */
const compactCoreVar = new Set();
Object.keys(coreVar).map((key) =>
getAllDependencies(coreVar, key).forEach((dependency) => compactCoreVar.add(dependency))
);
await write('src/scss/variables.scss', Array.from(compactCoreVar).join('\n'));
/* write theme variables */
const compactThemeVar = new Set();
Object.keys(themeVar).map((key) =>
getAllDependencies(themeVar, key).forEach((dependency) => compactThemeVar.add(dependency))
);
await write('src/scss/variables-theme.scss', Array.from(compactThemeVar).join('\n'));
/*
* recursive function to get a dependencie Set which is ordered so that no depencies exist to a later on entry
* @return Set with all the dependencies.
*/
function getAllDependencies(allVariables, currentKey, dependencies = new Set()) {
allVariables[currentKey].dependencies?.forEach((dependency) =>
getAllDependencies(allVariables, dependency, dependencies).forEach((newDependency) =>
dependencies.add(newDependency)
)
);
dependencies.add(`${currentKey}: ${allVariables[currentKey].value}`);
return Array.from(dependencies);
}
/*
* function to extract all the mixins from a given file with its data.
* @return an updated data where the mixins have been removed.
*/
function getMixinsFromFile(file, data) {
/* Step 1: get all includes and insert them, so that at least empty mixins exist. */
let regex = /@include ([a-z0-9-]+)/g;
let match = regex.exec(data);
while (match) {
if (!(match[1] in themeMixins)) {
themeMixins[match[1]] = ` ${match[1]}(){}`;
}
if (!(match[1] in coreMixins)) {
coreMixins[match[1]] = ` ${match[1]}(){}`;
}
match = regex.exec(data);
}
/* Step 2: get all multiline mixins */
regex = /@mixin ([\w-]*)\s*\((.*)\)\s*{\n(\s+[\w\W]+?)(?=\n})\n}/g;
match = regex.exec(data);
while (match) {
[themeMixins[match[1]]] = match;
if (!file.includes('theme/')) {
[coreMixins[match[1]]] = match;
}
match = regex.exec(data);
}
/* Step 3: get all singleline mixins */
regex = /@mixin ([\w-]*)\s*\((.*)\)\s*{( [^\n]+)}/g;
match = regex.exec(data);
while (match) {
[themeMixins[match[1]]] = match;
if (!file.includes('theme/')) {
[coreMixins[match[1]]] = match;
}
match = regex.exec(data);
}
/* Step 4: remove the mixins from the file, so that users can overwrite them in their custom code. */
return data
.replace(/@mixin ([\w-]*)\s*\((.*)\)\s*{\n(\s+[\w\W]+?)(?=\n})\n}/g, '')
.replace(/@mixin ([\w-]*)\s*\((.*)\)\s*{( [^\n]+)}/g, '');
}
/*
* function to extract all the variables from a given file with its data.
* @return an updated data where the icons have been replaced by the actual SVG data.
*/
async function getVariablesFromFile(file, data) {
const regex = /(\$[\w-]*)\s*:\s*(.*);/g;
let match = regex.exec(data);
while (match) {
/* check if variable is a background icon, if so replace it directly by the SVG */
if (match[0].includes('../../images/backgrounds')) {
const iconregex =
/(\$[\w-]+)\s*:\s*"\.\.\/\.\.\/images\/backgrounds\/([\w./-]+)" !default;/g;
const iconmatch = iconregex.exec(match[0]);
let svg = (await read(`src/images/backgrounds/${iconmatch[2]}`)).toString();
svg = `"${svg
.replace(/\r?\n|\r/g, '%0A')
.replace(/"/g, "'")
.replace(/\s/g, '%20')
.replace(/</g, '%3C')
.replace(/=/g, '%3D')
.replace(/'/g, '%22')
.replace(/:/g, '%3A')
.replace(/\//g, '%2F')
.replace(/>/g, '%3E')
.replace(/%3Csvg/, 'data:image/svg+xml;charset=UTF-8,%3Csvg')}"`;
/* add SVG to the coreVar and themeVar only if it is a theme file and make it optional */
if (!file.includes('theme/')) {
coreVar[iconmatch[1]] = { value: `${svg} !default;`, dependencies: [] };
}
themeVar[iconmatch[1]] = { value: `${svg} !default;`, dependencies: [] };
/* add SVG to the variable within the file itself as well */
const inlineSVG = `${iconmatch[1]}: ${svg} !default;`;
data = data.replace(match[0], inlineSVG);
/* when it is not an SVG add the variable and search for its dependencies */
} else {
const variablesRegex = /(\$[\w-]+)/g;
let variablesMatch = variablesRegex.exec(match[2]);
const dependencies = [];
while (variablesMatch) {
dependencies.push(variablesMatch[1]);
variablesMatch = variablesRegex.exec(match[2]);
}
/* add variables only to the core Variables if it is not a theme file */
if (!file.includes('theme/')) {
coreVar[match[1]] = {
value: `${match[2]};`,
dependencies: Array.from(dependencies),
};
}
themeVar[match[1]] = { value: `${match[2]};`, dependencies: Array.from(dependencies) };
}
match = regex.exec(data);
}
return data;
}