This repository was archived by the owner on Jan 18, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathcss.js
171 lines (142 loc) · 5.01 KB
/
css.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
import postcss from 'postcss'
import modules from 'postcss-modules'
import selectorParser from 'postcss-selector-parser'
import camelcase from 'camelcase'
// import MagicString from 'magic-string'
import genScopeID from '../gen-scope-id'
import debug from '../debug'
/**
* filter invalid tag, e.g. percentage, keyword(from, to)...
* @param tag
* @returns {boolean}
*/
function isInvalidTag (tag) {
if (
tag === 'from' ||
tag === 'to' ||
/^\d/.test(tag)
) {
return true
}
}
const addScopeID = postcss.plugin('add-scope-id', options => {
const selectorTransformer = selectorParser(selectors => {
selectors.each(selector => {
let target = null
/* eslint-disable complexity */
selector.each(n => {
if (n.type === 'combinator' && n.value === '>>>') {
n.value = ' '
n.spaces.before = n.spaces.after = ''
return false
}
if (n.type === 'tag') {
if (n.value === '/deep/') {
const next = n.next()
if (next.type === 'combinator' && next.value === ' ') {
next.remove()
}
n.remove()
return false
} else if (isInvalidTag(n.value)) {
return
}
}
if (n.type !== 'pseudo' && n.type !== 'combinator') {
target = n
}
})
/* eslint-enable complexity */
target && selector.insertAfter(target, selectorParser.attribute({
attribute: options.scopeID
}))
})
})
return root => {
root.walkRules(rule => {
rule.selector = selectorTransformer.process(rule.selector).result
})
}
})
function compileModule (code, map, source, options) {
let style
debug(`CSS Modules: ${source.id}`)
return postcss([
modules({
getJSON (filename, json) {
style = json
},
...options.cssModules
})
]).process(code, { map: { inline: false, prev: map }, from: source.id, to: source.id })
.then(
result => ({ code: result.css, map: result.map.toString(), module: style }),
error => {
throw error
}
)
}
function compileScopedCSS (code, map, source, options) {
debug(`Scoped CSS: ${source.id}`)
return postcss([
addScopeID({
scopeID: genScopeID(source.id)
})
]).process(code, { map: { inline: false, prev: map }, from: source.id, to: source.id })
.then(
result => ({ code: result.css, map: result.map.toString() }),
error => {
throw error
}
)
}
function escapeRegExp (str) {
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&')
}
export default async function (promise, options) {
const style = await promise
debug(`CSS: ${style.id}`)
const { code, map } = ('$compiled' in style) ? style.$compiled : style
if (style.module === true) {
return compileModule(code, map, style, options).then(compiled => {
if (style.$compiled) {
compiled.$prev = style.$compiled
const classes = Object.keys(compiled.module)
const cssModule = {}
if (classes.length) {
// Apply CSS modules to actual source.
// TODO: Update source map.
// const original = style.code
style.code = classes.reduce(
(result, original) => {
const transformed = compiled.module[original]
cssModule[camelcase(original)] = transformed
cssModule[original] = transformed
return result.replace(new RegExp(escapeRegExp(`.${original}`), 'g'), `.${transformed}`)
},
style.code
)
// style.map = (new MagicString(original))
compiled.module = (
typeof (style.module) === 'string' && style.attrs.module.length
) ? { [style.module]: cssModule } : cssModule
}
}
style.$compiled = compiled
return style
}).catch(error => debug(error))
}
if (style.scoped === true) {
return compileScopedCSS(code, map, style, options).then(compiled => {
if (style.$compiled) {
compiled.$prev = style.$compiled
}
style.$compiled = compiled
return style
})
}
const output = { code, map, lang: 'css' }
if (style.$compiled) output.$prev = style.$compiled
style.$compiled = output
return style
}