forked from Thream/styled-jsx-plugin-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (47 loc) · 1.72 KB
/
index.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
const sass = require('sass')
const stripIndent = require('strip-indent')
module.exports = (css, settings) => {
const cssWithPlaceholders = css
.replace(
/%%styled-jsx-placeholder-(\d+)%%%(\w*[ ),;!{])/g,
(_, id, p1) => `styled-jsx-percent-placeholder-${id}-${p1}`
)
.replace(
/%%styled-jsx-placeholder-(\d+)%%(\w*[ ),;!{])/g,
(_, id, p1) => `styled-jsx-placeholder-${id}-${p1}`
)
.replace(
/%%styled-jsx-placeholder-(\d+)%%%/g,
(_, id) => `/*%%styled-jsx-percent-placeholder-${id}%%*/`
)
.replace(
/%%styled-jsx-placeholder-(\d+)%%/g,
(_, id) => `/*%%styled-jsx-placeholder-${id}%%*/`
)
// Prepend option data to cssWithPlaceholders
const optionData = (settings.sassOptions && settings.sassOptions.data) || ''
// clean up extra indent (indentedSyntax is not compatible with extra indenting)
// they need to be cleaned up separately, and than concated
const data = stripIndent(optionData) + '\n' + stripIndent(cssWithPlaceholders)
const file = settings.babel && settings.babel.filename
const preprocessed = sass
.renderSync(Object.assign({}, { file }, settings.sassOptions, { data }))
.css.toString()
return preprocessed
.replace(
/styled-jsx-percent-placeholder-(\d+)-(\w*[ ),;!{])/g,
(_, id, p1) => `%%styled-jsx-placeholder-${id}%%%${p1}`
)
.replace(
/styled-jsx-placeholder-(\d+)-(\w*[ ),;!{])/g,
(_, id, p1) => `%%styled-jsx-placeholder-${id}%%${p1}`
)
.replace(
/\/\*%%styled-jsx-percent-placeholder-(\d+)%%\*\//g,
(_, id) => `%%styled-jsx-placeholder-${id}%%%`
)
.replace(
/\/\*%%styled-jsx-placeholder-(\d+)%%\*\//g,
(_, id) => `%%styled-jsx-placeholder-${id}%%`
)
}