提取scss變量,交給js

gulp.task('variables.js', function() {
  /**
   * 解析scss變量,注入到 variables.js 中
   * 具體解析哪些變量,在 variables.js 中定義
   */
  var t = fs.readFileSync('./node_modules/@alife/dpl-lst-scm/variables.scss', 'utf-8');
  var {variables = [] } = symbolsParser.parseSymbols(t);
  var variablesFile = require('./src/variables.js');
  result = Object.keys(variablesFile).reduce((buf, key)=>{
      let value = '';
      let times = 0;
      var vari = variables.find(v => v.name == key);
      if(vari) value = vari.value;
      // 若是值是變量,那麼遞歸查找最終的值
      while(value.indexOf('$') > -1){
        times++;
        value = value.split(' ')[0]; //去除 xx !import的干擾
        var vari = variables.find(v => v.name == value);
        if(vari) value = vari.value;
        if(times == 10){
          value = '';
          times = 0;
        }; // 10 次找不到我就不找了。
      }

      buf[key] = value;
      return buf;
    }, {});
    let content = 'define("ascm-comp/lib-resale-theme/variables",function(require,exports,module){module.exports='+JSON.stringify(result)+'});'
    fs.writeFileSync('./build/variables.js', content);
});
相關文章
相關標籤/搜索