简体   繁体   English

将 Gulp 与源映射插件一起使用时,源映射不正确

[英]Incorrect source maps when using Gulp with source maps plugin

I have recently started using Gulp as I much prefer the syntax over Grunt and whilst I like it for the most part I have noticed when using Chrome or any browser for that matter it tells me the incorrect line mappings when looking in the inspector.我最近开始使用Gulp因为我更喜欢Grunt的语法,虽然我在很大程度上喜欢它,但我注意到在使用Chrome或任何浏览器时它会告诉我在检查器中查看错误的行映射。

It always tells me the correct file , however it incorrectly tells me the line it is on.它总是告诉我正确的文件,但是它错误地告诉我它所在的

What I have found is it seems to have problems with the Sass nesting ;我发现它似乎与Sass嵌套有问题; in that the line number it gives me is the line for where the first parent starts, so for example in the below code:因为它给我的行号是第一个父级开始的行,例如在下面的代码中:

#foo {

    .bar {

    }

}

If I am trying to inspect an element that has the bar class, it will tell me it's on line 1 rather than 3 .如果我试图检查具有bar类的元素,它会告诉我它在第1行而不是第3

Here is my gulpfile.js file:这是我的gulpfile.js文件:

var gulp = require('gulp');
var concat = require('gulp-concat');
var plumber = require('gulp-plumber');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var autoprefixer = require('gulp-autoprefixer');

gulp.task('scripts', function() {
    gulp.src([
        'js_dev/jquery.js',
        'js_dev/plugins/*.js',
        // We have to set the bootstrap lines separately as some need to go before others
        'js_dev/bootstrap/alert.js',
        'js_dev/bootstrap/collapse.js',
        'js_dev/bootstrap/modal.js',
        'js_dev/bootstrap/tooltip.js',
        'js_dev/bootstrap/popover.js',
        'js_dev/bootstrap/tab.js',
        'js_dev/bootstrap/transition.js',
        'js_dev/scripts.js'
    ])
        .pipe(plumber())
        .pipe(sourcemaps.init())
        .pipe(concat('scripts.js'))
        .pipe(sourcemaps.write('../maps'))
        .pipe(gulp.dest('./js'))
});

gulp.task('styles', function() {
    gulp.src('scss/**/*.scss')
        .pipe(plumber())
        .pipe(sourcemaps.init())
        .pipe(sass())
        .pipe(autoprefixer({
            browsers: [
                'last 4 Chrome versions',
                'last 4 Firefox versions',
                'last 4 Edge versions',
                'last 2 Safari versions',
                'ie >= 10',
                '> 1.49%',
                'not ie <= 9'
            ],
            cascade: false
        }))
        .pipe(sourcemaps.write('../maps'))
        .pipe(gulp.dest('./css'));
});

gulp.task('watch', function() {
    gulp.watch(['scss/**/*.scss', 'js_dev/**/*.js'], ['scripts', 'styles']);
});

gulp.task('default', ['scripts', 'styles']);

The weird issue is that having more than one plugins between gulp-sourcempas is causing this issue.奇怪的问题是在gulp-sourcempas之间有多个插件会导致这个问题。 This is bug and currently I am able to fix is by temporarily removing extra plugins.这是错误,目前我可以通过临时删除额外的插件来修复。 Here is code which most probably will work这是最有可能工作的代码

gulp.task('styles', function() {
    gulp.src('scss/**/*.scss')
        .pipe(plumber())
        .pipe(sourcemaps.init())
        .pipe(sass())
        .pipe(sourcemaps.write('../maps'))
        .pipe(gulp.dest('./css'));
});

I know this is not real solution, because we need all sorts of other plugins, but this is what works to show correct line numbers.我知道这不是真正的解决方案,因为我们需要各种其他插件,但这是显示正确行号的方法。 You can watch this issue on github to monitor progress in this bug.您可以在 github 上观看此问题以监控此错误的进度。

I commented out the autoprefixer from between less and source maps write and it shows correct line number now in my case.我从 less 和 source maps write 之间注释掉了 autoprefixer,现在在我的情况下它显示了正确的行号。

gulp.src(['css/custom.less'])
        .pipe(sourcemaps.init())
        .pipe(less())
        // .pipe(autoprefixer({
        //    cascade: false
        // }))
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest('css/'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM