gulp sourcemap已创建但无法正常工作

时间:2016-02-05 18:38:11

标签: javascript css gulp

样式

styles.less

@import "one.less";
@import "two.less";

one.less

body { background:red; }

two.less

body { font-size: 12px; }

Gulpfile.js

var gulp = require('gulp');
var less = require('gulp-less');
var sourcemaps = require('gulp-sourcemaps');

gulp.src('./src/assets/less/styles.less')
  .pipe(less())
  .pipe(sourcemaps.init({loadMaps: true}))
  .pipe(sourcemaps.write('./'))
  .pipe(gulp.dest('./dist/css/'));

结果: styles.css + styles.map.css已创建,但是当我进入网页时,地图文件没有加载(当我检查“body”时,我看到样式。 CSS)

链接
少喝 - https://github.com/plus3network/gulp-less
gulp-sourcemaps - https://github.com/floridoo/gulp-sourcemaps

我很沮丧,我很感激任何帮助。感谢。

3 个答案:

答案 0 :(得分:4)

在初始化sourcemap插件之前,你正在“管道”到less(),正确的方法是:

var gulp = require('gulp');
var less = require('gulp-less');
var sourcemaps = require('gulp-sourcemaps');

gulp.src('./src/assets/less/styles.less')
  .pipe(sourcemaps.init({loadMaps: true}))
  .pipe(less())//<<< between init and write
  .pipe(sourcemaps.write('./'))
  .pipe(gulp.dest('./dist/css/')); 

答案 1 :(得分:0)

好的,这就是我应该使用的 - https://github.com/radist2s/gulp-less-sourcemap

答案 2 :(得分:0)

在我的情况下,我在initwrite之间编写了代码,但仍然没有在浏览器中加载地图

gulp.task('css-task',async function(){
    return gulp.src('project/public/sass/main.scss')
            .pipe(sourcemaps.init()) // init the source map
            .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
            .pipe(prefix('last 2 versions'))
            .pipe(sourcemaps.write('.'))
            .pipe(gulp.dest('dist/public/css'))
            .pipe(livereload());
});

我在loadMaps: true上添加了sourcemaps.init(),一切正常

gulp.task('css-task',async function(){
    return gulp.src('project/public/sass/main.scss')
            .pipe(sourcemaps.init({loadMaps: true})) // init the source map 
            ...
            .pipe(sourcemaps.write('.'))
            .pipe(gulp.dest('dist/public/css'))
            .pipe(livereload());
});