1
- const postcss = require ( 'postcss' )
1
+ const postcss = require ( 'postcss' ) ;
2
2
const fs = require ( 'fs' ) ;
3
3
4
4
const findCSSVars = styleString => {
@@ -14,17 +14,12 @@ const findCSSVars = styleString => {
14
14
module . exports = postcss . plugin ( 'add fallback plugin' , function ( opts ) {
15
15
let params = new Map ( ) ;
16
16
opts = opts || { } ;
17
- if ( ! opts . importFrom ) {
18
- console . log ( "importFrom option not specified, plugin will add no fallback parameters" ) ;
19
- } else {
20
- // Work with options here
21
- const sourceParams = fs . readFileSync ( opts . importFrom ) . toString ( ) ;
22
- params = findCSSVars ( sourceParams ) ;
23
- }
24
17
25
18
return function ( root , result ) {
26
- if ( ! opts . importFrom ) {
27
- return ;
19
+ // If importFrom was given, parse all CSS variables from there
20
+ if ( opts . importFrom ) {
21
+ const sourceParams = fs . readFileSync ( opts . importFrom ) . toString ( ) ;
22
+ params = findCSSVars ( sourceParams ) ;
28
23
}
29
24
30
25
root . walkDecls ( decl => {
@@ -36,13 +31,16 @@ module.exports = postcss.plugin('add fallback plugin', function (opts) {
36
31
decl . value = decl . value . replace ( varName , `${ varName } , ${ params . get ( varName ) } ` )
37
32
}
38
33
}
34
+ params . set ( decl . prop , decl . value ) ;
39
35
} ) ;
40
36
41
37
// add the importFrom file as dependency so this file is processed again on changes
42
- result . messages . push ( {
43
- type : "dependency" ,
44
- file : opts . importFrom ,
45
- parent : root . source . input . file ,
46
- } ) ;
38
+ if ( opts . importFrom ) {
39
+ result . messages . push ( {
40
+ type : "dependency" ,
41
+ file : opts . importFrom ,
42
+ parent : root . source . input . file ,
43
+ } ) ;
44
+ }
47
45
}
48
- } ) ;
46
+ } ) ;
0 commit comments