Gulp less not handling includes properly, included variables not defined

后端 未结 4 1175
时光取名叫无心
时光取名叫无心 2020-12-25 14:23

I am using less and Grunt, and am moving to gulp.

My less works. When I run:

lessc public/less/myapp.less

I get working output wit

4条回答
  •  滥情空心
    2020-12-25 14:45

    The difference was what I was compiling:

    • When I ran lessc myapp.less, I was compiling the main less file and it's dependencies
    • When I ran gulp using the gulpfile above, I was compiling each less file individually, because gulp.src was *.less not myapp.less. Since these less files are only ever loaded from the main less file, they didn't have @imports for the things they depend on (because myapp.less depends on them). Eg, there's no point importing, say, 'theme.less' in every individual file rather than just importing it first in myapp.less.

    Here's the working version:

    // Run 'gulp' to do the important stuff
    var gulp = require('gulp');
    var prefixer = require('gulp-autoprefixer');
    var less = require('gulp-less');
    var path = require('path');
    
    gulp.task('less', function () {
      gulp
        .src('./public/less/myapp.less') // This was the line that needed fixing
        .pipe(less({
          paths: ['public/less']
        }))
        .pipe(prefixer('last 2 versions', 'ie 9'))
        .pipe(gulp.dest('./public/css'));
    });
    
    // The default task (called when you run `gulp`)
    gulp.task('default', function() {
      gulp.run('less');
    
      // Watch files and run tasks if they change
      gulp.watch('./public/less/*.less', function(event) {
        gulp.run('less');
      });
    });
    

    Edit: see @noducks answer below for an improved version that works with the latest gulp.

提交回复
热议问题