-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (40 loc) · 1.29 KB
/
index.js
File metadata and controls
52 lines (40 loc) · 1.29 KB
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
/**
* @type {import('postcss').PluginCreator}
*/
module.exports = (options = {}) => {
const { separator, cb } = Object.assign({}, {
separator: '/',
cb: (styleClasses) => {}
}, options)
let styleClasses = []
return {
postcssPlugin: 'postcss-perspective-style-class',
Rule (rule, { postcss, result }) {
if (!rule.selector.includes('[psc=')) { return }
// Match entire attributes.
const newSelector = rule.selector.replace(/(?=\[psc=).*?]/gm, (match) => {
// Determine attribute contents, and replace entire attribute.
/* eslint-disable-next-line */
const classPath = match.match(/(?<=\[psc=['"])(.*?)(?=['"]])/gm)[0]
if (!classPath) {
rule.warn(result, 'empty Style Class path')
return
}
if (classPath.slice(-separator.length) === separator) {
rule.warn(result, 'trailing separator found')
return
}
styleClasses.push(classPath)
return `.psc-${classPath.replaceAll(separator, '\\/')}`
})
const replacement = rule.clone({})
replacement.selector = newSelector
rule.replaceWith(replacement)
},
OnceExit () {
styleClasses = Array.from(new Set(styleClasses))
cb(styleClasses)
}
}
}
module.exports.postcss = true