-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (42 loc) · 1.35 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
52
53
const loopWhile = require('deasync').loopWhile
const postcss = require('postcss')
const postcssPresetEnv = require('postcss-preset-env')
const { addTheme, removeTheme } = require('./theme-processor')
module.exports = (css, options) => {
options = options || {}
const cssWithPlaceholders = css
.replace(/%%styled-jsx-placeholder-(\d+)%%/g, (_, id) =>
`/*%%styled-jsx-placeholder-${id}%%*/`
)
const cssToBeProcessed = addTheme(cssWithPlaceholders, options);
let processedCss
let wait = true
function resolved(result) {
processedCss = result
wait = false
}
const postcssPresetEnvOptions = options.postcssPresetEnvOptions || {};
postcss([
postcssPresetEnv(Object.assign({}, {
stage: 0,
features: {
'color-mod-function': { unresolved: 'warn' },
},
}, postcssPresetEnvOptions)),
]).process(cssToBeProcessed, { from: false })
.then(result => result.css)
.then(resolved)
.catch(resolved)
loopWhile(() => wait)
if (processedCss instanceof Error || processedCss.name === 'CssSyntaxError') {
throw processedCss
}
const result = removeTheme(processedCss)
.replace(/\/\*%%styled-jsx-placeholder-(\d+)%%\*\//g, (_, id) =>
`%%styled-jsx-placeholder-${id}%%`
);
if (options.enableLog) {
console.log('START', css, '-------------', result, 'END');
}
return result;
}