我有使用LESS文件的网站。我的gulpfile.js
看起来像这样:
var gulp = require("gulp");
var fs = require("fs");
var less = require("gulp-less");
var watchLess = require('gulp-watch-less2');
var embedTemplates = require('gulp-angular-embed-templates');
var concat = require('gulp-concat');
var rename = require("gulp-rename");
var csso = require('gulp-csso');
var gutil = require('gulp-util');
var terser = require('gulp-terser');
var filesExist = require('files-exist');
function buildLess(arr, outputDir, outputName) {
return gulp.src(filesExist(arr))
.pipe(less().on('error', gutil.log))
.pipe(rename(outputName + '.css'))
.pipe(gulp.dest(outputDir))
.on('error', gutil.log);
}
gulp.task('homepage', [], function () {
return buildLess([
"./css/homepage/reset.less",
"./node_modules/components-font-awesome/css/font-awesome.min.css",
"./node_modules/bootstrap/dist/css/bootstrap.css",
"./css/bootstrap-callouts.less",
"./css/homepage/global.less",
"./css/fullpage-footer.less",
"./node_modules/fullpage.js/dist/jquery.fullpage.min.css",
"./css/Header.less",
"./node_modules/angular-material/angular-material.css", // Only this exists on output file.
], './wwwroot/css/', 'homepage');
});
gulp.task('default', [
'homepage'
], function () { });
因此,所有那些css \ less文件应该转换为路径wwwroot/css/homepage.css
上的一个大css文件。检查完这个文件后,我发现输出文件中只有最后一个文件。
几点有趣:
filesExist(lst)
验证)。我的npm版本:
"devDependencies": {
"gulp": "3.9.1",
"gulp-less": "3.5.0",
"gulp-watch-less": "^1.0.1",
"gulp-watch-less2": "^2.0.4",
"gulp-angular-embed-templates": "^2.3.0",
"gulp-concat": "2.6.1",
"gulp-rename": "1.4.0",
"gulp-csso": "3.0.1",
"gulp-terser": "1.1.7",
"files-exist": "1.1.0"
}
我做错了什么?
呃......刚发现问题 - 忘记了Concat的输出。
这是正确的代码:
function buildLess(arr, outputDir, outputName) {
return gulp.src(filesExist(arr))
.pipe(less().on('error', gutil.log))
.pipe(concat(outputName + '.css'))
.pipe(gulp.dest(outputDir))
.on('error', gutil.log)
.pipe(config.production ? csso() : gutil.noop())
.pipe(config.production ? rename(outputName + '.min.css') : gutil.noop())
.pipe(config.production ? gulp.dest(outputDir) : gutil.noop());
}