-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
198 lines (178 loc) · 5.73 KB
/
index.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
let valueParser = require('postcss-value-parser')
const PREFERS_COLOR_ONLY = /^\(\s*prefers-color-scheme\s*:\s*(dark|light)\s*\)$/
const PREFERS_COLOR = /\(\s*prefers-color-scheme\s*:\s*(dark|light)\s*\)/g
function escapeRegExp(string) {
return string.replace(/[$()*+.?[\\\]^{|}-]/g, '\\$&')
}
function replaceAll(string, find, replace) {
return string.replace(new RegExp(escapeRegExp(find), 'g'), replace)
}
function addColorSchemeMedia(isDark, propValue, declaration, postcss) {
let mediaQuery = postcss.atRule({
name: 'media',
params: `(prefers-color-scheme:${isDark ? 'dark' : 'light'})`,
source: declaration.source
})
mediaQuery.append(
postcss.rule({
nodes: [
postcss.decl({
important: declaration.important,
prop: declaration.prop,
source: declaration.source,
value: propValue
})
],
selector: declaration.parent.selector,
source: declaration.source
})
)
declaration.parent.after(mediaQuery)
}
function extractLightDark(isDark, declarationValue) {
let parsed = valueParser(declarationValue)
mutateLightDarkRec(isDark, parsed)
return valueParser.stringify(parsed)
}
function mutateLightDarkRec(isDark, parsed) {
let wasMutated = false
parsed.walk(node => {
if (wasMutated || node.type !== 'function' || node.value !== 'light-dark') {
return
}
let light = node.nodes[0]
let dark = node.nodes.find((child, index) => {
return index > 0 && (child.type === 'word' || child.type === 'function')
})
Object.assign(node, isDark ? dark : light)
mutateLightDarkRec(isDark, parsed)
wasMutated = true
return false
})
}
module.exports = (opts = {}) => {
let dark = opts.darkSelector || '.is-dark'
let light = opts.lightSelector || '.is-light'
let roots = opts.rootSelector || ['html', ':root']
if (!Array.isArray(roots)) roots = [roots]
let useWhere = opts.useWhere ?? true
let removeMedia = opts.removeMedia ?? false
let uniqueRoots = roots
if (uniqueRoots.includes('html')) {
uniqueRoots = uniqueRoots.filter(i => i !== ':root')
}
function processSelectors(selectors, add) {
return selectors.map(selector => {
let changed = false
for (let root of roots) {
if (selector.includes(root)) {
changed = true
if (useWhere) {
selector = replaceAll(selector, root, `${root}:where(${add})`)
} else {
selector = replaceAll(selector, root, `${root}${add}`)
}
}
}
if (!changed) {
selector = uniqueRoots
.map(root => {
if (useWhere) {
return `:where(${root}${add}) ${selector}`
} else {
return `${root}${add} ${selector}`
}
})
.join(',')
}
return selector
})
}
function processNodes(parent, add) {
parent.each(node => {
if (node.type === 'atrule') {
processNodes(node, add)
} else if (node.type === 'rule') {
node.selectors = processSelectors(node.selectors, add)
}
})
}
return {
AtRuleExit: {
media(atrule) {
let params = atrule.params
if (!params.includes('dark') && !params.includes('light')) {
return
}
let fixedSelector = params.includes('dark') ? dark : light
let nodeSelector = `:not(${params.includes('dark') ? light : dark})`
if (PREFERS_COLOR_ONLY.test(params)) {
let last = atrule
atrule.each(node => {
let fixed
if (node.type === 'atrule') {
fixed = node.clone()
processNodes(fixed, fixedSelector)
if (!removeMedia) {
processNodes(node, nodeSelector)
}
} else if (node.type === 'rule') {
if (!node.selector.includes(nodeSelector)) {
fixed = node.clone({
selectors: processSelectors(node.selectors, fixedSelector)
})
if (!removeMedia) {
node.selectors = processSelectors(
node.selectors,
nodeSelector
)
}
}
} else if (node.type === 'comment') {
fixed = node.clone()
}
if (fixed) {
last.after(fixed)
last = fixed
}
})
if (removeMedia) {
atrule.remove()
}
} else if (PREFERS_COLOR.test(params) && params.includes(' and ')) {
if (atrule.params.includes(' and ')) {
let fixed = atrule.clone({
params: atrule.params
.replace(PREFERS_COLOR, '')
.replace(/\s+and\s+and/i, ' and')
.replace(/^\s*and\s+/i, '')
.replace(/\s+and\s*$/i, '')
})
atrule.after(fixed)
processNodes(fixed, fixedSelector)
if (!removeMedia) {
processNodes(atrule, nodeSelector)
} else {
atrule.remove()
}
}
}
}
},
DeclarationExit: (declaration, { postcss }) => {
if (!declaration.value.includes('light-dark')) return
let lightValue = extractLightDark(false, declaration.value)
if (lightValue === declaration.value) return
let darkValue = extractLightDark(true, declaration.value)
addColorSchemeMedia(false, lightValue, declaration, postcss)
addColorSchemeMedia(true, darkValue, declaration, postcss)
let parent = declaration.parent
declaration.remove()
if (parent.nodes.length === 0) {
parent.remove()
}
},
postcssPlugin: 'postcss-dark-theme-class'
}
}
module.exports.postcss = true