/
apply-conditions.js
85 lines (68 loc) · 1.9 KB
/
apply-conditions.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
"use strict"
const base64EncodedConditionalImport = require("./base64-encoded-import")
module.exports = function applyConditions(bundle, atRule) {
bundle.forEach(stmt => {
if (
stmt.type === "charset" ||
stmt.type === "warning" ||
!stmt.conditions?.length
) {
return
}
if (stmt.type === "import") {
stmt.node.params = base64EncodedConditionalImport(
stmt.fullUri,
stmt.conditions,
)
return
}
const { nodes } = stmt
const { parent } = nodes[0]
const atRules = []
// Convert conditions to at-rules
for (const condition of stmt.conditions) {
if (typeof condition.media !== "undefined") {
const mediaNode = atRule({
name: "media",
params: condition.media,
source: parent.source,
})
atRules.push(mediaNode)
}
if (typeof condition.supports !== "undefined") {
const supportsNode = atRule({
name: "supports",
params: `(${condition.supports})`,
source: parent.source,
})
atRules.push(supportsNode)
}
if (typeof condition.layer !== "undefined") {
const layerNode = atRule({
name: "layer",
params: condition.layer,
source: parent.source,
})
atRules.push(layerNode)
}
}
// Add nodes to AST
const outerAtRule = atRules.shift()
const innerAtRule = atRules.reduce((previous, next) => {
previous.append(next)
return next
}, outerAtRule)
parent.insertBefore(nodes[0], outerAtRule)
// remove nodes
nodes.forEach(node => {
node.parent = undefined
})
// better output
nodes[0].raws.before = nodes[0].raws.before || "\n"
// wrap new rules with media query and/or layer at rule
innerAtRule.append(nodes)
stmt.type = "nodes"
stmt.nodes = [outerAtRule]
delete stmt.node
})
}