如何使用 gulp 编译检测 LESS 中的根文件

How to detect root files in LESS using gulp compilation

事情是这样的。我的 LESS 文件结构如下:

/less/root.less
/less/includes1/*.less (a lot of less files)
/less/includes2/*.less (a lot of less files)

我正在使用 gulp 来编译我的 LESS 文件。来源:

gulp.task('less', function () {
    var combined = combiner.obj([
    gulp.src('./less/**/*.less'),
    less(),
    gulp.dest('./www/dist/screen.css')
]);

// any errors in the above streams will get caught by this listener, instead of being thrown:
combined.on('error', console.error.bind(console));

return combined;
});

问题出在指定的路径上。我这样理解这条路径 ./less/**/*.less:递归遍历所有文件夹并编译所有以 .less 结尾的文件。问题是编译器想要一个一个地编译每个文件并且不知道所有文件都包含在我的 source.less 中。它不查找根文件,也不知道包含哪些文件以及哪些是根文件。

如何告诉 LESS 解析器哪些文件是根文件以及哪些文件要导入。在 SASS 中,它通过文件名中的下划线指定,如下所示:_includedPart.scss.

你可以使用 https://github.com/robrich/gulp-ignore. See: How can I use a glob to ignore files that start with an underscore?

  var condition = '_*.less'; //exclude condition
  gulp.src('content/**/*.less')
    .pipe(gulpIgnore.exclude(condition))

在许多情况下,您将所有 Less 代码放入一个 CSS 文件中。您的主文件包含 @import 指示所有其他必需的 Less 文件。