1 | "use strict"
|
---|
2 |
|
---|
3 | // external tooling
|
---|
4 | const valueParser = require("postcss-value-parser")
|
---|
5 |
|
---|
6 | // extended tooling
|
---|
7 | const { stringify } = valueParser
|
---|
8 |
|
---|
9 | function split(params, start) {
|
---|
10 | const list = []
|
---|
11 | const last = params.reduce((item, node, index) => {
|
---|
12 | if (index < start) return ""
|
---|
13 | if (node.type === "div" && node.value === ",") {
|
---|
14 | list.push(item)
|
---|
15 | return ""
|
---|
16 | }
|
---|
17 | return item + stringify(node)
|
---|
18 | }, "")
|
---|
19 | list.push(last)
|
---|
20 | return list
|
---|
21 | }
|
---|
22 |
|
---|
23 | module.exports = function (result, styles) {
|
---|
24 | const statements = []
|
---|
25 | let nodes = []
|
---|
26 |
|
---|
27 | styles.each(node => {
|
---|
28 | let stmt
|
---|
29 | if (node.type === "atrule") {
|
---|
30 | if (node.name === "import") stmt = parseImport(result, node)
|
---|
31 | else if (node.name === "media") stmt = parseMedia(result, node)
|
---|
32 | else if (node.name === "charset") stmt = parseCharset(result, node)
|
---|
33 | }
|
---|
34 |
|
---|
35 | if (stmt) {
|
---|
36 | if (nodes.length) {
|
---|
37 | statements.push({
|
---|
38 | type: "nodes",
|
---|
39 | nodes,
|
---|
40 | media: [],
|
---|
41 | })
|
---|
42 | nodes = []
|
---|
43 | }
|
---|
44 | statements.push(stmt)
|
---|
45 | } else nodes.push(node)
|
---|
46 | })
|
---|
47 |
|
---|
48 | if (nodes.length) {
|
---|
49 | statements.push({
|
---|
50 | type: "nodes",
|
---|
51 | nodes,
|
---|
52 | media: [],
|
---|
53 | })
|
---|
54 | }
|
---|
55 |
|
---|
56 | return statements
|
---|
57 | }
|
---|
58 |
|
---|
59 | function parseMedia(result, atRule) {
|
---|
60 | const params = valueParser(atRule.params).nodes
|
---|
61 | return {
|
---|
62 | type: "media",
|
---|
63 | node: atRule,
|
---|
64 | media: split(params, 0),
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | function parseCharset(result, atRule) {
|
---|
69 | if (atRule.prev()) {
|
---|
70 | return result.warn("@charset must precede all other statements", {
|
---|
71 | node: atRule,
|
---|
72 | })
|
---|
73 | }
|
---|
74 | return {
|
---|
75 | type: "charset",
|
---|
76 | node: atRule,
|
---|
77 | media: [],
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | function parseImport(result, atRule) {
|
---|
82 | let prev = atRule.prev()
|
---|
83 | if (prev) {
|
---|
84 | do {
|
---|
85 | if (
|
---|
86 | prev.type !== "comment" &&
|
---|
87 | (prev.type !== "atrule" ||
|
---|
88 | (prev.name !== "import" && prev.name !== "charset"))
|
---|
89 | ) {
|
---|
90 | return result.warn(
|
---|
91 | "@import must precede all other statements (besides @charset)",
|
---|
92 | { node: atRule }
|
---|
93 | )
|
---|
94 | }
|
---|
95 | prev = prev.prev()
|
---|
96 | } while (prev)
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (atRule.nodes) {
|
---|
100 | return result.warn(
|
---|
101 | "It looks like you didn't end your @import statement correctly. " +
|
---|
102 | "Child nodes are attached to it.",
|
---|
103 | { node: atRule }
|
---|
104 | )
|
---|
105 | }
|
---|
106 |
|
---|
107 | const params = valueParser(atRule.params).nodes
|
---|
108 | const stmt = {
|
---|
109 | type: "import",
|
---|
110 | node: atRule,
|
---|
111 | media: [],
|
---|
112 | }
|
---|
113 |
|
---|
114 | // prettier-ignore
|
---|
115 | if (
|
---|
116 | !params.length ||
|
---|
117 | (
|
---|
118 | params[0].type !== "string" ||
|
---|
119 | !params[0].value
|
---|
120 | ) &&
|
---|
121 | (
|
---|
122 | params[0].type !== "function" ||
|
---|
123 | params[0].value !== "url" ||
|
---|
124 | !params[0].nodes.length ||
|
---|
125 | !params[0].nodes[0].value
|
---|
126 | )
|
---|
127 | ) {
|
---|
128 | return result.warn(`Unable to find uri in '${ atRule.toString() }'`, {
|
---|
129 | node: atRule,
|
---|
130 | })
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (params[0].type === "string") stmt.uri = params[0].value
|
---|
134 | else stmt.uri = params[0].nodes[0].value
|
---|
135 | stmt.fullUri = stringify(params[0])
|
---|
136 |
|
---|
137 | if (params.length > 2) {
|
---|
138 | if (params[1].type !== "space") {
|
---|
139 | return result.warn("Invalid import media statement", { node: atRule })
|
---|
140 | }
|
---|
141 | stmt.media = split(params, 2)
|
---|
142 | }
|
---|
143 |
|
---|
144 | return stmt
|
---|
145 | }
|
---|