Skip to content

Commit

Permalink
feat(core): remove Less and SCSS variables in favor of CSS variables
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Aug 3, 2021
1 parent 7969c69 commit 91e8ea1
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 116 deletions.
5 changes: 0 additions & 5 deletions scripts/build-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,4 @@ module.exports = {
'free-mode',
'slides-per-column',
],
themeColor: '#007aff',
colors: {
white: '#ffffff',
black: '#000000',
},
};
46 changes: 14 additions & 32 deletions scripts/build-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,20 @@ const minifyCSS = require('./utils/clean-css');
const banner = require('./banner')();
const config = require('./build-config');

const base64Encode = async (file) => {
// read binary data
const bitmap = await fs.readFile(file);
// convert binary data to base64 encoded string
return Buffer.from(bitmap).toString('base64');
};

const readSwiperFile = async (filePath, iconsFontBase64, colors) => {
const readSwiperFile = async (filePath) => {
const fileContent = await fs.readFile(filePath, 'utf-8');
if (filePath.includes('swiper.less')) {
const coreContent = fs.readFileSync(
path.resolve(__dirname, '../src/components/core/core.less'),
'utf-8',
);
return fileContent
.replace('swiperIconsFont()', `'${iconsFontBase64}'`)
.replace('$themeColor', config.themeColor)
.replace('$colors', colors.join(', '))
.replace('//IMPORT_COMPONENTS', '')
.replace(`@import url('./less/mixins.less');`, '')
.replace(`@import url('./components/core/core.less');`, coreContent);
}
if (filePath.includes('swiper-vars.less')) {
return fileContent
.replace('$themeColor', config.themeColor)
.replace('$colors', colors.join(', '));
return fileContent;
}
if (filePath.includes('navigation.less') || filePath.includes('pagination.less')) {
return ["@import url('../../swiper-vars.less');", fileContent].join('\n\n');
Expand All @@ -53,19 +41,16 @@ const readSwiperFile = async (filePath, iconsFontBase64, colors) => {
return fileContent;
};

const buildCSS = async ({ isBundle, colors, components, minified, outputDir }) => {
const buildCSS = async ({ isBundle, components, minified, outputDir }) => {
let lessContent = await fs.readFile(path.resolve(__dirname, '../src/swiper.less'), 'utf8');
lessContent = lessContent
.replace(
'//IMPORT_COMPONENTS',
!isBundle
? ''
: components
.map((component) => `@import url('./components/${component}/${component}.less');`)
.join('\n'),
)
.replace('$themeColor', config.themeColor)
.replace('$colors', colors.join(', '));
lessContent = lessContent.replace(
'//IMPORT_COMPONENTS',
!isBundle
? ''
: components
.map((component) => `@import url('./components/${component}/${component}.less');`)
.join('\n'),
);

const cssContent = await autoprefixer(
await less(lessContent, path.resolve(__dirname, '../src')),
Expand Down Expand Up @@ -94,18 +79,15 @@ module.exports = async (outputDir) => {
const lessFilePath = `./src/components/${name}/${name}.less`;
return fs.existsSync(lessFilePath);
});
const colors = Object.keys(config.colors).map((key) => `${key} ${config.colors[key]}`);

buildCSS({ isBundle: true, colors, components, minified: env !== 'development', outputDir });
buildCSS({ isBundle: false, colors, components, minified: env !== 'development', outputDir });
buildCSS({ isBundle: true, components, minified: env !== 'development', outputDir });
buildCSS({ isBundle: false, components, minified: env !== 'development', outputDir });

if (env === 'development') {
return;
}

// Copy less & scss
const iconsFontBase64 = await base64Encode('./src/icons/font/swiper-icons.woff');

const files = await globby(
[
'**/**.scss',
Expand All @@ -123,7 +105,7 @@ module.exports = async (outputDir) => {
files.map(async (file) => {
const distFilePath = path.resolve(__dirname, `../${outputDir}`, file);
const srcFilePath = path.resolve(__dirname, '../src', file);
const distFileContent = await readSwiperFile(srcFilePath, iconsFontBase64, colors);
const distFileContent = await readSwiperFile(srcFilePath);
await fs.ensureDir(path.dirname(distFilePath));
await fs.writeFile(distFilePath, distFileContent);
}),
Expand Down
20 changes: 0 additions & 20 deletions src/components/navigation/navigation.less
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
.navigation-color-loop(@ruleset) when (length( @colors ) >= 1) {
@length: length(@colors);
.looper( 1 );
.looper( @index ) when ( @index <= @length ) and (length(extract( @colors, @index )) = 2) {
@navPair: extract(@colors, @index);
@navColorName: extract(@navPair, 1);
@navColorValue: extract(@navPair, 2);
@ruleset();
.looper( @index + 1 );
}
}

:root {
--swiper-navigation-size: 44px;
/*
Expand Down Expand Up @@ -60,14 +48,6 @@
right: 10px;
left: auto;
}
.navigation-color-loop({
.swiper-button-prev,
.swiper-button-next {
&.swiper-button-@{navColorName} {
--swiper-navigation-color: @navColorValue;
}
}
});
.swiper-button-lock {
display: none;
}
8 changes: 0 additions & 8 deletions src/components/navigation/navigation.scss
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,6 @@
left: auto;
}

@each $navColorName, $navColorValue in $colors {
.swiper-button-prev,
.swiper-button-next {
&.swiper-button-#{'' + $navColorName} {
--swiper-navigation-color: #{'' + $navColorValue};
}
}
}
.swiper-button-lock {
display: none;
}
17 changes: 0 additions & 17 deletions src/components/pagination/pagination.less
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
.pagination-color-loop(@ruleset) when (length( @colors ) >= 1) {
@length: length(@colors);
.looper( 1 );
.looper( @index ) when ( @index <= @length ) and (length(extract( @colors, @index )) = 2) {
@paginationPair: extract(@colors, @index);
@paginationColorName: extract(@paginationPair, 1);
@paginationColorValue: extract(@paginationPair, 2);
@ruleset();
.looper( @index + 1 );
}
}

:root {
/*
--swiper-pagination-color: var(--swiper-theme-color);
Expand Down Expand Up @@ -157,11 +145,6 @@
top: 0;
}
}
.pagination-color-loop({
.swiper-pagination-@{paginationColorName} {
--swiper-pagination-color: @paginationColorValue;
}
});
.swiper-pagination-lock {
display: none;
}
5 changes: 0 additions & 5 deletions src/components/pagination/pagination.scss
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,6 @@
top: 0;
}
}
@each $paginationColorName, $paginationColorValue in $colors {
.swiper-pagination-#{'' + $paginationColorName} {
--swiper-pagination-color: #{'' + $paginationColorValue};
}
}
.swiper-pagination-lock {
display: none;
}
1 change: 0 additions & 1 deletion src/less/mixins.less

This file was deleted.

17 changes: 0 additions & 17 deletions src/less/plugin.js

This file was deleted.

3 changes: 1 addition & 2 deletions src/swiper-vars.less
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
@themeColor: $themeColor;
@colors: $colors;
@themeColor: #007aff;
4 changes: 0 additions & 4 deletions src/swiper-vars.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
$themeColor: #007aff !default;
$colors: (
'white': #ffffff,
'black': #000000,
) !default;
7 changes: 2 additions & 5 deletions src/swiper.less
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
@import url('./less/mixins.less');

@themeColor: $themeColor;
@colors: $colors;
@themeColor: #007aff;

@font-face {
@fontBase64: swiperIconsFont();
font-family: 'swiper-icons';
src: url('data:application/font-woff;charset=utf-8;base64, @{fontBase64}') format('woff');
src: url('data:application/font-woff;charset=utf-8;base64, d09GRgABAAAAAAZgABAAAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABGRlRNAAAGRAAAABoAAAAci6qHkUdERUYAAAWgAAAAIwAAACQAYABXR1BPUwAABhQAAAAuAAAANuAY7+xHU1VCAAAFxAAAAFAAAABm2fPczU9TLzIAAAHcAAAASgAAAGBP9V5RY21hcAAAAkQAAACIAAABYt6F0cBjdnQgAAACzAAAAAQAAAAEABEBRGdhc3AAAAWYAAAACAAAAAj//wADZ2x5ZgAAAywAAADMAAAD2MHtryVoZWFkAAABbAAAADAAAAA2E2+eoWhoZWEAAAGcAAAAHwAAACQC9gDzaG10eAAAAigAAAAZAAAArgJkABFsb2NhAAAC0AAAAFoAAABaFQAUGG1heHAAAAG8AAAAHwAAACAAcABAbmFtZQAAA/gAAAE5AAACXvFdBwlwb3N0AAAFNAAAAGIAAACE5s74hXjaY2BkYGAAYpf5Hu/j+W2+MnAzMYDAzaX6QjD6/4//Bxj5GA8AuRwMYGkAPywL13jaY2BkYGA88P8Agx4j+/8fQDYfA1AEBWgDAIB2BOoAeNpjYGRgYNBh4GdgYgABEMnIABJzYNADCQAACWgAsQB42mNgYfzCOIGBlYGB0YcxjYGBwR1Kf2WQZGhhYGBiYGVmgAFGBiQQkOaawtDAoMBQxXjg/wEGPc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kAAAMDZJA8Q7OUJvkLsPfZ6zFVERPy8qHh2YER+3i/BP83vIBLLySsoKimrqKqpa2hp6+jq6RsYGhmbmJqZSy0sraxtbO3sHRydnEMU4uR6yx7JJXveP7WrDycAAAAAAAH//wACeNpjYGRgYOABYhkgZgJCZgZNBkYGLQZtIJsFLMYAAAw3ALgAeNolizEKgDAQBCchRbC2sFER0YD6qVQiBCv/H9ezGI6Z5XBAw8CBK/m5iQQVauVbXLnOrMZv2oLdKFa8Pjuru2hJzGabmOSLzNMzvutpB3N42mNgZGBg4GKQYzBhYMxJLMlj4GBgAYow/P/PAJJhLM6sSoWKfWCAAwDAjgbRAAB42mNgYGBkAIIbCZo5IPrmUn0hGA0AO8EFTQAA');
font-weight: 400;
font-style: normal;
}
Expand Down

0 comments on commit 91e8ea1

Please sign in to comment.