1- const postcss = require ( 'postcss' )
1+ const postcss = require ( 'postcss' ) ;
22const fs = require ( 'fs' ) ;
33
44const findCSSVars = styleString => {
@@ -14,17 +14,12 @@ const findCSSVars = styleString => {
1414module . exports = postcss . plugin ( 'add fallback plugin' , function ( opts ) {
1515 let params = new Map ( ) ;
1616 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- }
2417
2518 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 ) ;
2823 }
2924
3025 root . walkDecls ( decl => {
@@ -36,13 +31,16 @@ module.exports = postcss.plugin('add fallback plugin', function (opts) {
3631 decl . value = decl . value . replace ( varName , `${ varName } , ${ params . get ( varName ) } ` )
3732 }
3833 }
34+ params . set ( decl . prop , decl . value ) ;
3935 } ) ;
4036
4137 // 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+ }
4745 }
48- } ) ;
46+ } ) ;
0 commit comments