gulp-less不再生成CSS文件

时间:2016-06-01 09:27:08

标签: build npm gulp gulp-watch gulp-less

我正在使用gulp从LESS生成CSS。它工作得很好,但现在脚本似乎忽略了LESS文件。

这是我的gulpfile.js(这是绝对正确的,因为我上次没有改变它):

// Include Gulp plugins
var gulp = require('gulp'),
    less = require('gulp-less'),
    watch = require('gulp-watch'),
    prefix = require('gulp-autoprefixer'),
    plumber = require('gulp-plumber'),
    filter = require('gulp-filter'),
    rename = require('gulp-rename'),
    path = require('path')
;
// Compile LESS to CSS
gulp.task('build-less', function() {
    const fileFilter = filter(['*', '!mixins.less', '!variables.less']);
    gulp.src('./public/less/*.less') // path to less file
        .pipe(fileFilter)
        .pipe(plumber())
        .pipe(less())
        .pipe(gulp.dest('./public/css/')) // path to css directory
    ;
});
// Get vendors' code
gulp.task('build-vendors', function() {
    gulp.src(['./public/components/bootstrap/less/theme.less', './public/components/bootstrap/less/bootstrap.less']) // path to less file
        .pipe(plumber())
        .pipe(less())
        .pipe(rename(function (path) {
            //rename all files except 'bootstrap.css'
            if (path.basename + path.extname !== 'bootstrap.css') {
                path.basename = 'bootstrap-' + path.basename;
            }
        }))
        .pipe(gulp.dest('./public/css')) // path to css directory
    ;
});
// Run the build process
gulp.task('run', ['build-less', 'build-vendors']);
// Watch all LESS files, then run build-less
gulp.task('watch', function() {
    gulp.watch('public/less/*.less', ['run'])
});
// Default will run the 'entry' task
gulp.task('default', ['watch', 'run']);

这是电话和输出:

$ gulp
[11:21:03] Using gulpfile /var/www/path/to/project/gulpfile.js
[11:21:03] Starting 'watch'...
[11:21:03] Finished 'watch' after 21 ms
[11:21:03] Starting 'build-less'...
[11:21:03] Finished 'build-less' after 13 ms
[11:21:03] Starting 'build-vendors'...
[11:21:03] Finished 'build-vendors' after 4.65 ms
[11:21:03] Starting 'run'...
[11:21:03] Finished 'run' after 5.37 μs
[11:21:03] Starting 'default'...
[11:21:03] Finished 'default' after 6.05 μs

whatch也能正常工作 - 当我编辑LESS文件时,我得到一个这样的输出:

[11:22:22] Starting 'build-less'...
[11:22:22] Finished 'build-less' after 1.96 ms
[11:22:22] Starting 'build-vendors'...
[11:22:22] Finished 'build-vendors' after 1.78 ms
[11:22:22] Starting 'run'...
[11:22:22] Finished 'run' after 5.08 μs

我还尝试直接运行build-less

$ gulp build-less
[11:24:06] Using gulpfile /var/www/path/to/project/gulpfile.js
[11:24:06] Starting 'build-less'...
[11:24:06] Finished 'build-less' after 13 ms

没有错误,但CSS文件也没有变化。

这里可能出现什么问题以及如何解决?

1 个答案:

答案 0 :(得分:1)

gulp-filter使用multimatch将文件路径与globbing模式匹配。 multimatch has this to say about *的文档:

  

*匹配任意数量的字符,但不匹配/

由于您尝试使用单个星号public/less/foo.less/(包含*)等路径匹配文件,因此无法正常工作。您必须使用两个星号**

gulp.task('build-less', function() {
  const fileFilter = filter(['**/*', '!**/mixins.less', '!**/variables.less']);
  gulp.src('./public/less/*.less') // path to less file
    .pipe(fileFilter)
    .pipe(plumber())
    .pipe(less())
    .pipe(gulp.dest('./public/css/')); // path to css directory
});