jstuckey / gulp-gzip

Gzip plugin for gulp.
MIT License
175 stars 17 forks source link

gzip not working with mirror and uglify #22

Open Luncher opened 8 years ago

Luncher commented 8 years ago

var fs = require('fs'); var path = require('path'); var gulp = require('gulp'); var gzip = require('gulp-gzip'); var gutil = require('gulp-util'); var uglify = require('gulp-uglify'); var concat = require('gulp-concat'); var rename = require('gulp-rename'); var mirror = require('gulp-mirror'); var sourcemaps = require('gulp-sourcemaps');

gulp.task('build-ide', function() { var distDir = './js'; var commonFiles = fs.readFileSync('build/common_files', 'utf8').split('\n'); var gameFiles = fs.readFileSync('build/gamebuilder_files', 'utf8').split('\n');

var min = rename('gamebuilder.js');
min.pipe(uglify())
.pipe(gzip({append: true}));

return gulp.src(gameFiles.concat(commonFiles))
.pipe(sourcemaps.init())
.pipe(concat('gamebuilder-debug.js'))
.pipe(mirror(min))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(distDir));

});

the result cant generate min files, but can not gzip files,

jstuckey commented 8 years ago

Hey @Luncher! I tried my best to reproduce your gulp task to see what is going on.

What is the result of gameFiles.concat(commonFiles)? Does that concatenate the contents of those files? gulp.src takes in a glob string rather than the contents of files.

Also, I got an exception from uglify unless I wrote it like this:

  var min = rename('gamebuilder.js')
    .pipe(uglify())
    .pipe(gzip({append: true}));

Does that make a difference for you?

Luncher commented 8 years ago

Hey @jstuckey thank you for your reply, the gameFiles.concat(commonFiles) result is file path, not the file contents.

I am gulp beginners, I think this is the relative lack of learning materials .

Can you help me explain the following two methods are different?

var min = rename('gamebuilder.js') .pipe(uglify()) .pipe(gzip({append: true}));


var min = rename('gamebuilder.js'); min.pipe(uglify()) .pipe(gzip({append: true}));

jstuckey commented 8 years ago

The value that gets assigned to min is different. In the original, the value of min is only the rename stream. In the new version, the value of min is the pipeline of the three streams (rename, uglify, and gzip)

For example:


var min1 = rename('gamebuilder.js');
min1.pipe(uglify())
  .pipe(gzip({append: true}));

var min2 = rename('gamebuilder.js')
  .pipe(uglify())
  .pipe(gzip({append: true}));

console.log(min1);
console.log(min2);
console.log(min1 === min2);
Luncher commented 8 years ago

got it, but I still do not understand why can not generate gzip.

jstuckey commented 8 years ago

I was able to generate a gzipped file using

var min = rename('gamebuilder.js')
  .pipe(uglify())
  .pipe(gzip({append: true}));

Does that not work for you?