jstuckey / gulp-gzip

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

Keep original files #17

Closed sc0rp10 closed 9 years ago

sc0rp10 commented 9 years ago

Hello. How can I keep original files after .pipe(gzip())? Maybe is there special zlib option? Thanks.

jstuckey commented 9 years ago

Hey, @sc0rp10. Sorry, I didn't notice your question earlier.

The original files should not be affected. Could you please share the full code you're using?

sc0rp10 commented 9 years ago

Hello! Sure, I've created demo repository for demonstrating this problem: https://github.com/sc0rp10/gzip-gulp-demo Please clone it, and run npm run build within. After building, you're got folder build with only one file: app.css.gz, but I need two files: app.css and app.css.gz. Is it possbile?

jstuckey commented 9 years ago

Thanks! That helps clarify.

I recommend adding a second gulp task that copies the files. Then you can run both tasks together to accomplish what you're after.

Something like this:

var gulp = require("gulp");
var gzip = require("gulp-gzip");

gulp.task("default", ["copy", "gzip"]);

gulp.task("copy", function () {
    return gulp.src("./src/*")
        .pipe(gulp.dest("./build"));
});

gulp.task("gzip", function () {
    return gulp.src("./src/*")
        .pipe(gzip())
        .pipe(gulp.dest("./build"));
});
sc0rp10 commented 9 years ago

@jstuckey thanks for solution, but it adds some complicate to my workflow. My task looks like this:

gulp.task("minify-css", function() {
    return gulp.src(SOURCE_POSTCSS_GLOB)
        .pipe(plugins.concat(CONCATENATED_POSTCSS_FILE))
        .pipe(plugins.postcss(postcss_plugins))
        .pipe(plugins.rename({
            extname: ".css"
        }))
        .pipe(plugins.shorthand())
        .pipe(plugins.csso())
        .pipe(plugins.minify_css())
        .pipe(plugins.rename(MINIFIED_CSS_FILE))
        .pipe(plugins.gzip())
        .pipe(gulp.dest(BUILD_CSS_PATH));
});

i.e. I haven't original file in other task - it's be generated by some pipes queue. And there will be nice option like 'dont remove original files' in gzip() function. Maybe I can make PR with it. What do you think about it?

sc0rp10 commented 9 years ago

P.S. Original files needed by http://nginx.org/en/docs/http/ngx_http_gzip_static_module.html - without it this module won't work and returns 404 to each .css.gz file without .css in same dir.

jstuckey commented 9 years ago

Would this work?

gulp.task("default", ["minify-css", "gzip-css"]);

gulp.task("minify-css", function() {
    return gulp.src(SOURCE_POSTCSS_GLOB)
        .pipe(plugins.concat(CONCATENATED_POSTCSS_FILE))
        .pipe(plugins.postcss(postcss_plugins))
        .pipe(plugins.rename({
            extname: ".css"
        }))
        .pipe(plugins.shorthand())
        .pipe(plugins.csso())
        .pipe(plugins.minify_css())
        .pipe(plugins.rename(MINIFIED_CSS_FILE))
        .pipe(gulp.dest(BUILD_CSS_PATH));
});

gulp.task("gzip-css", function() {
    return gulp.src(BUILD_CSS_GLOB)
        .pipe(plugins.gzip())
        .pipe(gulp.dest(BUILD_CSS_PATH));
});

Where BUILD_CSS_GLOB refers to the minified css files in your build directory.

sc0rp10 commented 9 years ago

I'm newbie in gulp, but I think that gzip-css may start earlier than minify-css ends and result will be unpredictable. And plugins.gzip() still removes BUILD_CSS_GLOB file. IMO, single option 'keep_original' in gzip() will be more intuitive, than additional task with own limitations.

thebuilder commented 9 years ago

Try outputting the file with gulp.dest() before you gzip it.

gulp.task("minify-css", function() {
    return gulp.src(SOURCE_POSTCSS_GLOB)
        .pipe(plugins.concat(CONCATENATED_POSTCSS_FILE))
        .pipe(plugins.postcss(postcss_plugins))
        .pipe(plugins.rename({
            extname: ".css"
        }))
        .pipe(plugins.shorthand())
        .pipe(plugins.csso())
        .pipe(plugins.minify_css())
        .pipe(plugins.rename(MINIFIED_CSS_FILE))
        .pipe(gulp.dest(BUILD_CSS_PATH));
        .pipe(plugins.gzip())
        .pipe(gulp.dest(BUILD_CSS_PATH));
});
sc0rp10 commented 9 years ago

@thebuilder i tried it - you can see it in my test repo below (i've updated it). If i call gzip after dest in my build dir contains only css file, not css.gz.

sc0rp10 commented 9 years ago

@thebuilder ahh, I'm very inattentive, and didn't see you point. It looks like a nice solution, thanks!

jstuckey commented 9 years ago

Is that working for you, @sc0rp10?

I was reluctant to add a keep_original option because gulp tends to favor composing plugins over adding configuration options. This behavior seemed like something that could be accomplished without adding another option to gulp-gzip. It also seemed odd to output two files when only one is input.

Thanks, @thebuilder, for an idiomatic-gulp way of accomplishing this!

sc0rp10 commented 9 years ago

@jstuckey it sounds very logical. With @thebuilder method this option will be useless. Thank, guys!

jstuckey commented 9 years ago

Great! :tada: