This repository has been archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
felaExpandCssShorthandsPlugin.ts
56 lines (46 loc) · 1.81 KB
/
felaExpandCssShorthandsPlugin.ts
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
import * as _ from 'lodash'
import * as _expand from 'css-shorthand-expand'
import * as _memoize from 'fast-memoize'
// `fast-memoize` is a CJS library, there are known issues with them:
// https://github.com/rollup/rollup/issues/1267#issuecomment-446681320
const memoize = (_memoize as any).default || _memoize
// `css-shorthand-expand` is a CJS library, there are known issues with them:
// https://github.com/rollup/rollup/issues/1267#issuecomment-446681320
const expand = memoize((_expand as any).default || _expand)
// _.camelCase is quite fast, but we are running it for the same values many times
const camelCase = memoize(_.camelCase)
const handledCssPropsMap = {
font: 'font',
padding: 'padding',
margin: 'margin',
border: 'border',
borderWidth: 'border-width',
borderStyle: 'border-style',
borderColor: 'border-color',
borderTop: 'border-top',
borderRight: 'border-right',
borderBottom: 'border-bottom',
borderLeft: 'border-left',
borderRadius: 'border-radius',
background: 'background',
outline: 'outline',
}
export default () => {
const expandCssShorthands = (styles: Object) => {
return Object.keys(styles).reduce((acc, cssPropertyName) => {
const cssPropertyValue = styles[cssPropertyName]
if (typeof cssPropertyValue === 'object') {
return { ...acc, [cssPropertyName]: expandCssShorthands(cssPropertyValue) }
}
if (handledCssPropsMap[cssPropertyName]) {
const expandedProps = expand(handledCssPropsMap[cssPropertyName], `${cssPropertyValue}`)
if (expandedProps) {
return { ...acc, ...convertKeysToCamelCase(expandedProps) }
}
}
return { ...acc, [cssPropertyName]: cssPropertyValue }
}, {})
}
return expandCssShorthands
}
const convertKeysToCamelCase = obj => _.mapKeys(obj, (value, key) => camelCase(key))