简体   繁体   English

Gulp - 源映射未在 Chrome 中加载源

[英]Gulp - sourcemaps aren't loading source in Chrome

I haven't tested all browses but I use chrome regularly.我没有测试过所有浏览器,但我经常使用 chrome。

With Grunt I had no problem with uglify and the map file produced.使用 Grunt,我对 uglify 和生成的地图文件没有任何问题。 In Chrome, when in the developer tools, both the min.js and the original .js are loaded.在 Chrome 中,当在开发者工具中时,min.js 和原始 .js 都被加载。

I'm trying to learn Gulp and after hours of trying I cannot get the map file to link the original .js to match the .min.js.我正在尝试学习 Gulp,经过数小时的尝试,我无法让地图文件链接原始 .js 以匹配 .min.js。

Here is my gulpfile.js.这是我的 gulpfile.js。 What am I doing wrong?我究竟做错了什么?

var gulp = require('gulp')
    , uglify = require('gulp-uglify')
    , rename = require('gulp-rename')
    , sourcemaps = require('gulp-sourcemaps');

// default means this task will run whenever you type “gulp” at the command line without any parameters.
gulp.task('uglify:apps', function () {

    gulp.src(['core/apps/**/*.js', '!/**/*.min.js'])
        .pipe(sourcemaps.init())
        .pipe(uglify())
        .pipe(rename({ extname: ".min.js" }))
        .pipe(sourcemaps.write("."))
        .pipe(gulp.dest('core/apps/'))
    ;

});


gulp.task('default', ['uglify:apps']);

In my case I updated gulp-uglify and gulp-sourcemaps to: "gulp-sourcemaps": "2.6.5", "gulp-uglify": "3.0.2",就我而言,我将 gulp-uglify 和 gulp-sourcemaps 更新为:"gulp-sourcemaps": "2.6.5", "gulp-uglify": "3.0.2",

And I changed a little minification function from:我改变了一些缩小功能:

.pipe(sourcemaps.write('.', { includeContent: false, sourceRoot: '../' }))

to:到:

.pipe(sourcemaps.write('.', { includeContent: false, sourceRoot: '.' }))

for some reason when sourceRoot: '../' I cant see sources in browser (but debugger; and breakpoints were stopping the execution, I just didnt see the code)出于某种原因,当sourceRoot: '../'我无法在浏览器中看到源代码(但是调试器;并且断点正在停止执行,我只是没有看到代码)

My whole code right now我现在的整个代码

function minifyJs(src, dest) {
var minifyPaths = [src];
return gulp.src(minifyPaths)
.pipe(domSrc.duplex({ selector: 'script[data-concat!="false"]', attribute: 'src', cwd: '../' })) //dont pay attantion here
.pipe(sourcemaps.init())
.pipe(babel({
    presets: ["es2015-script"],
    compact: false
}))
.pipe(concat(dest))
.pipe(gulp.dest("."))
.pipe(uglify({
    compress: {
        unused: false
    }
}))
.pipe(rename({ extname: '.min.js' }))
.pipe(sourcemaps.write('.', { includeContent: false, sourceRoot: '.' }))
.pipe(gulp.dest("."));

} }

gulp-uglify as of Jan 15 2014 does not have working sourcemaps built in. gulp-uglify为2014年1月15日的没有内置的工作sourcemaps。

This is the current solution for getting sourcemaps to work:这是使源映射正常工作的当前解决方案:

npm install gulp-uglify@https://github.com/floridoo/gulp-uglify/tarball/sourcemap_fix --save-dev

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


gulp.task('ug', function(){
  gulp.src('./test.js')
    .pipe(sourcemaps.init())
    .pipe(uglify())
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('./out'));
});

gulp.task('default', ['ug']);

Example repo: https://github.com/stevelacy/gulp-test-sourcemaps示例存储库: https : //github.com/stevelacy/gulp-test-sourcemaps

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

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