当使用gulp-gzip时,Gulp SourceMaps无法正常工作

时间:2015-07-10 08:23:58

标签: node.js express gzip gulp gulp-sourcemaps

我正在使用Gulp来uglify javascript文件并生成他们的源地图。到目前为止一切顺利:

var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();

gulp.task('compress-js', ['clean-js'], function() {
  return gulp.src('./resources/js/**/*.js')
    .pipe(plugins.sourcemaps.init())
    .pipe(plugins.uglify())
    .pipe(plugins.rename({extname: '.min.js'}))
    .pipe(plugins.sourcemaps.write('./maps'))
    .pipe(gulp.dest('./public/js'))
    .pipe(plugins.livereload());
});

此代码生成以下输出文件夹和文件,这些文件夹和文件由浏览器正确加载:

公开/ JS / someScript.min.js

function doSomeStuff(){console.log("This stuff function simply says: hello!!")
//# sourceMappingURL=maps/someScript.min.js.map

public / js / maps / someScript.min.js.map :请注意,此文件在上面的行中引用。

但是我也想对它们进行gzip,所以我更新的gulpfile.js是:

gulp.task('compress-js', ['clean-js'], function() {
  return gulp.src('./resources/js/**/*.js')
    .pipe(plugins.sourcemaps.init())
    .pipe(plugins.uglify())
    .pipe(plugins.rename({extname: '.min.js'}))
    .pipe(plugins.sourcemaps.write('./maps'))
    .pipe(plugins.gzip())
    .pipe(gulp.dest('./public/js'))
    .pipe(plugins.livereload());
});

现在我收到这些文件:

公开/ JS / someScript.min.js.gz

function doSomeStuff(){console.log("This stuff function simply says: hello!!")}
//# sourceMappingURL=maps/someScript.min.js.map

公开/ JS /地图/ someScript.min.js.map.gz

源地图未被链接,因为js.gz文件引用 someScript.min.js.map 而不是 someScript.min.js.map.gz 。我错过了什么?如何使文件引用正确的扩展名?

2 个答案:

答案 0 :(得分:3)

我设法通过重命名生成的地图文件,删除.gz扩展名来解决此问题:

gulp.task('compress-js', function(cb) {
  gulp.src('./resources/js/**/*.js')
    .pipe(plugins.jshint())
    .pipe(plugins.sourcemaps.init())
    .pipe(plugins.uglify())
    .pipe(plugins.rename({extname: '.min.js'}))
    .pipe(plugins.sourcemaps.write('./maps'))
    .pipe(plugins.gzip({ append: true }))
    .pipe(gulp.dest('./public/js'))
    .pipe(plugins.livereload())
    .on('end', function() {
        postProcessMapFiles('./public/js/maps/**/*.map.gz', './public/js/maps', cb);
    });
});

function postProcessMapFiles(compressedMapFiles, publicDir, cb){
    gulp.src(compressedMapFiles)
       .pipe(plugins.debug())
       .pipe(plugins.rename({extname: ''}))
       .pipe(plugins.debug())
       .pipe(gulp.dest(publicDir))
       .on('end', function() {
          del([compressedMapFiles], cb);
       });
}

答案 1 :(得分:0)

我做了gunzip地图文件。

var gunzip = require('gulp-gunzip');

// this task will depend on task sass, that generates the problem
gulp.task('uncompress_map', ['sass'], function () {
    var maploc = 'src/build/maps';
    return gulp.src(maploc+'/*.gz')
        .pipe(gunzip())
        .pipe(gulp.dest(maploc));
});