-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.ts
131 lines (90 loc) · 4.22 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { FileType } from '../enum'
import { customReplace, handleCharacters } from '../utilities'
import { Options } from '../interfaces'
import { intrinsicComponents } from '../utilities'
export function transformSelector(options: Options) {
const processed = Symbol('processed')
const defaultSpaceBetweenItems = [ 'view', 'button', 'text', 'image' ]
const usersSpaceBetweenItems = options?.utilitiesSettings?.spaceBetweenItems || []
const spaceBetweenItems = Array.from(new Set([ ...defaultSpaceBetweenItems, ...usersSpaceBetweenItems ]))
const userDivideBetweenItems = options?.utilitiesSettings?.divideItems || []
const divideItems = Array.from(new Set([ ...defaultSpaceBetweenItems, ...userDivideBetweenItems ]))
const customComponents = options?.utilitiesSettings?.customComponents || []
const allComponents = Array.from(new Set([ ...intrinsicComponents, ...customComponents ]))
const customReplacement = new Map()
/**
* A polyfill that is compatible 'space-[x,y]-\d', 'divide-[x,y]-\d' syntax
* Note that in mini program environment ':not()' selector can only be used when it's combined with other selectors
* e.g. view:not() works but the standalone :not() selector couldn't work
*/
customReplacement.set(/^(\.-?space-\w)(-.+?)\s?>.*/, spaceBetweenItems.map(item => `$1$2:not($1-reverse)>${ item }:not([hidden]):not(:first-child), $1$2$1-reverse>${ item }:not([hidden]):not(:last-child)`).join(', '))
customReplacement.set(/^(\.-?space-\w-reverse).*/, spaceBetweenItems.map(item => `$1>${ item }:not([hidden])`).join(', '))
customReplacement.set(/^(\.-?divide-\w+)(-.+?)?\s?>.*/, divideItems.map(item => `$1$2:not($1-reverse)>${ item }:not([hidden]):not(:first-child), $1$2$1-reverse>${ item }:not([hidden]):not(:last-child)`).join(', '))
customReplacement.set(/^(\.-?divide-\w-reverse).*/, divideItems.map(item => `$1>${ item }:not([hidden])`).join(', '))
customReplacement.set(/(\*$|\*(?=,))/, allComponents.join(', '))
return {
postcssPlugin: 'transformSelectorName',
Rule(node) {
if (!node[ processed ]) {
node.selector = handleCharacters(node.selector, FileType.Style)
node.selector = customReplace(node.selector, customReplacement)
node[ processed ] = true
}
},
}
}
enum SourceUnit {
REM = 'rem',
PX = 'px',
}
enum TargerUnit {
RPX = 'rpx',
}
export function transformValue(options: Options) {
const processed = Symbol('processed')
return {
postcssPlugin: 'transformValue',
Declaration(node) {
if (!node[ processed ]) {
// Don't transform anything inside url() values
if (node.value.includes('url')) { return }
const remValues = findValues(node.value, SourceUnit.REM)
const pxValues = findValues(node.value, SourceUnit.PX)
if (remValues?.length) {
node.value = transformAllValue(node.value, remValues, SourceUnit.REM, options)
}
if (pxValues?.length) {
node.value = transformAllValue(node.value, pxValues, SourceUnit.PX, options)
}
node[ processed ] = true
}
},
}
}
const valueConvertor = {
rem: remToRpx,
px: pxToRpx,
}
function transformAllValue(raw: string, targets: number[], unit: SourceUnit, options: Options) {
for (const value of targets) {
/**
* Handle cases like '.5rem' '-.5rem' etc
*/
const pattern = new RegExp((value + unit).replace(/^(-?)0(\.)/, '$10?$2').replace(/\./, '\\.'))
raw = raw.replace(pattern, valueConvertor[ unit ](value, options.designWidth) + TargerUnit.RPX)
}
return raw
}
function findValues(raw, unit: SourceUnit): null | number[] {
const regExp = new RegExp(`[\\d./]+(?=${ unit })`, 'g')
return raw.match(regExp)?.map(value => Number(value))
}
function remToRpx(rem: number, base = 375) {
return pxToRpx(rem * 16, base)
}
function pxToRpx(px: number, base = 375) {
const rpx = (px / (base / 375)) * 2
return Number(rpx.toFixed(3))
}
transformSelector.postcss = true as const
transformValue.postcss = true as const