0

我在使用 glob 和 gulp.dest() 时遇到问题。我的任务看起来像这样

return gulp.src('./clients/*/assets/less/*.less',{ base: process.cwd() })
    .pipe(less({
        paths:[path.join(__dirname, 'less')]
    }).on('error', function(err){
        gutil.log(err);
        this.emit('end');
    }))
    .pipe(autoprefixer({
        browsers: ['last 2 versions'],
        cascade: false
    }))
    .pipe(gulp.dest(function(file){
        return path.normalize(path.join(file.path, '../css'));
    }));

例如,如果我的路径是./clients/my-client/assets/less/style.less路径最终是 `./clients/my-client/assets/css/my-client/assets/less/css 我尝试使用重命名函数,例如

.pipe(rename(function (path) {
    path.dirname = '';
}))

之前但所做的只是将文件放在./clients/css/style.css

所需的目录应该是./clients/my-client/assets/less/style.css并且认为这一切都相当容易,也许它是,但我真的很挣扎。任何帮助将非常感激!

4

1 回答 1

0

我觉得这不是一个很好的解决方案,考虑到理论上它可能会出错,但它现在对我有用。

我以前只是简单地gulp-rename替换less.cssdirname

var src = './clients/*/assets/less/*.less';

return gulp.src(src,{base: './clients/'})
    .pipe(plumber())
    .pipe(sourcemaps.init())
    .pipe(less({
        paths:[path.join(__dirname, 'less')]
    }).on('error', function(err){
        gutil.log(err);
        this.emit('end');
    }))
    .pipe(cleanCSS({debug: true}, function(details) {
        console.log(details.name + ': ' + details.stats.originalSize);
        console.log(details.name + ': ' + details.stats.minifiedSize);
    }))
    .pipe(filesize())
    .pipe(rename(function (path) {
        path.dirname = path.dirname.replace(/less/i,'css');
    }))
    .pipe(gulp.dest('./clients/'))
    .pipe(notify({ message: "Style compressed", onLast: true}) );
于 2018-05-02T07:53:34.833 回答