-
Notifications
You must be signed in to change notification settings - Fork 262
/
Copy pathstyle-transform.js
162 lines (144 loc) · 3.53 KB
/
style-transform.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
const Stylis = require('stylis')
const stylis = new Stylis()
function disableNestingPlugin(...args) {
let [context, , , parent = [], line, column] = args
if (context === 2) {
parent = parent[0]
if (
typeof parent === 'string' &&
parent.trim().length > 0 &&
parent.charAt(0) !== '@'
) {
throw new Error(
`Nesting detected at ${line}:${column}. ` +
'Unfortunately nesting is not supported by styled-jsx.'
)
}
}
}
let generator
let filename
let offset
function sourceMapsPlugin(...args) {
const [context, , , , line, column, length] = args
// Pre-processed, init source map
if (context === -1 && generator !== undefined) {
generator.addMapping({
generated: {
line: 1,
column: 0
},
source: filename,
original: offset
})
return
}
// Post-processed
if (context === -2 && generator !== undefined) {
generator = undefined
offset = undefined
filename = undefined
return
}
// Selector/property, update source map
if ((context === 1 || context === 2) && generator !== undefined) {
generator.addMapping({
generated: {
line: 1,
column: length
},
source: filename,
original: {
line: line + offset.line,
column: column + offset.column
}
})
}
}
/**
* splitRulesPlugin
* Used to split a blob of css into an array of rules
* that can inserted via sheet.insertRule
*
* courtesy of and (c) the emotion folks (with some fixes from us)
* https://github.com/emotion-js/emotion/blob/994ea265cf5a411c5fa9b606dd140ce66776d1db/packages/emotion/src/index.js#L23-L60
*/
let isSplitRulesEnabled = false
let splitRules = []
let splitRulesQueue = []
const nestedAtRules = ['m', 's', 'd', 'k', '-']
function splitRulesPlugin(
context,
content,
selectors,
parent,
line,
column,
length,
id
) {
if (context === -2) {
if (
selectors.filter(Boolean).length === 0 &&
content.charAt(0) === '@' &&
nestedAtRules.indexOf(content.charAt(1)) === -1
) {
splitRulesQueue.push(content)
}
splitRules = splitRules.concat(splitRulesQueue)
splitRulesQueue = []
return
}
if (context === 2) {
if (id === 0) {
const joinedSelectors = selectors.join(',')
const rule = `${joinedSelectors}{${content}}`
if (parent.join(',') === joinedSelectors || parent[0] === '') {
splitRulesQueue.push(rule)
} else {
splitRulesQueue.unshift(rule)
}
}
return
}
// after an at rule block
if (context === 3) {
const selectrs = selectors.join(',')
if (nestedAtRules.indexOf(selectrs.charAt(1)) === -1) {
splitRulesQueue.push(`${selectrs}${content}`)
} else {
if (selectrs.charAt(1) === 'k') {
splitRulesQueue.push(`@-webkit-${selectrs.slice(1)}{${content}}`)
}
splitRulesQueue.push(`${selectrs}{${content}}`)
}
}
}
stylis.use(disableNestingPlugin)
stylis.use(sourceMapsPlugin)
stylis.use(splitRulesPlugin)
stylis.set({
cascade: false,
compress: true
})
/**
* Public transform function
*
* @param {String} prefix
* @param {String} styles
* @param {Object} settings
* @return {string}
*/
function transform(prefix, styles, settings = {}) {
generator = settings.generator
offset = settings.offset
filename = settings.filename
isSplitRulesEnabled = settings.splitRules
splitRules = []
const cssString = stylis(prefix, styles)
if (isSplitRulesEnabled) {
return splitRules
}
return cssString
}
module.exports = transform