|
| 1 | +/* eslint no-console: 0 */ |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'path'; |
| 4 | +import ts from 'typescript'; |
| 5 | +import { PATHS } from '../shared.config.js'; |
| 6 | + |
| 7 | +const inputSourceFilePath = path.resolve( |
| 8 | + import.meta.dirname, |
| 9 | + PATHS.JS_BREAKPOINTS.replace(PATHS.ROOT_DIR, '../'), |
| 10 | +); |
| 11 | + |
| 12 | +const outputSourceFilePath = path.resolve( |
| 13 | + import.meta.dirname, |
| 14 | + PATHS.CSS_CUSTOM_MEDIAS.replace(PATHS.ROOT_DIR, '../'), |
| 15 | +); |
| 16 | + |
| 17 | +/** |
| 18 | + * Возвращает медиа выражения необходимые по дизайн-системе. У ключей синтаксис должен быть как у CSS Custom Properties. |
| 19 | + * |
| 20 | + * > ❗️IMPORTANT❗️ |
| 21 | + * > При изменении функции следует вызвать команду `yarn workspace @vkontakte/vkui run generate:css-custom-medias`, |
| 22 | + * > для обновления CSS файла, и закоммитить изменения. |
| 23 | + * |
| 24 | + * @link https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-custom-media |
| 25 | + * @link https://github.com/Microsoft/TypeScript-wiki/blob/main/Using-the-Compiler-API.md#a-simple-transform-function |
| 26 | + * @link https://2ality.com/2019/10/eval-via-import.html#evaluating-simple-code-via-import() |
| 27 | + * |
| 28 | + * @returns {Promise<import('../src/lib/adaptivity').CSSCustomMedias>} |
| 29 | + */ |
| 30 | +export const getCustomMedias = async () => { |
| 31 | + const sourceTS = fs.readFileSync(inputSourceFilePath, 'utf-8').toString(); |
| 32 | + const { outputText: sourceJS } = ts.transpileModule(sourceTS, { |
| 33 | + compilerOptions: { module: ts.ModuleKind.ESNext }, |
| 34 | + }); |
| 35 | + const { BREAKPOINTS, MEDIA_QUERIES, widthPlus, widthMinus, heightMinus, heightPlus } = |
| 36 | + await import(`data:text/javascript;charset=utf-8,${encodeURIComponent(sourceJS)}`); |
| 37 | + |
| 38 | + return { |
| 39 | + '--sizeX-regular': widthPlus(BREAKPOINTS.SMALL_TABLET), |
| 40 | + '--sizeX-compact': widthMinus(BREAKPOINTS.SMALL_TABLET), |
| 41 | + |
| 42 | + '--sizeY-compact': `(pointer: fine) and ${widthPlus(BREAKPOINTS.SMALL_TABLET)}, ${heightMinus(BREAKPOINTS.MOBILE_LANDSCAPE_HEIGHT)}`, // prettier-ignore |
| 43 | + '--sizeY-regular': `(pointer: coarse) and ${heightPlus(BREAKPOINTS.MOBILE_LANDSCAPE_HEIGHT)}, (pointer: none) and ${heightPlus(BREAKPOINTS.MOBILE_LANDSCAPE_HEIGHT)}, ${widthMinus(BREAKPOINTS.SMALL_TABLET)} and ${heightPlus(BREAKPOINTS.MOBILE_LANDSCAPE_HEIGHT)}`, // prettier-ignore |
| 44 | + |
| 45 | + '--hover-has': '(hover: hover) and (pointer: fine)', // см. https://github.com/VKCOM/VKUI/issues/3469 |
| 46 | + '--hover-has-not': '(hover: none)', |
| 47 | + |
| 48 | + '--pointer-has': '(pointer: fine)', |
| 49 | + '--pointer-has-not': '(pointer: coarse), (pointer: none)', |
| 50 | + |
| 51 | + '--reduce-motion': 'screen and (prefers-reduced-motion: reduce)', |
| 52 | + |
| 53 | + '--desktop': `${widthPlus(BREAKPOINTS.SMALL_TABLET)} and (pointer: fine), ${widthPlus(BREAKPOINTS.SMALL_TABLET)} and ${heightPlus(BREAKPOINTS.MEDIUM_HEIGHT)}`, // prettier-ignore |
| 54 | + '--mobile': `${widthMinus(BREAKPOINTS.SMALL_TABLET)}, (pointer: none) and ${heightMinus(BREAKPOINTS.MEDIUM_HEIGHT)}, (pointer: coarse) and ${heightMinus(BREAKPOINTS.MEDIUM_HEIGHT)}`, // prettier-ignore |
| 55 | + |
| 56 | + '--viewWidth-desktopPlus': MEDIA_QUERIES.DESKTOP_PLUS, |
| 57 | + |
| 58 | + '--viewWidth-tabletPlus': widthPlus(BREAKPOINTS.TABLET), |
| 59 | + '--viewWidth-tablet': MEDIA_QUERIES.TABLET, |
| 60 | + '--viewWidth-tabletMinus': widthMinus(BREAKPOINTS.TABLET), |
| 61 | + |
| 62 | + '--viewWidth-smallTabletPlus': MEDIA_QUERIES.SMALL_TABLET_PLUS, |
| 63 | + '--viewWidth-smallTablet': MEDIA_QUERIES.SMALL_TABLET, |
| 64 | + '--viewWidth-smallTabletMinus': widthMinus(BREAKPOINTS.SMALL_TABLET), |
| 65 | + |
| 66 | + '--viewWidth-mobilePlus': widthPlus(BREAKPOINTS.MOBILE), |
| 67 | + '--viewWidth-mobile': MEDIA_QUERIES.MOBILE, |
| 68 | + |
| 69 | + '--viewWidth-smallMobileMinus': widthMinus(BREAKPOINTS.MOBILE), |
| 70 | + }; |
| 71 | +}; |
| 72 | + |
| 73 | +async function main() { |
| 74 | + console.log('🔄 Processing...'); |
| 75 | + |
| 76 | + const customMedias = await getCustomMedias(); |
| 77 | + const dataRaw = []; |
| 78 | + |
| 79 | + dataRaw.push('/* ⚠️ Документ сгенерирован автоматически */'); |
| 80 | + dataRaw.push('/* 📝 Если требуется изменения, то запустите команду `yarn workspace @vkontakte/vkui run generate:css-custom-medias` */'); // prettier-ignore |
| 81 | + dataRaw.push(''); |
| 82 | + dataRaw.push('/* stylelint-disable */'); |
| 83 | + dataRaw.push( |
| 84 | + Object.entries(customMedias) |
| 85 | + .map(([key, value]) => { |
| 86 | + return ['/* prettier-ignore */', `@custom-media ${key} ${value};`].join('\n'); |
| 87 | + }) |
| 88 | + .join('\n'), |
| 89 | + ); |
| 90 | + dataRaw.push(''); |
| 91 | + |
| 92 | + const data = dataRaw.join('\n'); |
| 93 | + |
| 94 | + fs.writeFileSync(outputSourceFilePath, data, 'utf-8'); |
| 95 | + |
| 96 | + console.log(`✅ ${outputSourceFilePath}`); |
| 97 | +} |
| 98 | + |
| 99 | +void main(); |
0 commit comments