1- import postcssJs from 'postcss-js'
21import compose from 'lodash/fp/compose'
3- import get from 'lodash/fp/get'
42import cond from 'lodash/fp/cond'
5- import lowerFirst from 'lodash/lowerFirst'
63import postcss from 'postcss'
74import cssnext from 'postcss-cssnext'
85import atImport from 'postcss-import'
96
107const isDev = ( ) => process . env . NODE_ENV === 'development'
118const isProd = ( ) => ! isDev ( )
129
13- const transform = ( data ) => {
14- if ( ! data ) {
15- return { }
16- }
17- return Object
18- . keys ( data )
19- . reduce ( ( acc , k ) => {
20- const v = data [ k ]
21-
22- k = k . replace ( / ^ - + / , '' ) // strips '-'
23- k = lowerFirst ( k )
24- acc [ k ] = v . endsWith ( 'px' ) ? parseInt ( v , 10 ) : v
25- return acc
26- } , { } )
27- }
28-
2910const toProdExport = ( code ) => `export default ${ code } `
3011const toDevExport = ( code ) => `let config = ${ code } ;
31- if (typeof global. Proxy !== 'undefined') {
12+ if (typeof Proxy !== 'undefined') {
3213 config = new Proxy(config, {
3314 get(target, key) {
3415 if (key !== '__esModule' && !target[key]) {
@@ -52,21 +33,29 @@ const toExport = cond([
5233] )
5334
5435const toString = ( data ) => `${ JSON . stringify ( data , null , '\t' ) } `
55- const toData = compose ( transform , get ( ':root' ) )
5636
57- export const toConfig = compose ( toExport , toString , toData )
58- export const toES5Config = compose ( toES5Export , toString , toData )
37+ const objectify = ( root , filepath ) => {
38+ const result = { }
39+
40+ if ( ! root ) {
41+ return result
42+ }
43+
44+ root . walkDecls ( ( rule ) => {
45+ if ( rule . source . input . file !== filepath ) {
46+ return
47+ }
48+ if ( rule . parent && rule . parent . selectors . find ( ( sel ) => sel === ':root' ) ) {
49+ const v = rule . value // replace "--"
5950
60- export const objectify = ( root , filepath ) => {
61- // removes imported rules, so we have only computed output
62- root . walkRules ( ( r ) => {
63- if ( r . source . input . file !== filepath ) {
64- r . remove ( )
51+ result [ rule . prop . replace ( / ^ - + / , '' ) ] = v . endsWith ( 'px' ) ? parseInt ( v , 10 ) : v
6552 }
6653 } )
67- return postcssJs . objectify ( root )
54+ return result
6855}
6956
57+ export const toConfig = compose ( toExport , toString , objectify )
58+ export const toES5Config = compose ( toES5Export , toString , objectify )
7059export const getPostcss = ( async ) => postcss ( )
7160 . use ( atImport ( { async } ) )
7261 . use ( cssnext ( { features : { customProperties : { preserve : 'computed' } } } ) )
0 commit comments