Skip to content

Commit d378b04

Browse files
authored
Added a feature to preserve or not the original code (#34)
* Added a feature to preserve or not the original code * Optimization * Renamed the inverted the preserve param logic, into a onlySelectors param. * Renamed onlySelectors to removeMedia * Added test to the removeMedia feature
1 parent 66cbbb3 commit d378b04

3 files changed

Lines changed: 56 additions & 3 deletions

File tree

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ declare const darkClass: PluginCreator<{
55
lightSelector?: string
66
rootSelector?: string | string[]
77
useWhere?: boolean
8+
removeMedia?: boolean
89
}>
910

1011
export default darkClass

index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ module.exports = (opts = {}) => {
6767

6868
let useWhere = opts.useWhere ?? true
6969

70+
let removeMedia = opts.removeMedia ?? false
71+
7072
let uniqueRoots = roots
7173
if (uniqueRoots.includes('html')) {
7274
uniqueRoots = uniqueRoots.filter(i => i !== ':root')
@@ -128,13 +130,17 @@ module.exports = (opts = {}) => {
128130
if (node.type === 'atrule') {
129131
fixed = node.clone()
130132
processNodes(fixed, fixedSelector)
131-
processNodes(node, nodeSelector)
133+
if (!removeMedia) {
134+
processNodes(node, nodeSelector)
135+
}
132136
} else if (node.type === 'rule') {
133137
if (!node.selector.includes(nodeSelector)) {
134138
fixed = node.clone({
135139
selectors: processSelectors(node.selectors, fixedSelector)
136140
})
137-
node.selectors = processSelectors(node.selectors, nodeSelector)
141+
if (!removeMedia) {
142+
node.selectors = processSelectors(node.selectors, nodeSelector)
143+
}
138144
}
139145
} else if (node.type === 'comment') {
140146
fixed = node.clone()
@@ -144,6 +150,9 @@ module.exports = (opts = {}) => {
144150
last = fixed
145151
}
146152
})
153+
if (removeMedia) {
154+
atrule.remove()
155+
}
147156
} else if (PREFERS_COLOR.test(params) && params.includes(' and ')) {
148157
if (atrule.params.includes(' and ')) {
149158
let fixed = atrule.clone({
@@ -155,7 +164,11 @@ module.exports = (opts = {}) => {
155164
})
156165
atrule.after(fixed)
157166
processNodes(fixed, fixedSelector)
158-
processNodes(atrule, nodeSelector)
167+
if (!removeMedia) {
168+
processNodes(atrule, nodeSelector)
169+
} else {
170+
atrule.remove()
171+
}
159172
}
160173
}
161174
}

index.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,3 +725,42 @@ test('transforms complex nested light-dark()', () => {
725725
}`
726726
)
727727
})
728+
729+
test('removes media using transforms complex nested light-dark()', () => {
730+
run(
731+
`.light-dark-function-mix-a {
732+
color: light-dark(color-mix(in oklch, red, light-dark(cyan, rgb(0, 0, 0))), blue);
733+
}`,
734+
`:where(html.is-dark) .light-dark-function-mix-a {
735+
color: blue
736+
}
737+
:where(html.is-light) .light-dark-function-mix-a {
738+
color: color-mix(in oklch, red, cyan)
739+
}`,
740+
{ removeMedia: true }
741+
)
742+
})
743+
744+
test('removes media with combined queries in the middle - dark scheme', () => {
745+
run(
746+
`@media (width > 0) and (prefers-color-scheme: dark) and (width > 0) {
747+
a { color: white }
748+
}`,
749+
`@media (width > 0) and (width > 0) {
750+
:where(html.is-dark) a { color: white }
751+
}`,
752+
{ removeMedia: true }
753+
)
754+
})
755+
756+
test('removes media with combined queries in the middle - light scheme', () => {
757+
run(
758+
`@media (width > 0) and (prefers-color-scheme: light) and (width > 0) {
759+
a { color: white }
760+
}`,
761+
`@media (width > 0) and (width > 0) {
762+
:where(html.is-light) a { color: white }
763+
}`,
764+
{ removeMedia: true }
765+
)
766+
})

0 commit comments

Comments
 (0)