如何让源图包含所有我的LESS文件与gulp

时间:2014-08-31 12:18:23

标签: less gulp gulp-less gulp-sourcemaps

我/ less文件夹中有两个LESS文件:

main.less:

@import 'vars';

body{
  background-color: @blau;
}

和vars.less

@blau : #6621ab;

我的gulp任务使用gulp-less和gulp-sourcemaps

gulp.task('less', function () {
  gulp.src('./less/main.less')
    .pipe(sourcemaps.init())
    .pipe(less())
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('./public/'))
});

CSS生成(在/public/main.css)工作正常,但在源图中,我只能看到main.less,而不是vars.less。任何的想法?提前致谢

1 个答案:

答案 0 :(得分:3)

据我所知,您的配置会生成以下源代码代码:

/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm1haW4ubGVzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFFQTtFQUNFLHlCQUFBIiwiZmlsZSI6Im1haW4uY3NzIiwic291cmNlc0NvbnRlbnQiOlsiQGltcG9ydCAndmFycyc7XG5cbmJvZHl7XG4gIGJhY2tncm91bmQtY29sb3I6IEBibGF1O1xufVxuIl0sInNvdXJjZVJvb3QiOiIvc291cmNlLyJ9 */

编码版本:

{"version":3,"sources":["main.less"],"names":[],"mappings":"AAEA;EACE,yBAAA","file":"main.css","sourcesContent":["@import 'vars';\n\nbody{\n  background-color: @blau;\n}\n"],"sourceRoot":"/source/"}

您的vars.less不会向CSS生成任何输出,因此不应包含在源地图中。

只要您的vars.less生成输出,例如在此文件的末尾添加.selector {p:1;},该文件也将包含在源地图中:

{"version":3,"sources":["vars.less","main.less"],"names":[],"mappings":"AACA;EAAW,IAAA;;ACCX;EACE,yBAAA","file":"main.css","sourcesContent":["@blau : #6621ab;\n.selector {p:1;}\n","@import 'vars';\n\nbody{\n  background-color: @blau;\n}\n"],"sourceRoot":"/source/"}

请注意,lessc编译器对源映射有不同的选项:

  --source-map[=FILENAME]  Outputs a v3 sourcemap to the filename (or output filename.map)
  --source-map-rootpath=X  adds this path onto the sourcemap filename and less file paths
  --source-map-basepath=X  Sets sourcemap base path, defaults to current working directory.
  --source-map-less-inline puts the less files into the map instead of referencing them
  --source-map-map-inline  puts the map (and any less files) into the output css file
  --source-map-url=URL     the complete url and filename put in the less file

gulp-sourcemaps输出与使用--source-map-less-inline--source-map-map-inline选项进行编译相同的结果