Load variables into LESS CSS preprocessor from JSON file

后端 未结 4 1174
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-05 04:47

Is it possible to load variables into LESS CSS preprocessor from a JSON file like you can do with Stylus?

With contents of file myvars.json

         


        
4条回答
  •  终归单人心
    2021-02-05 05:31

    If you not use LESS in browser but compile the CSS, I just publish a gulp plugin to do this job.

    It can look like this :

    css/myvars.json

    {
        "color1": "#112345",
        "color2": "#667890"
    }
    

    less/styles.less

    @json-import "myvars.json";
    
    body {
      background-color: @color1;
    }
    

    gulpfile.js

    var gulp = require('gulp');
    var less = require('gulp-less');
    var lessJsonImport = require('gulp-less-json-import');
    
    gulp.task('less', function(){ 
      gulp.src(['./less/**/*.less', '!./less/**/_*.less'])
          .pipe(lessJsonImport())
          .pipe(less())
          .pipe(gulp.dest('./css'));
    });
    

提交回复
热议问题