1

我正在使用 Gulp 对 javascript 文件进行 uglify 并生成它们的源映射。到目前为止,一切都很好:

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

public/js/maps/someScript.min.js.map.gz

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

4

2 回答 2

4

我设法通过重命名生成的地图文件,删除 .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);
       });
}
于 2015-07-23T12:42:36.507 回答
0

我确实枪压缩了地图文件。

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));
});
于 2018-05-14T14:04:05.570 回答